blob: 8392f2a7919a9f9cf7519577f079b50fdbc40956 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040024#define _GNU_SOURCE
25
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026#include "../config.h"
27
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040042
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030043#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030056#else /* HAVE_CAIRO_EGL */
57typedef void *EGLDisplay;
58typedef void *EGLConfig;
59typedef void *EGLContext;
60#define EGL_NO_DISPLAY ((EGLDisplay)0)
61#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062
Daniel Stone9d4f0302012-02-15 16:33:21 +000063#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030064#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040065
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020067#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040068#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131};
132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400134 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400136 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500137 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500139 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_CUSTOM
141};
Rob Bradford7507b572012-05-15 17:55:34 +0100142
143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanenec076692012-11-30 13:37:27 +0200148enum toysurface_prepare_flags {
149 SURFACE_HINT_RESIZE = 0x01,
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
157 * width,height are the new surface size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200163 int width, int height, uint32_t flags);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164
165 /*
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
169 */
170 void (*swap)(struct toysurface *base,
171 struct rectangle *server_allocation);
172
173 /*
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
176 */
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
178
179 /*
180 * Release the toysurface from the EGL context, returning control
181 * to Cairo.
182 */
183 void (*release)(struct toysurface *base);
184
185 /*
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
188 */
189 void (*destroy)(struct toysurface *base);
190};
191
Pekka Paalanen4e373742013-02-13 16:17:13 +0200192struct surface {
193 struct window *window;
194
195 struct wl_surface *surface;
196};
197
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500198struct window {
199 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500200 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100201 struct wl_list window_output_list;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500202 struct wl_region *input_region;
203 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500204 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500205 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400206 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500207 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500208 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400209 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400210 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400211 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400212 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400213 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400214 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400215 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400216 int focus_count;
217
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400218 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200219 enum wl_output_transform buffer_transform;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200220 struct toysurface *toysurface;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400221 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500222
Pekka Paalanen99436862012-11-19 17:15:59 +0200223 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200224 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400225
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500226 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500227 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400228 window_data_handler_t data_handler;
229 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500230 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400231 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200232 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400233
Pekka Paalanen4e373742013-02-13 16:17:13 +0200234 struct surface *main_surface;
235 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300236 struct wl_callback *frame_cb;
237
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200238 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500239 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500240
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500241 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400242 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500243};
244
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500245struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500246 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300247 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500248 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400249 struct wl_list link;
250 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500251 widget_resize_handler_t resize_handler;
252 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500253 widget_enter_handler_t enter_handler;
254 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500255 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500256 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200257 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400258 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500259 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300260 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500261 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400262};
263
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400264struct input {
265 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100266 struct wl_seat *seat;
267 struct wl_pointer *pointer;
268 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400269 struct window *pointer_focus;
270 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300271 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300272 uint32_t cursor_anim_start;
273 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300274 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400275 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400276 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400277 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400278 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400279 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400280
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500281 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500282 struct widget *grab;
283 uint32_t grab_button;
284
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400285 struct wl_data_device *data_device;
286 struct data_offer *drag_offer;
287 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100288
289 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100290 struct xkb_keymap *keymap;
291 struct xkb_state *state;
292 xkb_mod_mask_t control_mask;
293 xkb_mod_mask_t alt_mask;
294 xkb_mod_mask_t shift_mask;
295 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400296
297 struct task repeat_task;
298 int repeat_timer_fd;
299 uint32_t repeat_sym;
300 uint32_t repeat_key;
301 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400302};
303
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500304struct output {
305 struct display *display;
306 struct wl_output *output;
307 struct rectangle allocation;
308 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600309 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200310
311 display_output_handler_t destroy_handler;
312 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500313};
314
Martin Minarik1998b152012-05-10 02:04:35 +0200315enum frame_button_action {
316 FRAME_BUTTON_NULL = 0,
317 FRAME_BUTTON_ICON = 1,
318 FRAME_BUTTON_CLOSE = 2,
319 FRAME_BUTTON_MINIMIZE = 3,
320 FRAME_BUTTON_MAXIMIZE = 4,
321};
322
323enum frame_button_pointer {
324 FRAME_BUTTON_DEFAULT = 0,
325 FRAME_BUTTON_OVER = 1,
326 FRAME_BUTTON_ACTIVE = 2,
327};
328
329enum frame_button_align {
330 FRAME_BUTTON_RIGHT = 0,
331 FRAME_BUTTON_LEFT = 1,
332};
333
334enum frame_button_decoration {
335 FRAME_BUTTON_NONE = 0,
336 FRAME_BUTTON_FANCY = 1,
337};
338
339struct frame_button {
340 struct widget *widget;
341 struct frame *frame;
342 cairo_surface_t *icon;
343 enum frame_button_action type;
344 enum frame_button_pointer state;
345 struct wl_list link; /* buttons_list */
346 enum frame_button_align align;
347 enum frame_button_decoration decoration;
348};
349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500350struct frame {
351 struct widget *widget;
352 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200353 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500354};
355
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500356struct menu {
357 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500358 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500359 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500360 const char **entries;
361 uint32_t time;
362 int current;
363 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400364 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500365 menu_func_t func;
366};
367
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300368struct tooltip {
369 struct widget *parent;
370 struct window *window;
371 struct widget *widget;
372 char *entry;
373 struct task tooltip_task;
374 int tooltip_fd;
375 float x, y;
376};
377
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700378struct shm_pool {
379 struct wl_shm_pool *pool;
380 size_t size;
381 size_t used;
382 void *data;
383};
384
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400385enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300386 CURSOR_DEFAULT = 100,
387 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400388};
389
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500390enum window_location {
391 WINDOW_INTERIOR = 0,
392 WINDOW_RESIZING_TOP = 1,
393 WINDOW_RESIZING_BOTTOM = 2,
394 WINDOW_RESIZING_LEFT = 4,
395 WINDOW_RESIZING_TOP_LEFT = 5,
396 WINDOW_RESIZING_BOTTOM_LEFT = 6,
397 WINDOW_RESIZING_RIGHT = 8,
398 WINDOW_RESIZING_TOP_RIGHT = 9,
399 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
400 WINDOW_RESIZING_MASK = 15,
401 WINDOW_EXTERIOR = 16,
402 WINDOW_TITLEBAR = 17,
403 WINDOW_CLIENT_AREA = 18,
404};
405
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200406static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500408#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200410struct egl_window_surface {
411 struct toysurface base;
412 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100413 struct display *display;
414 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200415 struct wl_egl_window *egl_window;
416 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100417};
418
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200419static struct egl_window_surface *
420to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100421{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200422 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100423}
424
425static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200426egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200427 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100428{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429 struct egl_window_surface *surface = to_egl_window_surface(base);
430
431 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
432 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
433
434 return cairo_surface_reference(surface->cairo_surface);
435}
436
437static void
438egl_window_surface_swap(struct toysurface *base,
439 struct rectangle *server_allocation)
440{
441 struct egl_window_surface *surface = to_egl_window_surface(base);
442
443 cairo_gl_surface_swapbuffers(surface->cairo_surface);
444 wl_egl_window_get_attached_size(surface->egl_window,
445 &server_allocation->width,
446 &server_allocation->height);
447}
448
449static int
450egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
451{
452 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200453 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100454
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200455 device = cairo_surface_get_device(surface->cairo_surface);
456 if (!device)
457 return -1;
458
459 if (!ctx) {
460 if (device == surface->display->argb_device)
461 ctx = surface->display->argb_ctx;
462 else
463 assert(0);
464 }
465
466 cairo_device_flush(device);
467 cairo_device_acquire(device);
468 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
469 surface->egl_surface, ctx))
470 fprintf(stderr, "failed to make surface current\n");
471
472 return 0;
473}
474
475static void
476egl_window_surface_release(struct toysurface *base)
477{
478 struct egl_window_surface *surface = to_egl_window_surface(base);
479 cairo_device_t *device;
480
481 device = cairo_surface_get_device(surface->cairo_surface);
482 if (!device)
483 return;
484
485 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
486 surface->display->argb_ctx))
487 fprintf(stderr, "failed to make context current\n");
488
489 cairo_device_release(device);
490}
491
492static void
493egl_window_surface_destroy(struct toysurface *base)
494{
495 struct egl_window_surface *surface = to_egl_window_surface(base);
496 struct display *d = surface->display;
497
498 cairo_surface_destroy(surface->cairo_surface);
499 eglDestroySurface(d->dpy, surface->egl_surface);
500 wl_egl_window_destroy(surface->egl_window);
501 surface->surface = NULL;
502
503 free(surface);
504}
505
506static struct toysurface *
507egl_window_surface_create(struct display *display,
508 struct wl_surface *wl_surface,
509 uint32_t flags,
510 struct rectangle *rectangle)
511{
512 struct egl_window_surface *surface;
513
Pekka Paalanenb3627362012-11-19 17:16:00 +0200514 if (display->dpy == EGL_NO_DISPLAY)
515 return NULL;
516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 surface = calloc(1, sizeof *surface);
518 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519 return NULL;
520
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 surface->base.prepare = egl_window_surface_prepare;
522 surface->base.swap = egl_window_surface_swap;
523 surface->base.acquire = egl_window_surface_acquire;
524 surface->base.release = egl_window_surface_release;
525 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527 surface->display = display;
528 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 surface->egl_window = wl_egl_window_create(surface->surface,
531 rectangle->width,
532 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 surface->egl_surface = eglCreateWindowSurface(display->dpy,
535 display->argb_config,
536 surface->egl_window,
537 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100538
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 surface->cairo_surface =
540 cairo_gl_surface_create_for_egl(display->argb_device,
541 surface->egl_surface,
542 rectangle->width,
543 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100544
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100546}
547
Pekka Paalanenb3627362012-11-19 17:16:00 +0200548#else
549
550static struct toysurface *
551egl_window_surface_create(struct display *display,
552 struct wl_surface *wl_surface,
553 uint32_t flags,
554 struct rectangle *rectangle)
555{
556 return NULL;
557}
558
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559#endif
560
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200561struct shm_surface_data {
562 struct wl_buffer *buffer;
563 struct shm_pool *pool;
564};
565
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566struct wl_buffer *
567display_get_buffer_for_surface(struct display *display,
568 cairo_surface_t *surface)
569{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200570 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200572 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400573
574 return data->buffer;
575}
576
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500577static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700578shm_pool_destroy(struct shm_pool *pool);
579
580static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581shm_surface_data_destroy(void *p)
582{
583 struct shm_surface_data *data = p;
584
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200585 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700586 if (data->pool)
587 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300588
589 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590}
591
Kristian Høgsberg16626282012-04-03 11:21:27 -0400592static struct wl_shm_pool *
593make_shm_pool(struct display *display, int size, void **data)
594{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400595 struct wl_shm_pool *pool;
596 int fd;
597
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300598 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400599 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300600 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
601 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400602 return NULL;
603 }
604
605 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400606 if (*data == MAP_FAILED) {
607 fprintf(stderr, "mmap failed: %m\n");
608 close(fd);
609 return NULL;
610 }
611
612 pool = wl_shm_create_pool(display->shm, fd, size);
613
614 close(fd);
615
616 return pool;
617}
618
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700619static struct shm_pool *
620shm_pool_create(struct display *display, size_t size)
621{
622 struct shm_pool *pool = malloc(sizeof *pool);
623
624 if (!pool)
625 return NULL;
626
627 pool->pool = make_shm_pool(display, size, &pool->data);
628 if (!pool->pool) {
629 free(pool);
630 return NULL;
631 }
632
633 pool->size = size;
634 pool->used = 0;
635
636 return pool;
637}
638
639static void *
640shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
641{
642 if (pool->used + size > pool->size)
643 return NULL;
644
645 *offset = pool->used;
646 pool->used += size;
647
648 return (char *) pool->data + *offset;
649}
650
651/* destroy the pool. this does not unmap the memory though */
652static void
653shm_pool_destroy(struct shm_pool *pool)
654{
655 munmap(pool->data, pool->size);
656 wl_shm_pool_destroy(pool->pool);
657 free(pool);
658}
659
660/* Start allocating from the beginning of the pool again */
661static void
662shm_pool_reset(struct shm_pool *pool)
663{
664 pool->used = 0;
665}
666
667static int
668data_length_for_shm_surface(struct rectangle *rect)
669{
670 int stride;
671
672 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
673 rect->width);
674 return stride * rect->height;
675}
676
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500677static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700678display_create_shm_surface_from_pool(struct display *display,
679 struct rectangle *rectangle,
680 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681{
682 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400683 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400684 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400686 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
688 data = malloc(sizeof *data);
689 if (data == NULL)
690 return NULL;
691
692 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
693 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694 length = stride * rectangle->height;
695 data->pool = NULL;
696 map = shm_pool_allocate(pool, length, &offset);
697
698 if (!map) {
699 free(data);
700 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400701 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400703 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 CAIRO_FORMAT_ARGB32,
705 rectangle->width,
706 rectangle->height,
707 stride);
708
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200709 cairo_surface_set_user_data(surface, &shm_surface_data_key,
710 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400712 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500713 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400714 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500715 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400716
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200717 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
718 rectangle->width,
719 rectangle->height,
720 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400721
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700722 return surface;
723}
724
725static cairo_surface_t *
726display_create_shm_surface(struct display *display,
727 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200728 struct shm_pool *alternate_pool,
729 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700730{
731 struct shm_surface_data *data;
732 struct shm_pool *pool;
733 cairo_surface_t *surface;
734
Pekka Paalanen99436862012-11-19 17:15:59 +0200735 if (alternate_pool) {
736 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700737 surface = display_create_shm_surface_from_pool(display,
738 rectangle,
739 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200740 alternate_pool);
741 if (surface) {
742 data = cairo_surface_get_user_data(surface,
743 &shm_surface_data_key);
744 goto out;
745 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700746 }
747
748 pool = shm_pool_create(display,
749 data_length_for_shm_surface(rectangle));
750 if (!pool)
751 return NULL;
752
753 surface =
754 display_create_shm_surface_from_pool(display, rectangle,
755 flags, pool);
756
757 if (!surface) {
758 shm_pool_destroy(pool);
759 return NULL;
760 }
761
762 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200763 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700764 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400765
Pekka Paalanen99436862012-11-19 17:15:59 +0200766out:
767 if (data_ret)
768 *data_ret = data;
769
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400770 return surface;
771}
772
nobled7b87cb02011-02-01 18:51:47 +0000773static int
774check_size(struct rectangle *rect)
775{
776 if (rect->width && rect->height)
777 return 0;
778
779 fprintf(stderr, "tried to create surface of "
780 "width: %d, height: %d\n", rect->width, rect->height);
781 return -1;
782}
783
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784cairo_surface_t *
785display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400787 struct rectangle *rectangle,
788 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400789{
nobled7b87cb02011-02-01 18:51:47 +0000790 if (check_size(rectangle) < 0)
791 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200792
793 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200794 return display_create_shm_surface(display, rectangle, flags,
795 NULL, NULL);
796}
797
Pekka Paalanena4eda732012-11-19 17:16:02 +0200798struct shm_surface_leaf {
799 cairo_surface_t *cairo_surface;
800 /* 'data' is automatically destroyed, when 'cairo_surface' is */
801 struct shm_surface_data *data;
802
803 struct shm_pool *resize_pool;
804 int busy;
805};
806
807static void
808shm_surface_leaf_release(struct shm_surface_leaf *leaf)
809{
810 if (leaf->cairo_surface)
811 cairo_surface_destroy(leaf->cairo_surface);
812 /* leaf->data already destroyed via cairo private */
813
814 if (leaf->resize_pool)
815 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200816
817 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200818}
819
Pekka Paalanen99436862012-11-19 17:15:59 +0200820struct shm_surface {
821 struct toysurface base;
822 struct display *display;
823 struct wl_surface *surface;
824 uint32_t flags;
825 int dx, dy;
826
Pekka Paalanena4eda732012-11-19 17:16:02 +0200827 struct shm_surface_leaf leaf[2];
828 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200829};
830
831static struct shm_surface *
832to_shm_surface(struct toysurface *base)
833{
834 return container_of(base, struct shm_surface, base);
835}
836
Pekka Paalanena4eda732012-11-19 17:16:02 +0200837static void
838shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
839{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200840 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200841
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200842 if (surface->leaf[0].data->buffer == buffer)
843 surface->leaf[0].busy = 0;
844 else if (surface->leaf[1].data->buffer == buffer)
845 surface->leaf[1].busy = 0;
846 else
847 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200848
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200849 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
850 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200851}
852
853static const struct wl_buffer_listener shm_surface_buffer_listener = {
854 shm_surface_buffer_release
855};
856
Pekka Paalanen99436862012-11-19 17:15:59 +0200857static cairo_surface_t *
858shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200859 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200860{
Pekka Paalanenec076692012-11-30 13:37:27 +0200861 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200862 struct shm_surface *surface = to_shm_surface(base);
863 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200864 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200865
866 surface->dx = dx;
867 surface->dy = dy;
868
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200869 /* See shm_surface_buffer_release() */
870 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
871 surface->leaf[1].cairo_surface) {
872 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
873 __func__);
874 }
875
Pekka Paalanena4eda732012-11-19 17:16:02 +0200876 /* pick a free buffer from the two */
877 if (!surface->leaf[0].busy)
878 leaf = &surface->leaf[0];
879 else if (!surface->leaf[1].busy)
880 leaf = &surface->leaf[1];
881 else {
882 fprintf(stderr, "%s: both buffers are held by the server.\n",
883 __func__);
884 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200885 }
886
Pekka Paalanena4eda732012-11-19 17:16:02 +0200887 if (!resize_hint && leaf->resize_pool) {
888 cairo_surface_destroy(leaf->cairo_surface);
889 leaf->cairo_surface = NULL;
890 shm_pool_destroy(leaf->resize_pool);
891 leaf->resize_pool = NULL;
892 }
893
894 if (leaf->cairo_surface &&
895 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
896 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200897 goto out;
898
Pekka Paalanena4eda732012-11-19 17:16:02 +0200899 if (leaf->cairo_surface)
900 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200901
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200903 /* Create a big pool to allocate from, while continuously
904 * resizing. Mmapping a new pool in the server
905 * is relatively expensive, so reusing a pool performs
906 * better, but may temporarily reserve unneeded memory.
907 */
908 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200909 leaf->resize_pool = shm_pool_create(surface->display,
910 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200911 }
912
Pekka Paalanena4eda732012-11-19 17:16:02 +0200913 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 display_create_shm_surface(surface->display, &rect,
915 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 leaf->resize_pool,
917 &leaf->data);
918 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200919 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200920
921out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922 surface->current = leaf;
923
924 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200925}
926
927static void
928shm_surface_swap(struct toysurface *base,
929 struct rectangle *server_allocation)
930{
931 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200932 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200933
934 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200936 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200940 surface->dx, surface->dy);
941 wl_surface_damage(surface->surface, 0, 0,
942 server_allocation->width, server_allocation->height);
943 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200944
945 leaf->busy = 1;
946 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200947}
948
949static int
950shm_surface_acquire(struct toysurface *base, EGLContext ctx)
951{
952 return -1;
953}
954
955static void
956shm_surface_release(struct toysurface *base)
957{
958}
959
960static void
961shm_surface_destroy(struct toysurface *base)
962{
963 struct shm_surface *surface = to_shm_surface(base);
964
Pekka Paalanena4eda732012-11-19 17:16:02 +0200965 shm_surface_leaf_release(&surface->leaf[0]);
966 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200967
968 free(surface);
969}
970
971static struct toysurface *
972shm_surface_create(struct display *display, struct wl_surface *wl_surface,
973 uint32_t flags, struct rectangle *rectangle)
974{
975 struct shm_surface *surface;
976
977 surface = calloc(1, sizeof *surface);
978 if (!surface)
979 return NULL;
980
981 surface->base.prepare = shm_surface_prepare;
982 surface->base.swap = shm_surface_swap;
983 surface->base.acquire = shm_surface_acquire;
984 surface->base.release = shm_surface_release;
985 surface->base.destroy = shm_surface_destroy;
986
987 surface->display = display;
988 surface->surface = wl_surface;
989 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200990
991 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400992}
993
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200994/*
995 * The following correspondences between file names and cursors was copied
996 * from: https://bugs.kde.org/attachment.cgi?id=67313
997 */
998
999static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001000 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001001 "sw-resize"
1002};
1003
1004static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001005 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001006 "se-resize"
1007};
1008
1009static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001010 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001011 "s-resize"
1012};
1013
1014static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001015 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001016 "closedhand",
1017 "208530c400c041818281048008011002"
1018};
1019
1020static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001021 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001022 "default",
1023 "top_left_arrow",
1024 "left-arrow"
1025};
1026
1027static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001028 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001029 "w-resize"
1030};
1031
1032static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001033 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001034 "e-resize"
1035};
1036
1037static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001038 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001039 "nw-resize"
1040};
1041
1042static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001043 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001044 "ne-resize"
1045};
1046
1047static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001048 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001049 "n-resize"
1050};
1051
1052static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001053 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001054 "ibeam",
1055 "text"
1056};
1057
1058static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001059 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001060 "pointer",
1061 "pointing_hand",
1062 "e29285e634086352946a0e7090d73106"
1063};
1064
1065static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001066 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001067 "wait",
1068 "0426c94ea35c87780ff01dc239897213"
1069};
1070
1071struct cursor_alternatives {
1072 const char **names;
1073 size_t count;
1074};
1075
1076static const struct cursor_alternatives cursors[] = {
1077 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1078 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1079 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1080 {grabbings, ARRAY_LENGTH(grabbings)},
1081 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1082 {left_sides, ARRAY_LENGTH(left_sides)},
1083 {right_sides, ARRAY_LENGTH(right_sides)},
1084 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1085 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1086 {top_sides, ARRAY_LENGTH(top_sides)},
1087 {xterms, ARRAY_LENGTH(xterms)},
1088 {hand1s, ARRAY_LENGTH(hand1s)},
1089 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001090};
1091
1092static void
1093create_cursors(struct display *display)
1094{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001095 char *config_file;
1096 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001097 unsigned int i, j;
1098 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001099 struct config_key shell_keys[] = {
1100 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1101 };
1102 struct config_section cs[] = {
1103 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1104 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001105
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001106 config_file = config_file_path("weston.ini");
1107 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1108 free(config_file);
1109
1110 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001111 display->cursors =
1112 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1113
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001114 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001115 cursor = NULL;
1116 for (j = 0; !cursor && j < cursors[i].count; ++j)
1117 cursor = wl_cursor_theme_get_cursor(
1118 display->cursor_theme, cursors[i].names[j]);
1119
1120 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001121 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001122 cursors[i].names[0]);
1123
1124 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001125 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001126}
1127
1128static void
1129destroy_cursors(struct display *display)
1130{
1131 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001132 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001133}
1134
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001135struct wl_cursor_image *
1136display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001137{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001138 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001140 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001141}
1142
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001143static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001144window_get_resize_dx_dy(struct window *window, int *x, int *y)
1145{
1146 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1147 *x = window->server_allocation.width - window->allocation.width;
1148 else
1149 *x = 0;
1150
1151 if (window->resize_edges & WINDOW_RESIZING_TOP)
1152 *y = window->server_allocation.height -
1153 window->allocation.height;
1154 else
1155 *y = 0;
1156
1157 window->resize_edges = 0;
1158}
1159
1160static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001161window_attach_surface(struct window *window)
1162{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001163 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001164
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001165 if (window->type == TYPE_NONE) {
1166 window->type = TYPE_TOPLEVEL;
1167 if (display->shell)
1168 wl_shell_surface_set_toplevel(window->shell_surface);
1169 }
1170
Pekka Paalanen512dde82012-10-10 12:49:27 +03001171 if (window->opaque_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001172 wl_surface_set_opaque_region(window->main_surface->surface,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001173 window->opaque_region);
1174 wl_region_destroy(window->opaque_region);
1175 window->opaque_region = NULL;
1176 }
1177
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001178 if (window->input_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001179 wl_surface_set_input_region(window->main_surface->surface,
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001180 window->input_region);
1181 wl_region_destroy(window->input_region);
1182 window->input_region = NULL;
1183 }
1184
Pekka Paalanen99436862012-11-19 17:15:59 +02001185 window->toysurface->swap(window->toysurface,
1186 &window->server_allocation);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001187}
1188
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001189int
1190window_has_focus(struct window *window)
1191{
1192 return window->focus_count > 0;
1193}
1194
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001195static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001196window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001197{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001198 if (!window->cairo_surface)
1199 return;
1200
1201 window_attach_surface(window);
1202 cairo_surface_destroy(window->cairo_surface);
1203 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001204}
1205
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001206struct display *
1207window_get_display(struct window *window)
1208{
1209 return window->display;
1210}
1211
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001212static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001213window_create_surface(struct window *window)
1214{
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001215 struct rectangle allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001216 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001217 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001218
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001219 if (!window->transparent)
1220 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001221
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001222 switch (window->buffer_transform) {
1223 case WL_OUTPUT_TRANSFORM_90:
1224 case WL_OUTPUT_TRANSFORM_270:
1225 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1226 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1227 allocation.width = window->allocation.height;
1228 allocation.height = window->allocation.width;
1229 break;
1230 default:
1231 break;
1232 }
1233
Pekka Paalanenb3627362012-11-19 17:16:00 +02001234 if (!window->toysurface &&
1235 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1236 window->display->dpy) {
1237 window->toysurface =
1238 egl_window_surface_create(window->display,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001239 window->main_surface->surface,
1240 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001241 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001242 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001243
Pekka Paalanenb3627362012-11-19 17:16:00 +02001244 if (!window->toysurface)
Pekka Paalanen99436862012-11-19 17:15:59 +02001245 window->toysurface = shm_surface_create(window->display,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001246 window->main_surface->surface,
1247 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001248
Pekka Paalanenec076692012-11-30 13:37:27 +02001249 if (window->resizing)
1250 flags = SURFACE_HINT_RESIZE;
1251 else
1252 flags = 0;
1253
Pekka Paalanen99436862012-11-19 17:15:59 +02001254 window_get_resize_dx_dy(window, &dx, &dy);
1255 window->cairo_surface =
1256 window->toysurface->prepare(window->toysurface, dx, dy,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001257 allocation.width,
1258 allocation.height,
Pekka Paalanenec076692012-11-30 13:37:27 +02001259 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001260}
1261
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001262int
1263window_get_buffer_transform(struct window *window)
1264{
1265 return window->buffer_transform;
1266}
1267
1268void
1269window_set_buffer_transform(struct window *window,
1270 enum wl_output_transform transform)
1271{
1272 window->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001273 wl_surface_set_buffer_transform(window->main_surface->surface,
1274 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001275}
1276
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001277static void frame_destroy(struct frame *frame);
1278
Pekka Paalanen4e373742013-02-13 16:17:13 +02001279static void
1280surface_destroy(struct surface *surface)
1281{
1282 wl_surface_destroy(surface->surface);
1283 free(surface);
1284}
1285
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001286void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001287window_destroy(struct window *window)
1288{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001289 struct display *display = window->display;
1290 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001291 struct window_output *window_output;
1292 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001293
1294 if (window->redraw_scheduled)
1295 wl_list_remove(&window->redraw_task.link);
1296
1297 wl_list_for_each(input, &display->input_list, link) {
1298 if (input->pointer_focus == window)
1299 input->pointer_focus = NULL;
1300 if (input->keyboard_focus == window)
1301 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001302 if (input->focus_widget &&
1303 input->focus_widget->window == window)
1304 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001305 }
1306
Rob Bradford7507b572012-05-15 17:55:34 +01001307 wl_list_for_each_safe(window_output, window_output_tmp,
1308 &window->window_output_list, link) {
1309 free (window_output);
1310 }
1311
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001312 if (window->input_region)
1313 wl_region_destroy(window->input_region);
1314 if (window->opaque_region)
1315 wl_region_destroy(window->opaque_region);
1316
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001317 if (window->frame)
1318 frame_destroy(window->frame);
1319
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001320 if (window->shell_surface)
1321 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001322
1323 surface_destroy(window->main_surface);
1324
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001325 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001326
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001327 if (window->toysurface)
1328 window->toysurface->destroy(window->toysurface);
1329
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001330 if (window->frame_cb)
1331 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001332 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001333 free(window);
1334}
1335
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001336static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001337widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001339 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001340
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001341 wl_list_for_each(child, &widget->child_list, link) {
1342 target = widget_find_widget(child, x, y);
1343 if (target)
1344 return target;
1345 }
1346
1347 if (widget->allocation.x <= x &&
1348 x < widget->allocation.x + widget->allocation.width &&
1349 widget->allocation.y <= y &&
1350 y < widget->allocation.y + widget->allocation.height) {
1351 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001352 }
1353
1354 return NULL;
1355}
1356
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001357static struct widget *
1358widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001359{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001360 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001361
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001362 widget = malloc(sizeof *widget);
1363 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001364 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001365 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001366 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001367 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001368 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001369 widget->tooltip = NULL;
1370 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001371 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001372
1373 return widget;
1374}
1375
1376struct widget *
1377window_add_widget(struct window *window, void *data)
1378{
1379 window->widget = widget_create(window, data);
1380 wl_list_init(&window->widget->link);
1381
1382 return window->widget;
1383}
1384
1385struct widget *
1386widget_add_widget(struct widget *parent, void *data)
1387{
1388 struct widget *widget;
1389
1390 widget = widget_create(parent->window, data);
1391 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001392
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001393 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001394}
1395
1396void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001397widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001398{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001399 struct display *display = widget->window->display;
1400 struct input *input;
1401
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001402 if (widget->tooltip) {
1403 free(widget->tooltip);
1404 widget->tooltip = NULL;
1405 }
1406
Pekka Paalanene156fb62012-01-19 13:51:38 +02001407 wl_list_for_each(input, &display->input_list, link) {
1408 if (input->focus_widget == widget)
1409 input->focus_widget = NULL;
1410 }
1411
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001412 wl_list_remove(&widget->link);
1413 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001414}
1415
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001416void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001417widget_set_default_cursor(struct widget *widget, int cursor)
1418{
1419 widget->default_cursor = cursor;
1420}
1421
1422void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001423widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001425 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001426}
1427
1428void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001429widget_set_size(struct widget *widget, int32_t width, int32_t height)
1430{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001431 widget->allocation.width = width;
1432 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001433}
1434
1435void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001436widget_set_allocation(struct widget *widget,
1437 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001438{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001439 widget->allocation.x = x;
1440 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001441 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001442}
1443
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001444void
1445widget_set_transparent(struct widget *widget, int transparent)
1446{
1447 widget->opaque = !transparent;
1448}
1449
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001450void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001451widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001453 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001454}
1455
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001456void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001457widget_set_resize_handler(struct widget *widget,
1458 widget_resize_handler_t handler)
1459{
1460 widget->resize_handler = handler;
1461}
1462
1463void
1464widget_set_redraw_handler(struct widget *widget,
1465 widget_redraw_handler_t handler)
1466{
1467 widget->redraw_handler = handler;
1468}
1469
1470void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001471widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001472{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001473 widget->enter_handler = handler;
1474}
1475
1476void
1477widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1478{
1479 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001480}
1481
1482void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001483widget_set_motion_handler(struct widget *widget,
1484 widget_motion_handler_t handler)
1485{
1486 widget->motion_handler = handler;
1487}
1488
1489void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001490widget_set_button_handler(struct widget *widget,
1491 widget_button_handler_t handler)
1492{
1493 widget->button_handler = handler;
1494}
1495
1496void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001497widget_set_axis_handler(struct widget *widget,
1498 widget_axis_handler_t handler)
1499{
1500 widget->axis_handler = handler;
1501}
1502
1503void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001504widget_schedule_redraw(struct widget *widget)
1505{
1506 window_schedule_redraw(widget->window);
1507}
1508
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001509cairo_surface_t *
1510window_get_surface(struct window *window)
1511{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001512 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001513}
1514
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001515struct wl_surface *
1516window_get_wl_surface(struct window *window)
1517{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001519}
1520
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001521struct wl_shell_surface *
1522window_get_wl_shell_surface(struct window *window)
1523{
1524 return window->shell_surface;
1525}
1526
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001527static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001528tooltip_redraw_handler(struct widget *widget, void *data)
1529{
1530 cairo_t *cr;
1531 const int32_t r = 3;
1532 struct tooltip *tooltip = data;
1533 int32_t width, height;
1534 struct window *window = widget->window;
1535
1536 cr = cairo_create(window->cairo_surface);
1537 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1538 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1539 cairo_paint(cr);
1540
1541 width = window->allocation.width;
1542 height = window->allocation.height;
1543 rounded_rect(cr, 0, 0, width, height, r);
1544
1545 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1546 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1547 cairo_fill(cr);
1548
1549 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1550 cairo_move_to(cr, 10, 16);
1551 cairo_show_text(cr, tooltip->entry);
1552 cairo_destroy(cr);
1553}
1554
1555static cairo_text_extents_t
1556get_text_extents(struct tooltip *tooltip)
1557{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001558 cairo_t *cr;
1559 cairo_text_extents_t extents;
1560
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001561 /* Use the dummy_surface because tooltip's surface was not
1562 * created yet, and parent does not have a valid surface
1563 * outside repaint, either.
1564 */
1565 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001566 cairo_text_extents(cr, tooltip->entry, &extents);
1567 cairo_destroy(cr);
1568
1569 return extents;
1570}
1571
1572static int
1573window_create_tooltip(struct tooltip *tooltip)
1574{
1575 struct widget *parent = tooltip->parent;
1576 struct display *display = parent->window->display;
1577 struct window *window;
1578 const int offset_y = 27;
1579 const int margin = 3;
1580 cairo_text_extents_t extents;
1581
1582 if (tooltip->widget)
1583 return 0;
1584
1585 window = window_create_transient(display, parent->window, tooltip->x,
1586 tooltip->y + offset_y,
1587 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1588 if (!window)
1589 return -1;
1590
1591 tooltip->window = window;
1592 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1593
1594 extents = get_text_extents(tooltip);
1595 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1596 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1597
1598 return 0;
1599}
1600
1601void
1602widget_destroy_tooltip(struct widget *parent)
1603{
1604 struct tooltip *tooltip = parent->tooltip;
1605
1606 parent->tooltip_count = 0;
1607 if (!tooltip)
1608 return;
1609
1610 if (tooltip->widget) {
1611 widget_destroy(tooltip->widget);
1612 window_destroy(tooltip->window);
1613 tooltip->widget = NULL;
1614 tooltip->window = NULL;
1615 }
1616
1617 close(tooltip->tooltip_fd);
1618 free(tooltip->entry);
1619 free(tooltip);
1620 parent->tooltip = NULL;
1621}
1622
1623static void
1624tooltip_func(struct task *task, uint32_t events)
1625{
1626 struct tooltip *tooltip =
1627 container_of(task, struct tooltip, tooltip_task);
1628 uint64_t exp;
1629
Martin Olsson8df662a2012-07-08 03:03:47 +02001630 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1631 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001632 window_create_tooltip(tooltip);
1633}
1634
1635#define TOOLTIP_TIMEOUT 500
1636static int
1637tooltip_timer_reset(struct tooltip *tooltip)
1638{
1639 struct itimerspec its;
1640
1641 its.it_interval.tv_sec = 0;
1642 its.it_interval.tv_nsec = 0;
1643 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1644 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1645 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1646 fprintf(stderr, "could not set timerfd\n: %m");
1647 return -1;
1648 }
1649
1650 return 0;
1651}
1652
1653int
1654widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1655{
1656 struct tooltip *tooltip = parent->tooltip;
1657
1658 parent->tooltip_count++;
1659 if (tooltip) {
1660 tooltip->x = x;
1661 tooltip->y = y;
1662 tooltip_timer_reset(tooltip);
1663 return 0;
1664 }
1665
1666 /* the handler might be triggered too fast via input device motion, so
1667 * we need this check here to make sure tooltip is fully initialized */
1668 if (parent->tooltip_count > 1)
1669 return 0;
1670
1671 tooltip = malloc(sizeof *tooltip);
1672 if (!tooltip)
1673 return -1;
1674
1675 parent->tooltip = tooltip;
1676 tooltip->parent = parent;
1677 tooltip->widget = NULL;
1678 tooltip->window = NULL;
1679 tooltip->x = x;
1680 tooltip->y = y;
1681 tooltip->entry = strdup(entry);
1682 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1683 if (tooltip->tooltip_fd < 0) {
1684 fprintf(stderr, "could not create timerfd\n: %m");
1685 return -1;
1686 }
1687
1688 tooltip->tooltip_task.run = tooltip_func;
1689 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1690 EPOLLIN, &tooltip->tooltip_task);
1691 tooltip_timer_reset(tooltip);
1692
1693 return 0;
1694}
1695
1696static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001697workspace_manager_state(void *data,
1698 struct workspace_manager *workspace_manager,
1699 uint32_t current,
1700 uint32_t count)
1701{
1702 struct display *display = data;
1703
1704 display->workspace = current;
1705 display->workspace_count = count;
1706}
1707
1708static const struct workspace_manager_listener workspace_manager_listener = {
1709 workspace_manager_state
1710};
1711
1712static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001713frame_resize_handler(struct widget *widget,
1714 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001715{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716 struct frame *frame = data;
1717 struct widget *child = frame->child;
1718 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001719 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001720 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001721 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001722 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001723 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001724 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001725
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001726 switch (widget->window->type) {
1727 case TYPE_FULLSCREEN:
1728 decoration_width = 0;
1729 decoration_height = 0;
1730
1731 allocation.x = 0;
1732 allocation.y = 0;
1733 allocation.width = width;
1734 allocation.height = height;
1735 opaque_margin = 0;
1736
1737 wl_list_for_each(button, &frame->buttons_list, link)
1738 button->widget->opaque = 1;
1739 break;
1740 case TYPE_MAXIMIZED:
1741 decoration_width = t->width * 2;
1742 decoration_height = t->width + t->titlebar_height;
1743
1744 allocation.x = t->width;
1745 allocation.y = t->titlebar_height;
1746 allocation.width = width - decoration_width;
1747 allocation.height = height - decoration_height;
1748
1749 opaque_margin = 0;
1750
1751 wl_list_for_each(button, &frame->buttons_list, link)
1752 button->widget->opaque = 0;
1753 break;
1754 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001755 decoration_width = (t->width + t->margin) * 2;
1756 decoration_height = t->width +
1757 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001758
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001759 allocation.x = t->width + t->margin;
1760 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001761 allocation.width = width - decoration_width;
1762 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001763
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001764 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001765
1766 wl_list_for_each(button, &frame->buttons_list, link)
1767 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001768 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001769 }
1770
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001771 widget_set_allocation(child, allocation.x, allocation.y,
1772 allocation.width, allocation.height);
1773
1774 if (child->resize_handler)
1775 child->resize_handler(child,
1776 allocation.width,
1777 allocation.height,
1778 child->user_data);
1779
Scott Moreauf7e498c2012-05-14 11:39:29 -06001780 width = child->allocation.width + decoration_width;
1781 height = child->allocation.height + decoration_height;
1782
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001783 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1784
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001785 widget->window->input_region =
1786 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001787 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001788 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001789 shadow_margin, shadow_margin,
1790 width - 2 * shadow_margin,
1791 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001792 } else {
1793 wl_region_add(widget->window->input_region,
1794 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001795 }
1796
Scott Moreauf7e498c2012-05-14 11:39:29 -06001797 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001798
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001799 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001800 wl_region_add(widget->window->opaque_region,
1801 opaque_margin, opaque_margin,
1802 widget->allocation.width - 2 * opaque_margin,
1803 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001804
1805 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001806 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1807 x_l = t->width + shadow_margin;
1808 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001809 wl_list_for_each(button, &frame->buttons_list, link) {
1810 const int button_padding = 4;
1811 w = cairo_image_surface_get_width(button->icon);
1812 h = cairo_image_surface_get_height(button->icon);
1813
1814 if (button->decoration == FRAME_BUTTON_FANCY)
1815 w += 10;
1816
1817 if (button->align == FRAME_BUTTON_LEFT) {
1818 widget_set_allocation(button->widget,
1819 x_l, y , w + 1, h + 1);
1820 x_l += w;
1821 x_l += button_padding;
1822 } else {
1823 x_r -= w;
1824 widget_set_allocation(button->widget,
1825 x_r, y , w + 1, h + 1);
1826 x_r -= button_padding;
1827 }
1828 }
1829}
1830
1831static int
1832frame_button_enter_handler(struct widget *widget,
1833 struct input *input, float x, float y, void *data)
1834{
1835 struct frame_button *frame_button = data;
1836
1837 widget_schedule_redraw(frame_button->widget);
1838 frame_button->state = FRAME_BUTTON_OVER;
1839
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001840 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001841}
1842
1843static void
1844frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1845{
1846 struct frame_button *frame_button = data;
1847
1848 widget_schedule_redraw(frame_button->widget);
1849 frame_button->state = FRAME_BUTTON_DEFAULT;
1850}
1851
1852static void
1853frame_button_button_handler(struct widget *widget,
1854 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001855 uint32_t button,
1856 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001857{
1858 struct frame_button *frame_button = data;
1859 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001860 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001861
1862 if (button != BTN_LEFT)
1863 return;
1864
1865 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001866 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001867 frame_button->state = FRAME_BUTTON_ACTIVE;
1868 widget_schedule_redraw(frame_button->widget);
1869
1870 if (frame_button->type == FRAME_BUTTON_ICON)
1871 window_show_frame_menu(window, input, time);
1872 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001873 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001874 frame_button->state = FRAME_BUTTON_DEFAULT;
1875 widget_schedule_redraw(frame_button->widget);
1876 break;
1877 }
1878
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001879 if (!was_pressed)
1880 return;
1881
Martin Minarik1998b152012-05-10 02:04:35 +02001882 switch (frame_button->type) {
1883 case FRAME_BUTTON_CLOSE:
1884 if (window->close_handler)
1885 window->close_handler(window->parent,
1886 window->user_data);
1887 else
1888 display_exit(window->display);
1889 break;
1890 case FRAME_BUTTON_MINIMIZE:
1891 fprintf(stderr,"Minimize stub\n");
1892 break;
1893 case FRAME_BUTTON_MAXIMIZE:
1894 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1895 break;
1896 default:
1897 /* Unknown operation */
1898 break;
1899 }
1900}
1901
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001902static int
1903frame_button_motion_handler(struct widget *widget,
1904 struct input *input, uint32_t time,
1905 float x, float y, void *data)
1906{
1907 struct frame_button *frame_button = data;
1908 enum frame_button_pointer previous_button_state = frame_button->state;
1909
1910 /* only track state for a pressed button */
1911 if (input->grab != widget)
1912 return CURSOR_LEFT_PTR;
1913
1914 if (x > widget->allocation.x &&
1915 x < (widget->allocation.x + widget->allocation.width) &&
1916 y > widget->allocation.y &&
1917 y < (widget->allocation.y + widget->allocation.height)) {
1918 frame_button->state = FRAME_BUTTON_ACTIVE;
1919 } else {
1920 frame_button->state = FRAME_BUTTON_DEFAULT;
1921 }
1922
1923 if (frame_button->state != previous_button_state)
1924 widget_schedule_redraw(frame_button->widget);
1925
1926 return CURSOR_LEFT_PTR;
1927}
1928
Martin Minarik1998b152012-05-10 02:04:35 +02001929static void
1930frame_button_redraw_handler(struct widget *widget, void *data)
1931{
1932 struct frame_button *frame_button = data;
1933 cairo_t *cr;
1934 int width, height, x, y;
1935 struct window *window = widget->window;
1936
1937 x = widget->allocation.x;
1938 y = widget->allocation.y;
1939 width = widget->allocation.width;
1940 height = widget->allocation.height;
1941
1942 if (!width)
1943 return;
1944 if (!height)
1945 return;
1946 if (widget->opaque)
1947 return;
1948
1949 cr = cairo_create(window->cairo_surface);
1950
1951 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1952 cairo_set_line_width(cr, 1);
1953
1954 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1955 cairo_rectangle (cr, x, y, 25, 16);
1956
1957 cairo_stroke_preserve(cr);
1958
1959 switch (frame_button->state) {
1960 case FRAME_BUTTON_DEFAULT:
1961 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1962 break;
1963 case FRAME_BUTTON_OVER:
1964 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1965 break;
1966 case FRAME_BUTTON_ACTIVE:
1967 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1968 break;
1969 }
1970
1971 cairo_fill (cr);
1972
1973 x += 4;
1974 }
1975
1976 cairo_set_source_surface(cr, frame_button->icon, x, y);
1977 cairo_paint(cr);
1978
1979 cairo_destroy(cr);
1980}
1981
1982static struct widget *
1983frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1984 enum frame_button_align align, enum frame_button_decoration style)
1985{
1986 struct frame_button *frame_button;
1987 const char *icon = data;
1988
1989 frame_button = malloc (sizeof *frame_button);
1990 memset(frame_button, 0, sizeof *frame_button);
1991
1992 frame_button->icon = cairo_image_surface_create_from_png(icon);
1993 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1994 frame_button->frame = frame;
1995 frame_button->type = type;
1996 frame_button->align = align;
1997 frame_button->decoration = style;
1998
1999 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2000
2001 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2002 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2003 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2004 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002005 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002006 return frame_button->widget;
2007}
2008
2009static void
2010frame_button_destroy(struct frame_button *frame_button)
2011{
2012 widget_destroy(frame_button->widget);
2013 wl_list_remove(&frame_button->link);
2014 cairo_surface_destroy(frame_button->icon);
2015 free(frame_button);
2016
2017 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002018}
2019
2020static void
2021frame_redraw_handler(struct widget *widget, void *data)
2022{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002023 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002024 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002025 struct theme *t = window->display->theme;
2026 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002027
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002028 if (window->type == TYPE_FULLSCREEN)
2029 return;
2030
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002031 cr = cairo_create(window->cairo_surface);
2032
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002033 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002034 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002035 if (window->type == TYPE_MAXIMIZED)
2036 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002037 theme_render_frame(t, cr, widget->allocation.width,
2038 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002039
2040 cairo_destroy(cr);
2041}
2042
2043static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002045{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002046 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002047 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002048 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002049
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002050 location = theme_get_location(t, input->sx, input->sy,
2051 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002052 frame->widget->allocation.height,
2053 window->type == TYPE_MAXIMIZED ?
2054 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002055
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002056 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002057 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002058 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002059 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002060 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002061 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002062 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002063 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002064 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002065 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002066 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002067 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002068 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002069 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002070 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002071 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002072 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002073 case THEME_LOCATION_EXTERIOR:
2074 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002075 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002076 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002077 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002078}
2079
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002080static void
2081frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002082{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002083 struct display *display;
2084
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002085 switch (index) {
2086 case 0: /* close */
2087 if (window->close_handler)
2088 window->close_handler(window->parent,
2089 window->user_data);
2090 else
2091 display_exit(window->display);
2092 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002093 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002094 display = window->display;
2095 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002096 workspace_manager_move_surface(
2097 display->workspace_manager,
2098 window->main_surface->surface,
2099 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002100 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002101 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002102 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002103 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002104 workspace_manager_move_surface(
2105 display->workspace_manager,
2106 window->main_surface->surface,
2107 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002108 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002109 case 3: /* fullscreen */
2110 /* we don't have a way to get out of fullscreen for now */
2111 if (window->fullscreen_handler)
2112 window->fullscreen_handler(window, window->user_data);
2113 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002114 }
2115}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002116
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002117void
2118window_show_frame_menu(struct window *window,
2119 struct input *input, uint32_t time)
2120{
2121 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002122 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002123
2124 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002125 "Close",
2126 "Move to workspace above", "Move to workspace below",
2127 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002128 };
2129
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002130 if (window->fullscreen_handler)
2131 count = ARRAY_LENGTH(entries);
2132 else
2133 count = ARRAY_LENGTH(entries) - 1;
2134
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002135 input_get_position(input, &x, &y);
2136 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002137 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002138}
2139
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002140static int
2141frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002142 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002143{
2144 return frame_get_pointer_image_for_location(data, input);
2145}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002146
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147static int
2148frame_motion_handler(struct widget *widget,
2149 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002150 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151{
2152 return frame_get_pointer_image_for_location(data, input);
2153}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002154
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002155static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156frame_button_handler(struct widget *widget,
2157 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002158 uint32_t button, enum wl_pointer_button_state state,
2159 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160
2161{
2162 struct frame *frame = data;
2163 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002164 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002166
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002167 location = theme_get_location(display->theme, input->sx, input->sy,
2168 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002169 frame->widget->allocation.height,
2170 window->type == TYPE_MAXIMIZED ?
2171 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172
Daniel Stone4dbadb12012-05-30 16:31:51 +01002173 if (window->display->shell && button == BTN_LEFT &&
2174 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002176 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177 if (!window->shell_surface)
2178 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002179 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002180 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002181 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002182 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002183 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002184 case THEME_LOCATION_RESIZING_TOP:
2185 case THEME_LOCATION_RESIZING_BOTTOM:
2186 case THEME_LOCATION_RESIZING_LEFT:
2187 case THEME_LOCATION_RESIZING_RIGHT:
2188 case THEME_LOCATION_RESIZING_TOP_LEFT:
2189 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2190 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2191 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 if (!window->shell_surface)
2193 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002194 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002195
Pekka Paalanen99436862012-11-19 17:15:59 +02002196 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002198 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002199 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200 break;
2201 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002202 } else if (button == BTN_RIGHT &&
2203 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002204 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205 }
2206}
2207
2208struct widget *
2209frame_create(struct window *window, void *data)
2210{
2211 struct frame *frame;
2212
2213 frame = malloc(sizeof *frame);
2214 memset(frame, 0, sizeof *frame);
2215
2216 frame->widget = window_add_widget(window, frame);
2217 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002218
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2220 widget_set_resize_handler(frame->widget, frame_resize_handler);
2221 widget_set_enter_handler(frame->widget, frame_enter_handler);
2222 widget_set_motion_handler(frame->widget, frame_motion_handler);
2223 widget_set_button_handler(frame->widget, frame_button_handler);
2224
Martin Minarik1998b152012-05-10 02:04:35 +02002225 /* Create empty list for frame buttons */
2226 wl_list_init(&frame->buttons_list);
2227
2228 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2229 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2230
2231 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2232 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2233
2234 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2235 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2236
2237 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2238 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2239
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002240 window->frame = frame;
2241
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002242 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243}
2244
Kristian Høgsberga1627922012-06-20 17:30:03 -04002245void
2246frame_set_child_size(struct widget *widget, int child_width, int child_height)
2247{
2248 struct display *display = widget->window->display;
2249 struct theme *t = display->theme;
2250 int decoration_width, decoration_height;
2251 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002252 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002253
2254 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002255 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002256 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002257 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002258
2259 width = child_width + decoration_width;
2260 height = child_height + decoration_height;
2261 } else {
2262 width = child_width;
2263 height = child_height;
2264 }
2265
2266 window_schedule_resize(widget->window, width, height);
2267}
2268
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002269static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002270frame_destroy(struct frame *frame)
2271{
Martin Minarik1998b152012-05-10 02:04:35 +02002272 struct frame_button *button, *tmp;
2273
2274 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2275 frame_button_destroy(button);
2276
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002277 /* frame->child must be destroyed by the application */
2278 widget_destroy(frame->widget);
2279 free(frame);
2280}
2281
2282static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002283input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002284 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002285{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002286 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002287 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002288
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002289 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002290 return;
2291
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002292 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002293 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002294 widget = old;
2295 if (input->grab)
2296 widget = input->grab;
2297 if (widget->leave_handler)
2298 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002299 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002300 }
2301
2302 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002303 widget = focus;
2304 if (input->grab)
2305 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002306 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002307 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002308 cursor = widget->enter_handler(focus, input, x, y,
2309 widget->user_data);
2310 else
2311 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002312
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002313 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002314 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002315}
2316
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002317void
2318input_grab(struct input *input, struct widget *widget, uint32_t button)
2319{
2320 input->grab = widget;
2321 input->grab_button = button;
2322}
2323
2324void
2325input_ungrab(struct input *input)
2326{
2327 struct widget *widget;
2328
2329 input->grab = NULL;
2330 if (input->pointer_focus) {
2331 widget = widget_find_widget(input->pointer_focus->widget,
2332 input->sx, input->sy);
2333 input_set_focus_widget(input, widget, input->sx, input->sy);
2334 }
2335}
2336
2337static void
2338input_remove_pointer_focus(struct input *input)
2339{
2340 struct window *window = input->pointer_focus;
2341
2342 if (!window)
2343 return;
2344
2345 input_set_focus_widget(input, NULL, 0, 0);
2346
2347 input->pointer_focus = NULL;
2348 input->current_cursor = CURSOR_UNSET;
2349}
2350
2351static void
2352pointer_handle_enter(void *data, struct wl_pointer *pointer,
2353 uint32_t serial, struct wl_surface *surface,
2354 wl_fixed_t sx_w, wl_fixed_t sy_w)
2355{
2356 struct input *input = data;
2357 struct window *window;
2358 struct widget *widget;
2359 float sx = wl_fixed_to_double(sx_w);
2360 float sy = wl_fixed_to_double(sy_w);
2361
2362 if (!surface) {
2363 /* enter event for a window we've just destroyed */
2364 return;
2365 }
2366
2367 input->display->serial = serial;
2368 input->pointer_enter_serial = serial;
2369 input->pointer_focus = wl_surface_get_user_data(surface);
2370 window = input->pointer_focus;
2371
Pekka Paalanen99436862012-11-19 17:15:59 +02002372 if (window->resizing) {
2373 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002374 /* Schedule a redraw to free the pool */
2375 window_schedule_redraw(window);
2376 }
2377
2378 input->sx = sx;
2379 input->sy = sy;
2380
2381 widget = widget_find_widget(window->widget, sx, sy);
2382 input_set_focus_widget(input, widget, sx, sy);
2383}
2384
2385static void
2386pointer_handle_leave(void *data, struct wl_pointer *pointer,
2387 uint32_t serial, struct wl_surface *surface)
2388{
2389 struct input *input = data;
2390
2391 input->display->serial = serial;
2392 input_remove_pointer_focus(input);
2393}
2394
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002395static void
Daniel Stone37816df2012-05-16 18:45:18 +01002396pointer_handle_motion(void *data, struct wl_pointer *pointer,
2397 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002398{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002399 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002400 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002401 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002402 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002403 float sx = wl_fixed_to_double(sx_w);
2404 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002405
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002406 input->sx = sx;
2407 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002408
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002409 if (!window)
2410 return;
2411
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002412 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002413 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002414 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002415 }
2416
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002417 if (input->grab)
2418 widget = input->grab;
2419 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002420 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002421 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002422 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002423 input, time, sx, sy,
2424 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002425 else
2426 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002427
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002428 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002429}
2430
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002431static void
Daniel Stone37816df2012-05-16 18:45:18 +01002432pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002433 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002434{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002435 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002436 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002437 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002438
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002439 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002440 if (input->focus_widget && input->grab == NULL &&
2441 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002442 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002443
Neil Roberts6b28aad2012-01-23 19:11:18 +00002444 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002445 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002446 (*widget->button_handler)(widget,
2447 input, time,
2448 button, state,
2449 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002450
Daniel Stone4dbadb12012-05-30 16:31:51 +01002451 if (input->grab && input->grab_button == button &&
2452 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002453 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002454}
2455
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002456static void
Daniel Stone37816df2012-05-16 18:45:18 +01002457pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002458 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002459{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002460 struct input *input = data;
2461 struct widget *widget;
2462
2463 widget = input->focus_widget;
2464 if (input->grab)
2465 widget = input->grab;
2466 if (widget && widget->axis_handler)
2467 (*widget->axis_handler)(widget,
2468 input, time,
2469 axis, value,
2470 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002471}
2472
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002473static const struct wl_pointer_listener pointer_listener = {
2474 pointer_handle_enter,
2475 pointer_handle_leave,
2476 pointer_handle_motion,
2477 pointer_handle_button,
2478 pointer_handle_axis,
2479};
2480
2481static void
2482input_remove_keyboard_focus(struct input *input)
2483{
2484 struct window *window = input->keyboard_focus;
2485 struct itimerspec its;
2486
2487 its.it_interval.tv_sec = 0;
2488 its.it_interval.tv_nsec = 0;
2489 its.it_value.tv_sec = 0;
2490 its.it_value.tv_nsec = 0;
2491 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2492
2493 if (!window)
2494 return;
2495
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002496 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002497 if (window->keyboard_focus_handler)
2498 (*window->keyboard_focus_handler)(window, NULL,
2499 window->user_data);
2500
2501 input->keyboard_focus = NULL;
2502}
2503
2504static void
2505keyboard_repeat_func(struct task *task, uint32_t events)
2506{
2507 struct input *input =
2508 container_of(task, struct input, repeat_task);
2509 struct window *window = input->keyboard_focus;
2510 uint64_t exp;
2511
2512 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2513 /* If we change the timer between the fd becoming
2514 * readable and getting here, there'll be nothing to
2515 * read and we get EAGAIN. */
2516 return;
2517
2518 if (window && window->key_handler) {
2519 (*window->key_handler)(window, input, input->repeat_time,
2520 input->repeat_key, input->repeat_sym,
2521 WL_KEYBOARD_KEY_STATE_PRESSED,
2522 window->user_data);
2523 }
2524}
2525
2526static void
2527keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2528 uint32_t format, int fd, uint32_t size)
2529{
2530 struct input *input = data;
2531 char *map_str;
2532
2533 if (!data) {
2534 close(fd);
2535 return;
2536 }
2537
2538 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2539 close(fd);
2540 return;
2541 }
2542
2543 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2544 if (map_str == MAP_FAILED) {
2545 close(fd);
2546 return;
2547 }
2548
2549 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2550 map_str,
2551 XKB_KEYMAP_FORMAT_TEXT_V1,
2552 0);
2553 munmap(map_str, size);
2554 close(fd);
2555
2556 if (!input->xkb.keymap) {
2557 fprintf(stderr, "failed to compile keymap\n");
2558 return;
2559 }
2560
2561 input->xkb.state = xkb_state_new(input->xkb.keymap);
2562 if (!input->xkb.state) {
2563 fprintf(stderr, "failed to create XKB state\n");
2564 xkb_map_unref(input->xkb.keymap);
2565 input->xkb.keymap = NULL;
2566 return;
2567 }
2568
2569 input->xkb.control_mask =
2570 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2571 input->xkb.alt_mask =
2572 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2573 input->xkb.shift_mask =
2574 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2575}
2576
2577static void
2578keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2579 uint32_t serial, struct wl_surface *surface,
2580 struct wl_array *keys)
2581{
2582 struct input *input = data;
2583 struct window *window;
2584
2585 input->display->serial = serial;
2586 input->keyboard_focus = wl_surface_get_user_data(surface);
2587
2588 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002589 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002590 if (window->keyboard_focus_handler)
2591 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002592 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002593}
2594
2595static void
2596keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2597 uint32_t serial, struct wl_surface *surface)
2598{
2599 struct input *input = data;
2600
2601 input->display->serial = serial;
2602 input_remove_keyboard_focus(input);
2603}
2604
Scott Moreau210d0792012-03-22 10:47:01 -06002605static void
Daniel Stone37816df2012-05-16 18:45:18 +01002606keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002607 uint32_t serial, uint32_t time, uint32_t key,
2608 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002609{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002610 struct input *input = data;
2611 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002612 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002613 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002614 const xkb_keysym_t *syms;
2615 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002616 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002617
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002618 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002619 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002620 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002621 return;
2622
Daniel Stone97f68542012-05-30 16:32:01 +01002623 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002624
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002625 sym = XKB_KEY_NoSymbol;
2626 if (num_syms == 1)
2627 sym = syms[0];
2628
2629 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002630 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002631 window_set_maximized(window,
2632 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002633 } else if (sym == XKB_KEY_F11 &&
2634 window->fullscreen_handler &&
2635 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2636 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002637 } else if (sym == XKB_KEY_F4 &&
2638 input->modifiers == MOD_ALT_MASK &&
2639 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2640 if (window->close_handler)
2641 window->close_handler(window->parent,
2642 window->user_data);
2643 else
2644 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002645 } else if (window->key_handler) {
2646 (*window->key_handler)(window, input, time, key,
2647 sym, state, window->user_data);
2648 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002649
2650 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2651 key == input->repeat_key) {
2652 its.it_interval.tv_sec = 0;
2653 its.it_interval.tv_nsec = 0;
2654 its.it_value.tv_sec = 0;
2655 its.it_value.tv_nsec = 0;
2656 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2657 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2658 input->repeat_sym = sym;
2659 input->repeat_key = key;
2660 input->repeat_time = time;
2661 its.it_interval.tv_sec = 0;
2662 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2663 its.it_value.tv_sec = 0;
2664 its.it_value.tv_nsec = 400 * 1000 * 1000;
2665 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2666 }
2667}
2668
2669static void
Daniel Stone351eb612012-05-31 15:27:47 -04002670keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2671 uint32_t serial, uint32_t mods_depressed,
2672 uint32_t mods_latched, uint32_t mods_locked,
2673 uint32_t group)
2674{
2675 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002676 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002677
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002678 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2679 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002680 mask = xkb_state_serialize_mods(input->xkb.state,
2681 XKB_STATE_DEPRESSED |
2682 XKB_STATE_LATCHED);
2683 input->modifiers = 0;
2684 if (mask & input->xkb.control_mask)
2685 input->modifiers |= MOD_CONTROL_MASK;
2686 if (mask & input->xkb.alt_mask)
2687 input->modifiers |= MOD_ALT_MASK;
2688 if (mask & input->xkb.shift_mask)
2689 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002690}
2691
Daniel Stone37816df2012-05-16 18:45:18 +01002692static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002693 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002694 keyboard_handle_enter,
2695 keyboard_handle_leave,
2696 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002697 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002698};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002699
2700static void
Daniel Stone37816df2012-05-16 18:45:18 +01002701seat_handle_capabilities(void *data, struct wl_seat *seat,
2702 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002703{
Daniel Stone37816df2012-05-16 18:45:18 +01002704 struct input *input = data;
2705
2706 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2707 input->pointer = wl_seat_get_pointer(seat);
2708 wl_pointer_set_user_data(input->pointer, input);
2709 wl_pointer_add_listener(input->pointer, &pointer_listener,
2710 input);
2711 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2712 wl_pointer_destroy(input->pointer);
2713 input->pointer = NULL;
2714 }
2715
2716 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2717 input->keyboard = wl_seat_get_keyboard(seat);
2718 wl_keyboard_set_user_data(input->keyboard, input);
2719 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2720 input);
2721 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2722 wl_keyboard_destroy(input->keyboard);
2723 input->keyboard = NULL;
2724 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002725}
2726
Daniel Stone37816df2012-05-16 18:45:18 +01002727static const struct wl_seat_listener seat_listener = {
2728 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002729};
2730
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002731void
2732input_get_position(struct input *input, int32_t *x, int32_t *y)
2733{
2734 *x = input->sx;
2735 *y = input->sy;
2736}
2737
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002738struct display *
2739input_get_display(struct input *input)
2740{
2741 return input->display;
2742}
2743
Daniel Stone37816df2012-05-16 18:45:18 +01002744struct wl_seat *
2745input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002746{
Daniel Stone37816df2012-05-16 18:45:18 +01002747 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002748}
2749
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002750uint32_t
2751input_get_modifiers(struct input *input)
2752{
2753 return input->modifiers;
2754}
2755
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002756struct widget *
2757input_get_focus_widget(struct input *input)
2758{
2759 return input->focus_widget;
2760}
2761
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002762struct data_offer {
2763 struct wl_data_offer *offer;
2764 struct input *input;
2765 struct wl_array types;
2766 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002767
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002768 struct task io_task;
2769 int fd;
2770 data_func_t func;
2771 int32_t x, y;
2772 void *user_data;
2773};
2774
2775static void
2776data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2777{
2778 struct data_offer *offer = data;
2779 char **p;
2780
2781 p = wl_array_add(&offer->types, sizeof *p);
2782 *p = strdup(type);
2783}
2784
2785static const struct wl_data_offer_listener data_offer_listener = {
2786 data_offer_offer,
2787};
2788
2789static void
2790data_offer_destroy(struct data_offer *offer)
2791{
2792 char **p;
2793
2794 offer->refcount--;
2795 if (offer->refcount == 0) {
2796 wl_data_offer_destroy(offer->offer);
2797 for (p = offer->types.data; *p; p++)
2798 free(*p);
2799 wl_array_release(&offer->types);
2800 free(offer);
2801 }
2802}
2803
2804static void
2805data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002806 struct wl_data_device *data_device,
2807 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002808{
2809 struct data_offer *offer;
2810
2811 offer = malloc(sizeof *offer);
2812
2813 wl_array_init(&offer->types);
2814 offer->refcount = 1;
2815 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002816 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002817 wl_data_offer_add_listener(offer->offer,
2818 &data_offer_listener, offer);
2819}
2820
2821static void
2822data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002823 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002824 wl_fixed_t x_w, wl_fixed_t y_w,
2825 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002826{
2827 struct input *input = data;
2828 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002829 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002830 float x = wl_fixed_to_double(x_w);
2831 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002832 char **p;
2833
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002834 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002835 window = wl_surface_get_user_data(surface);
2836 input->pointer_focus = window;
2837
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002838 if (offer) {
2839 input->drag_offer = wl_data_offer_get_user_data(offer);
2840
2841 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2842 *p = NULL;
2843
2844 types_data = input->drag_offer->types.data;
2845 } else {
2846 input->drag_offer = NULL;
2847 types_data = NULL;
2848 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002849
2850 window = input->pointer_focus;
2851 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002852 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002853 window->user_data);
2854}
2855
2856static void
2857data_device_leave(void *data, struct wl_data_device *data_device)
2858{
2859 struct input *input = data;
2860
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002861 if (input->drag_offer) {
2862 data_offer_destroy(input->drag_offer);
2863 input->drag_offer = NULL;
2864 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002865}
2866
2867static void
2868data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002869 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002870{
2871 struct input *input = data;
2872 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002873 float x = wl_fixed_to_double(x_w);
2874 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002875 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002876
2877 input->sx = x;
2878 input->sy = y;
2879
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002880 if (input->drag_offer)
2881 types_data = input->drag_offer->types.data;
2882 else
2883 types_data = NULL;
2884
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002885 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002886 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002887 window->user_data);
2888}
2889
2890static void
2891data_device_drop(void *data, struct wl_data_device *data_device)
2892{
2893 struct input *input = data;
2894 struct window *window = input->pointer_focus;
2895
2896 if (window->drop_handler)
2897 window->drop_handler(window, input,
2898 input->sx, input->sy, window->user_data);
2899}
2900
2901static void
2902data_device_selection(void *data,
2903 struct wl_data_device *wl_data_device,
2904 struct wl_data_offer *offer)
2905{
2906 struct input *input = data;
2907 char **p;
2908
2909 if (input->selection_offer)
2910 data_offer_destroy(input->selection_offer);
2911
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002912 if (offer) {
2913 input->selection_offer = wl_data_offer_get_user_data(offer);
2914 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2915 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002916 } else {
2917 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002918 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002919}
2920
2921static const struct wl_data_device_listener data_device_listener = {
2922 data_device_data_offer,
2923 data_device_enter,
2924 data_device_leave,
2925 data_device_motion,
2926 data_device_drop,
2927 data_device_selection
2928};
2929
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002930static void
2931input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002932{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002933 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002934 struct wl_cursor *cursor;
2935 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002936
Daniel Stone80972742012-11-07 17:51:39 +11002937 if (!input->pointer)
2938 return;
2939
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002940 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002941 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002942 return;
2943
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002944 if (index >= (int) cursor->image_count) {
2945 fprintf(stderr, "cursor index out of range\n");
2946 return;
2947 }
2948
2949 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002950 buffer = wl_cursor_image_get_buffer(image);
2951 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002952 return;
2953
Kristian Høgsbergae277372012-08-01 09:41:08 -04002954 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002955 input->pointer_surface,
2956 image->hotspot_x, image->hotspot_y);
2957 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2958 wl_surface_damage(input->pointer_surface, 0, 0,
2959 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002960 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002961}
2962
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002963static const struct wl_callback_listener pointer_surface_listener;
2964
2965static void
2966pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2967 uint32_t time)
2968{
2969 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002970 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002971 int i;
2972
2973 if (callback) {
2974 assert(callback == input->cursor_frame_cb);
2975 wl_callback_destroy(callback);
2976 input->cursor_frame_cb = NULL;
2977 }
2978
Daniel Stone80972742012-11-07 17:51:39 +11002979 if (!input->pointer)
2980 return;
2981
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002982 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002983 wl_pointer_set_cursor(input->pointer,
2984 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002985 NULL, 0, 0);
2986 return;
2987 }
2988
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002989 if (input->current_cursor == CURSOR_UNSET)
2990 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002991 cursor = input->display->cursors[input->current_cursor];
2992 if (!cursor)
2993 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002994
2995 /* FIXME We don't have the current time on the first call so we set
2996 * the animation start to the time of the first frame callback. */
2997 if (time == 0)
2998 input->cursor_anim_start = 0;
2999 else if (input->cursor_anim_start == 0)
3000 input->cursor_anim_start = time;
3001
3002 if (time == 0 || input->cursor_anim_start == 0)
3003 i = 0;
3004 else
3005 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3006
Pekka Paalanenbc106382012-10-10 12:49:31 +03003007 if (cursor->image_count > 1) {
3008 input->cursor_frame_cb =
3009 wl_surface_frame(input->pointer_surface);
3010 wl_callback_add_listener(input->cursor_frame_cb,
3011 &pointer_surface_listener, input);
3012 }
3013
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003014 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003015}
3016
3017static const struct wl_callback_listener pointer_surface_listener = {
3018 pointer_surface_frame_callback
3019};
3020
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003021void
3022input_set_pointer_image(struct input *input, int pointer)
3023{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003024 int force = 0;
3025
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003026 if (!input->pointer)
3027 return;
3028
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003029 if (input->pointer_enter_serial > input->cursor_serial)
3030 force = 1;
3031
3032 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003033 return;
3034
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003035 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003036 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003037 if (!input->cursor_frame_cb)
3038 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003039 else if (force) {
3040 /* The current frame callback may be stuck if, for instance,
3041 * the set cursor request was processed by the server after
3042 * this client lost the focus. In this case the cursor surface
3043 * might not be mapped and the frame callback wouldn't ever
3044 * complete. Send a set_cursor and attach to try to map the
3045 * cursor surface again so that the callback will finish */
3046 input_set_pointer_image_index(input, 0);
3047 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003048}
3049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003050struct wl_data_device *
3051input_get_data_device(struct input *input)
3052{
3053 return input->data_device;
3054}
3055
3056void
3057input_set_selection(struct input *input,
3058 struct wl_data_source *source, uint32_t time)
3059{
3060 wl_data_device_set_selection(input->data_device, source, time);
3061}
3062
3063void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003064input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003065{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003066 wl_data_offer_accept(input->drag_offer->offer,
3067 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003068}
3069
3070static void
3071offer_io_func(struct task *task, uint32_t events)
3072{
3073 struct data_offer *offer =
3074 container_of(task, struct data_offer, io_task);
3075 unsigned int len;
3076 char buffer[4096];
3077
3078 len = read(offer->fd, buffer, sizeof buffer);
3079 offer->func(buffer, len,
3080 offer->x, offer->y, offer->user_data);
3081
3082 if (len == 0) {
3083 close(offer->fd);
3084 data_offer_destroy(offer);
3085 }
3086}
3087
3088static void
3089data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3090 data_func_t func, void *user_data)
3091{
3092 int p[2];
3093
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003094 if (pipe2(p, O_CLOEXEC) == -1)
3095 return;
3096
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003097 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3098 close(p[1]);
3099
3100 offer->io_task.run = offer_io_func;
3101 offer->fd = p[0];
3102 offer->func = func;
3103 offer->refcount++;
3104 offer->user_data = user_data;
3105
3106 display_watch_fd(offer->input->display,
3107 offer->fd, EPOLLIN, &offer->io_task);
3108}
3109
3110void
3111input_receive_drag_data(struct input *input, const char *mime_type,
3112 data_func_t func, void *data)
3113{
3114 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3115 input->drag_offer->x = input->sx;
3116 input->drag_offer->y = input->sy;
3117}
3118
3119int
3120input_receive_selection_data(struct input *input, const char *mime_type,
3121 data_func_t func, void *data)
3122{
3123 char **p;
3124
3125 if (input->selection_offer == NULL)
3126 return -1;
3127
3128 for (p = input->selection_offer->types.data; *p; p++)
3129 if (strcmp(mime_type, *p) == 0)
3130 break;
3131
3132 if (*p == NULL)
3133 return -1;
3134
3135 data_offer_receive_data(input->selection_offer,
3136 mime_type, func, data);
3137 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003138}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003139
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003140int
3141input_receive_selection_data_to_fd(struct input *input,
3142 const char *mime_type, int fd)
3143{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003144 if (input->selection_offer)
3145 wl_data_offer_receive(input->selection_offer->offer,
3146 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003147
3148 return 0;
3149}
3150
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003151void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003152window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003153{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003154 if (!window->shell_surface)
3155 return;
3156
Daniel Stone37816df2012-05-16 18:45:18 +01003157 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003158}
3159
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003160static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003161idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003162{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003163 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003164 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003165
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003166 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003167 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003168 widget_set_allocation(widget,
3169 window->pending_allocation.x,
3170 window->pending_allocation.y,
3171 window->pending_allocation.width,
3172 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003173
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003174 if (window->input_region) {
3175 wl_region_destroy(window->input_region);
3176 window->input_region = NULL;
3177 }
3178
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003179 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003180 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003181
3182 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003183
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003184 if (widget->resize_handler)
3185 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003186 widget->allocation.width,
3187 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003188 widget->user_data);
3189
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05003190 if (window->allocation.width != widget->allocation.width ||
3191 window->allocation.height != widget->allocation.height) {
3192 window->allocation = widget->allocation;
3193 window_schedule_redraw(window);
3194 }
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003195
3196 if (widget->opaque)
3197 wl_region_add(window->opaque_region, 0, 0,
3198 widget->allocation.width,
3199 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003200}
3201
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003202void
3203window_schedule_resize(struct window *window, int width, int height)
3204{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003205 window->pending_allocation.x = 0;
3206 window->pending_allocation.y = 0;
3207 window->pending_allocation.width = width;
3208 window->pending_allocation.height = height;
3209
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003210 if (window->min_allocation.width == 0)
3211 window->min_allocation = window->pending_allocation;
3212 if (window->pending_allocation.width < window->min_allocation.width)
3213 window->pending_allocation.width = window->min_allocation.width;
3214 if (window->pending_allocation.height < window->min_allocation.height)
3215 window->pending_allocation.height = window->min_allocation.height;
3216
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003217 window->resize_needed = 1;
3218 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003219}
3220
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003221void
3222widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3223{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003224 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003225}
3226
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003227static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003228handle_ping(void *data, struct wl_shell_surface *shell_surface,
3229 uint32_t serial)
3230{
3231 wl_shell_surface_pong(shell_surface, serial);
3232}
3233
3234static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003235handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003236 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003237{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003238 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003239
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003240 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003241 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003242}
3243
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003244static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003245menu_destroy(struct menu *menu)
3246{
3247 widget_destroy(menu->widget);
3248 window_destroy(menu->window);
3249 free(menu);
3250}
3251
3252static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003253handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3254{
3255 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003256 struct menu *menu = window->widget->user_data;
3257
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003258 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003259 * device. Or just use wl_callback. And this really needs to
3260 * be a window vfunc that the menu can set. And we need the
3261 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003262
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003263 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003264 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003265 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003266}
3267
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003268static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003269 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003270 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003271 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003272};
3273
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003274void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003275window_get_allocation(struct window *window,
3276 struct rectangle *allocation)
3277{
3278 *allocation = window->allocation;
3279}
3280
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003281static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003282widget_redraw(struct widget *widget)
3283{
3284 struct widget *child;
3285
3286 if (widget->redraw_handler)
3287 widget->redraw_handler(widget, widget->user_data);
3288 wl_list_for_each(child, &widget->child_list, link)
3289 widget_redraw(child);
3290}
3291
3292static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003293frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3294{
3295 struct window *window = data;
3296
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003297 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003298 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003299 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003300 window->redraw_scheduled = 0;
3301 if (window->redraw_needed)
3302 window_schedule_redraw(window);
3303}
3304
3305static const struct wl_callback_listener listener = {
3306 frame_callback
3307};
3308
3309static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003310idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003311{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003312 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003313
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003314 if (window->resize_needed)
3315 idle_resize(window);
3316
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003317 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003318 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003319 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003320 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003321
Pekka Paalanen4e373742013-02-13 16:17:13 +02003322 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003323 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003324 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003325}
3326
3327void
3328window_schedule_redraw(struct window *window)
3329{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003330 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003331 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003332 window->redraw_task.run = idle_redraw;
3333 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003334 window->redraw_scheduled = 1;
3335 }
3336}
3337
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003338int
3339window_is_fullscreen(struct window *window)
3340{
3341 return window->type == TYPE_FULLSCREEN;
3342}
3343
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003344void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003345window_set_fullscreen(struct window *window, int fullscreen)
3346{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003347 if (!window->display->shell)
3348 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003349
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003350 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003351 return;
3352
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003353 if (fullscreen) {
3354 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003355 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003356 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003357 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003358 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003359 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003360 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003361 wl_shell_surface_set_toplevel(window->shell_surface);
3362 window_schedule_resize(window,
3363 window->saved_allocation.width,
3364 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003365 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003366}
3367
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003368void
3369window_set_fullscreen_method(struct window *window,
3370 enum wl_shell_surface_fullscreen_method method)
3371{
3372 window->fullscreen_method = method;
3373}
3374
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003375int
3376window_is_maximized(struct window *window)
3377{
3378 return window->type == TYPE_MAXIMIZED;
3379}
3380
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003381void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003382window_set_maximized(struct window *window, int maximized)
3383{
3384 if (!window->display->shell)
3385 return;
3386
3387 if ((window->type == TYPE_MAXIMIZED) == maximized)
3388 return;
3389
3390 if (window->type == TYPE_TOPLEVEL) {
3391 window->saved_allocation = window->allocation;
3392 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3393 window->type = TYPE_MAXIMIZED;
3394 } else {
3395 wl_shell_surface_set_toplevel(window->shell_surface);
3396 window->type = TYPE_TOPLEVEL;
3397 window_schedule_resize(window,
3398 window->saved_allocation.width,
3399 window->saved_allocation.height);
3400 }
3401}
3402
3403void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003404window_set_user_data(struct window *window, void *data)
3405{
3406 window->user_data = data;
3407}
3408
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003409void *
3410window_get_user_data(struct window *window)
3411{
3412 return window->user_data;
3413}
3414
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003415void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003416window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003417 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003418{
3419 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003420}
3421
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003422void
3423window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003424 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003425{
3426 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003427}
3428
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003429void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003430window_set_data_handler(struct window *window, window_data_handler_t handler)
3431{
3432 window->data_handler = handler;
3433}
3434
3435void
3436window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3437{
3438 window->drop_handler = handler;
3439}
3440
3441void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003442window_set_close_handler(struct window *window,
3443 window_close_handler_t handler)
3444{
3445 window->close_handler = handler;
3446}
3447
3448void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003449window_set_fullscreen_handler(struct window *window,
3450 window_fullscreen_handler_t handler)
3451{
3452 window->fullscreen_handler = handler;
3453}
3454
3455void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003456window_set_output_handler(struct window *window,
3457 window_output_handler_t handler)
3458{
3459 window->output_handler = handler;
3460}
3461
3462void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003463window_set_title(struct window *window, const char *title)
3464{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003465 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003466 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003467 if (window->shell_surface)
3468 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003469}
3470
3471const char *
3472window_get_title(struct window *window)
3473{
3474 return window->title;
3475}
3476
3477void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003478window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3479{
3480 struct text_cursor_position *text_cursor_position =
3481 window->display->text_cursor_position;
3482
Scott Moreau9295ce02012-06-01 12:46:10 -06003483 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003484 return;
3485
3486 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003487 window->main_surface->surface,
3488 wl_fixed_from_int(x),
3489 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003490}
3491
3492void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003493window_damage(struct window *window, int32_t x, int32_t y,
3494 int32_t width, int32_t height)
3495{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003496 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003497}
3498
Casey Dahlin9074db52012-04-19 22:50:09 -04003499static void
3500surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003501 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003502{
Rob Bradford7507b572012-05-15 17:55:34 +01003503 struct window *window = data;
3504 struct output *output;
3505 struct output *output_found = NULL;
3506 struct window_output *window_output;
3507
3508 wl_list_for_each(output, &window->display->output_list, link) {
3509 if (output->output == wl_output) {
3510 output_found = output;
3511 break;
3512 }
3513 }
3514
3515 if (!output_found)
3516 return;
3517
3518 window_output = malloc (sizeof *window_output);
3519 window_output->output = output_found;
3520
3521 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003522
3523 if (window->output_handler)
3524 window->output_handler(window, output_found, 1,
3525 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003526}
3527
3528static void
3529surface_leave(void *data,
3530 struct wl_surface *wl_surface, struct wl_output *output)
3531{
Rob Bradford7507b572012-05-15 17:55:34 +01003532 struct window *window = data;
3533 struct window_output *window_output;
3534 struct window_output *window_output_found = NULL;
3535
3536 wl_list_for_each(window_output, &window->window_output_list, link) {
3537 if (window_output->output->output == output) {
3538 window_output_found = window_output;
3539 break;
3540 }
3541 }
3542
3543 if (window_output_found) {
3544 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003545
3546 if (window->output_handler)
3547 window->output_handler(window, window_output->output,
3548 0, window->user_data);
3549
Rob Bradford7507b572012-05-15 17:55:34 +01003550 free(window_output_found);
3551 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003552}
3553
3554static const struct wl_surface_listener surface_listener = {
3555 surface_enter,
3556 surface_leave
3557};
3558
Pekka Paalanen4e373742013-02-13 16:17:13 +02003559static struct surface *
3560surface_create(struct window *window)
3561{
3562 struct display *display = window->display;
3563 struct surface *surface;
3564
3565 surface = calloc(1, sizeof *surface);
3566 if (!surface)
3567 return NULL;
3568
3569 surface->window = window;
3570 surface->surface = wl_compositor_create_surface(display->compositor);
3571 wl_surface_add_listener(surface->surface, &surface_listener, window);
3572
3573 return surface;
3574}
3575
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003576static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003577window_create_internal(struct display *display,
3578 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003579{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003580 struct window *window;
3581
3582 window = malloc(sizeof *window);
3583 if (window == NULL)
3584 return NULL;
3585
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003586 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003587 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003588 window->parent = parent;
Pekka Paalanen4e373742013-02-13 16:17:13 +02003589 window->main_surface = surface_create(window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003590 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003591 window->shell_surface =
3592 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003593 window->main_surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003594 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003595 window->allocation.x = 0;
3596 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003597 window->allocation.width = 0;
3598 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003599 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003600 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003601 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003602 window->input_region = NULL;
3603 window->opaque_region = NULL;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003604 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003605
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003606 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003607#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003608 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003609#else
3610 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3611#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003612 else
3613 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003614
Pekka Paalanen4e373742013-02-13 16:17:13 +02003615 wl_surface_set_user_data(window->main_surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003616 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003617 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003618
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003619 if (window->shell_surface) {
3620 wl_shell_surface_set_user_data(window->shell_surface, window);
3621 wl_shell_surface_add_listener(window->shell_surface,
3622 &shell_surface_listener, window);
3623 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003624
Rob Bradford7507b572012-05-15 17:55:34 +01003625 wl_list_init (&window->window_output_list);
3626
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003627 return window;
3628}
3629
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003630struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003631window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003632{
3633 struct window *window;
3634
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003635 window = window_create_internal(display, NULL, TYPE_NONE);
3636 if (!window)
3637 return NULL;
3638
3639 return window;
3640}
3641
3642struct window *
3643window_create_custom(struct display *display)
3644{
3645 struct window *window;
3646
3647 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003648 if (!window)
3649 return NULL;
3650
3651 return window;
3652}
3653
3654struct window *
3655window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003656 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003657{
3658 struct window *window;
3659
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003660 window = window_create_internal(parent->display,
3661 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003662 if (!window)
3663 return NULL;
3664
3665 window->x = x;
3666 window->y = y;
3667
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003668 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003669 wl_shell_surface_set_transient(
3670 window->shell_surface,
3671 window->parent->main_surface->surface,
3672 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003673
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003674 return window;
3675}
3676
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003677static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003678menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003679{
3680 int next;
3681
3682 next = (sy - 8) / 20;
3683 if (menu->current != next) {
3684 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003685 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003686 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003687}
3688
3689static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003690menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003691 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003692 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003693{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003694 struct menu *menu = data;
3695
3696 if (widget == menu->widget)
3697 menu_set_item(data, y);
3698
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003699 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003700}
3701
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003702static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003703menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003704 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003705{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003706 struct menu *menu = data;
3707
3708 if (widget == menu->widget)
3709 menu_set_item(data, y);
3710
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003711 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003712}
3713
3714static void
3715menu_leave_handler(struct widget *widget, struct input *input, void *data)
3716{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003717 struct menu *menu = data;
3718
3719 if (widget == menu->widget)
3720 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003721}
3722
3723static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003724menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003725 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003726 uint32_t button, enum wl_pointer_button_state state,
3727 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003728
3729{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003730 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003731
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003732 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3733 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003734 /* Either relase after press-drag-release or
3735 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003736 menu->func(menu->window->parent,
3737 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003738 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003739 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003740 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003741 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003742 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003743}
3744
3745static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003746menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003747{
3748 cairo_t *cr;
3749 const int32_t r = 3, margin = 3;
3750 struct menu *menu = data;
3751 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003752 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003753
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003754 cr = cairo_create(window->cairo_surface);
3755 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3756 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3757 cairo_paint(cr);
3758
3759 width = window->allocation.width;
3760 height = window->allocation.height;
3761 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003762
3763 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003764 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3765 cairo_fill(cr);
3766
3767 for (i = 0; i < menu->count; i++) {
3768 if (i == menu->current) {
3769 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3770 cairo_rectangle(cr, margin, i * 20 + margin,
3771 width - 2 * margin, 20);
3772 cairo_fill(cr);
3773 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3774 cairo_move_to(cr, 10, i * 20 + 16);
3775 cairo_show_text(cr, menu->entries[i]);
3776 } else {
3777 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3778 cairo_move_to(cr, 10, i * 20 + 16);
3779 cairo_show_text(cr, menu->entries[i]);
3780 }
3781 }
3782
3783 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003784}
3785
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003786void
3787window_show_menu(struct display *display,
3788 struct input *input, uint32_t time, struct window *parent,
3789 int32_t x, int32_t y,
3790 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003791{
3792 struct window *window;
3793 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003794 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003795
3796 menu = malloc(sizeof *menu);
3797 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003798 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003799
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003800 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003801 if (!window) {
3802 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003803 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003804 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003805
3806 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003807 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003808 menu->entries = entries;
3809 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003810 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003811 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003812 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003813 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003814 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003815 window->type = TYPE_MENU;
3816 window->x = x;
3817 window->y = y;
3818
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003819 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003820 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003821 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003822 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003823 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003824
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003825 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003826 widget_set_enter_handler(menu->widget, menu_enter_handler);
3827 widget_set_leave_handler(menu->widget, menu_leave_handler);
3828 widget_set_motion_handler(menu->widget, menu_motion_handler);
3829 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003830
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003831 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003832 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003833}
3834
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003835void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003836window_set_buffer_type(struct window *window, enum window_buffer_type type)
3837{
3838 window->buffer_type = type;
3839}
3840
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003841
3842static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003843display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003844 struct wl_output *wl_output,
3845 int x, int y,
3846 int physical_width,
3847 int physical_height,
3848 int subpixel,
3849 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003850 const char *model,
3851 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003852{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003853 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003854
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003855 output->allocation.x = x;
3856 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003857 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003858}
3859
3860static void
3861display_handle_mode(void *data,
3862 struct wl_output *wl_output,
3863 uint32_t flags,
3864 int width,
3865 int height,
3866 int refresh)
3867{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003868 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003869 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003870
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003871 if (flags & WL_OUTPUT_MODE_CURRENT) {
3872 output->allocation.width = width;
3873 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003874 if (display->output_configure_handler)
3875 (*display->output_configure_handler)(
3876 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003877 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003878}
3879
3880static const struct wl_output_listener output_listener = {
3881 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003882 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003883};
3884
3885static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003886display_add_output(struct display *d, uint32_t id)
3887{
3888 struct output *output;
3889
3890 output = malloc(sizeof *output);
3891 if (output == NULL)
3892 return;
3893
3894 memset(output, 0, sizeof *output);
3895 output->display = d;
3896 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003897 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003898 wl_list_insert(d->output_list.prev, &output->link);
3899
3900 wl_output_add_listener(output->output, &output_listener, output);
3901}
3902
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003903static void
3904output_destroy(struct output *output)
3905{
3906 if (output->destroy_handler)
3907 (*output->destroy_handler)(output, output->user_data);
3908
3909 wl_output_destroy(output->output);
3910 wl_list_remove(&output->link);
3911 free(output);
3912}
3913
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003914void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003915display_set_global_handler(struct display *display,
3916 display_global_handler_t handler)
3917{
3918 struct global *global;
3919
3920 display->global_handler = handler;
3921 if (!handler)
3922 return;
3923
3924 wl_list_for_each(global, &display->global_list, link)
3925 display->global_handler(display,
3926 global->name, global->interface,
3927 global->version, display->user_data);
3928}
3929
3930void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003931display_set_output_configure_handler(struct display *display,
3932 display_output_handler_t handler)
3933{
3934 struct output *output;
3935
3936 display->output_configure_handler = handler;
3937 if (!handler)
3938 return;
3939
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003940 wl_list_for_each(output, &display->output_list, link) {
3941 if (output->allocation.width == 0 &&
3942 output->allocation.height == 0)
3943 continue;
3944
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003945 (*display->output_configure_handler)(output,
3946 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003947 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003948}
3949
3950void
3951output_set_user_data(struct output *output, void *data)
3952{
3953 output->user_data = data;
3954}
3955
3956void *
3957output_get_user_data(struct output *output)
3958{
3959 return output->user_data;
3960}
3961
3962void
3963output_set_destroy_handler(struct output *output,
3964 display_output_handler_t handler)
3965{
3966 output->destroy_handler = handler;
3967 /* FIXME: implement this, once we have way to remove outputs */
3968}
3969
3970void
Scott Moreau4e072362012-09-29 02:03:11 -06003971output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003972{
Scott Moreau4e072362012-09-29 02:03:11 -06003973 struct rectangle allocation = output->allocation;
3974
3975 switch (output->transform) {
3976 case WL_OUTPUT_TRANSFORM_90:
3977 case WL_OUTPUT_TRANSFORM_270:
3978 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3979 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3980 /* Swap width and height */
3981 allocation.width = output->allocation.height;
3982 allocation.height = output->allocation.width;
3983 break;
3984 }
3985
3986 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003987}
3988
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003989struct wl_output *
3990output_get_wl_output(struct output *output)
3991{
3992 return output->output;
3993}
3994
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003995enum wl_output_transform
3996output_get_transform(struct output *output)
3997{
3998 return output->transform;
3999}
4000
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004001static void
Daniel Stone97f68542012-05-30 16:32:01 +01004002fini_xkb(struct input *input)
4003{
4004 xkb_state_unref(input->xkb.state);
4005 xkb_map_unref(input->xkb.keymap);
4006}
4007
4008static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004009display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004010{
4011 struct input *input;
4012
4013 input = malloc(sizeof *input);
4014 if (input == NULL)
4015 return;
4016
4017 memset(input, 0, sizeof *input);
4018 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004019 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004020 input->pointer_focus = NULL;
4021 input->keyboard_focus = NULL;
4022 wl_list_insert(d->input_list.prev, &input->link);
4023
Daniel Stone37816df2012-05-16 18:45:18 +01004024 wl_seat_add_listener(input->seat, &seat_listener, input);
4025 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004026
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004027 input->data_device =
4028 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004029 input->seat);
4030 wl_data_device_add_listener(input->data_device, &data_device_listener,
4031 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004032
4033 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004034
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004035 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4036 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004037 input->repeat_task.run = keyboard_repeat_func;
4038 display_watch_fd(d, input->repeat_timer_fd,
4039 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004040}
4041
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004042static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004043input_destroy(struct input *input)
4044{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004045 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004046 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004047
4048 if (input->drag_offer)
4049 data_offer_destroy(input->drag_offer);
4050
4051 if (input->selection_offer)
4052 data_offer_destroy(input->selection_offer);
4053
4054 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004055 fini_xkb(input);
4056
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004057 wl_surface_destroy(input->pointer_surface);
4058
Pekka Paalanene1207c72011-12-16 12:02:09 +02004059 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004060 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004061 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004062 free(input);
4063}
4064
4065static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004066init_workspace_manager(struct display *d, uint32_t id)
4067{
4068 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004069 wl_registry_bind(d->registry, id,
4070 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004071 if (d->workspace_manager != NULL)
4072 workspace_manager_add_listener(d->workspace_manager,
4073 &workspace_manager_listener,
4074 d);
4075}
4076
4077static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004078registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4079 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004080{
4081 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004082 struct global *global;
4083
4084 global = malloc(sizeof *global);
4085 global->name = id;
4086 global->interface = strdup(interface);
4087 global->version = version;
4088 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004089
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004090 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004091 d->compositor = wl_registry_bind(registry, id,
4092 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004093 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004094 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004095 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004096 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004097 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004098 d->shell = wl_registry_bind(registry,
4099 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004100 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004101 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004102 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4103 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004104 wl_registry_bind(registry, id,
4105 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004106 } else if (strcmp(interface, "text_cursor_position") == 0) {
4107 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004108 wl_registry_bind(registry, id,
4109 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004110 } else if (strcmp(interface, "workspace_manager") == 0) {
4111 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004112 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004113
4114 if (d->global_handler)
4115 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004116}
4117
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004118static void
4119registry_handle_global_remove(void *data, struct wl_registry *registry,
4120 uint32_t name)
4121{
4122 struct display *d = data;
4123 struct global *global;
4124 struct global *tmp;
4125
4126 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4127 if (global->name != name)
4128 continue;
4129
4130 /* XXX: Should destroy bound globals, and call
4131 * the counterpart of display::global_handler
4132 */
4133 wl_list_remove(&global->link);
4134 free(global->interface);
4135 free(global);
4136 }
4137}
4138
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004139void *
4140display_bind(struct display *display, uint32_t name,
4141 const struct wl_interface *interface, uint32_t version)
4142{
4143 return wl_registry_bind(display->registry, name, interface, version);
4144}
4145
4146static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004147 registry_handle_global,
4148 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004149};
4150
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004151#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004152static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004153init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004154{
4155 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004156 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004157
Rob Clark6396ed32012-03-11 19:48:41 -05004158#ifdef USE_CAIRO_GLESV2
4159# define GL_BIT EGL_OPENGL_ES2_BIT
4160#else
4161# define GL_BIT EGL_OPENGL_BIT
4162#endif
4163
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004164 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004165 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004166 EGL_RED_SIZE, 1,
4167 EGL_GREEN_SIZE, 1,
4168 EGL_BLUE_SIZE, 1,
4169 EGL_ALPHA_SIZE, 1,
4170 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004171 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004172 EGL_NONE
4173 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004174
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004175#ifdef USE_CAIRO_GLESV2
4176 static const EGLint context_attribs[] = {
4177 EGL_CONTEXT_CLIENT_VERSION, 2,
4178 EGL_NONE
4179 };
4180 EGLint api = EGL_OPENGL_ES_API;
4181#else
4182 EGLint *context_attribs = NULL;
4183 EGLint api = EGL_OPENGL_API;
4184#endif
4185
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004186 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004187 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004188 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004189 return -1;
4190 }
4191
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004192 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004193 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004194 return -1;
4195 }
4196
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004197 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4198 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004199 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004200 return -1;
4201 }
4202
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004203 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004204 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004205 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004206 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004207 return -1;
4208 }
4209
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004210 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004211 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004212 return -1;
4213 }
4214
Benjamin Franzke0c991632011-09-27 21:57:31 +02004215 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4216 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004217 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004218 return -1;
4219 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004220
4221 return 0;
4222}
4223
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004224static void
4225fini_egl(struct display *display)
4226{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004227 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004228
4229 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4230 EGL_NO_CONTEXT);
4231
4232 eglTerminate(display->dpy);
4233 eglReleaseThread();
4234}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004235#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004236
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004237static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004238init_dummy_surface(struct display *display)
4239{
4240 int len;
4241 void *data;
4242
4243 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4244 data = malloc(len);
4245 display->dummy_surface =
4246 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4247 1, 1, len);
4248 display->dummy_surface_data = data;
4249}
4250
4251static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004252handle_display_data(struct task *task, uint32_t events)
4253{
4254 struct display *display =
4255 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004256 struct epoll_event ep;
4257 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004258
4259 display->display_fd_events = events;
4260
4261 if (events & EPOLLERR || events & EPOLLHUP) {
4262 display_exit(display);
4263 return;
4264 }
4265
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004266 if (events & EPOLLIN) {
4267 ret = wl_display_dispatch(display->display);
4268 if (ret == -1) {
4269 display_exit(display);
4270 return;
4271 }
4272 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004273
4274 if (events & EPOLLOUT) {
4275 ret = wl_display_flush(display->display);
4276 if (ret == 0) {
4277 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4278 ep.data.ptr = &display->display_task;
4279 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4280 display->display_fd, &ep);
4281 } else if (ret == -1 && errno != EAGAIN) {
4282 display_exit(display);
4283 return;
4284 }
4285 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004286}
4287
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004288struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004289display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004290{
4291 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004292
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004293 d = malloc(sizeof *d);
4294 if (d == NULL)
4295 return NULL;
4296
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004297 memset(d, 0, sizeof *d);
4298
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004299 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004300 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004301 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004302 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004303 return NULL;
4304 }
4305
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004306 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004307 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004308 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004309 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4310 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004311
4312 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004313 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004314 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004315 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004316
Daniel Stone97f68542012-05-30 16:32:01 +01004317 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004318 if (d->xkb_context == NULL) {
4319 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004320 return NULL;
4321 }
4322
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004323 d->workspace = 0;
4324 d->workspace_count = 1;
4325
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004326 d->registry = wl_display_get_registry(d->display);
4327 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004328
4329 if (wl_display_dispatch(d->display) < 0) {
4330 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4331 return NULL;
4332 }
4333
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004334#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004335 if (init_egl(d) < 0)
4336 fprintf(stderr, "EGL does not seem to work, "
4337 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004338#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004339
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004340 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004341
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004342 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004343
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004344 wl_list_init(&d->window_list);
4345
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004346 init_dummy_surface(d);
4347
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004348 return d;
4349}
4350
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004351static void
4352display_destroy_outputs(struct display *display)
4353{
4354 struct output *tmp;
4355 struct output *output;
4356
4357 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4358 output_destroy(output);
4359}
4360
Pekka Paalanene1207c72011-12-16 12:02:09 +02004361static void
4362display_destroy_inputs(struct display *display)
4363{
4364 struct input *tmp;
4365 struct input *input;
4366
4367 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4368 input_destroy(input);
4369}
4370
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004371void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004372display_destroy(struct display *display)
4373{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004374 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004375 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4376 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004377
4378 if (!wl_list_empty(&display->deferred_list))
4379 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4380
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004381 cairo_surface_destroy(display->dummy_surface);
4382 free(display->dummy_surface_data);
4383
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004384 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004385 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004386
Daniel Stone97f68542012-05-30 16:32:01 +01004387 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004388
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004389 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004390 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004391
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004392#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004393 if (display->argb_device)
4394 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004395#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004396
Pekka Paalanenc2052982011-12-16 11:41:32 +02004397 if (display->shell)
4398 wl_shell_destroy(display->shell);
4399
4400 if (display->shm)
4401 wl_shm_destroy(display->shm);
4402
4403 if (display->data_device_manager)
4404 wl_data_device_manager_destroy(display->data_device_manager);
4405
4406 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004407 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004408
4409 close(display->epoll_fd);
4410
U. Artie Eoff44874d92012-10-02 21:12:35 -07004411 if (!(display->display_fd_events & EPOLLERR) &&
4412 !(display->display_fd_events & EPOLLHUP))
4413 wl_display_flush(display->display);
4414
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004415 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004416 free(display);
4417}
4418
4419void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004420display_set_user_data(struct display *display, void *data)
4421{
4422 display->user_data = data;
4423}
4424
4425void *
4426display_get_user_data(struct display *display)
4427{
4428 return display->user_data;
4429}
4430
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004431struct wl_display *
4432display_get_display(struct display *display)
4433{
4434 return display->display;
4435}
4436
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004437struct output *
4438display_get_output(struct display *display)
4439{
4440 return container_of(display->output_list.next, struct output, link);
4441}
4442
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004443struct wl_compositor *
4444display_get_compositor(struct display *display)
4445{
4446 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004447}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004448
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004449uint32_t
4450display_get_serial(struct display *display)
4451{
4452 return display->serial;
4453}
4454
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004455EGLDisplay
4456display_get_egl_display(struct display *d)
4457{
4458 return d->dpy;
4459}
4460
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004461struct wl_data_source *
4462display_create_data_source(struct display *display)
4463{
4464 return wl_data_device_manager_create_data_source(display->data_device_manager);
4465}
4466
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004467EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004468display_get_argb_egl_config(struct display *d)
4469{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004470 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004471}
4472
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004473struct wl_shell *
4474display_get_shell(struct display *display)
4475{
4476 return display->shell;
4477}
4478
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004479int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004480display_acquire_window_surface(struct display *display,
4481 struct window *window,
4482 EGLContext ctx)
4483{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004484 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004485 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004486
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004487 return window->toysurface->acquire(window->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004488}
4489
4490void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004491display_release_window_surface(struct display *display,
4492 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004493{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004494 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004495 return;
4496
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004497 window->toysurface->release(window->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004498}
4499
4500void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004501display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004502{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004503 wl_list_insert(&display->deferred_list, &task->link);
4504}
4505
4506void
4507display_watch_fd(struct display *display,
4508 int fd, uint32_t events, struct task *task)
4509{
4510 struct epoll_event ep;
4511
4512 ep.events = events;
4513 ep.data.ptr = task;
4514 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4515}
4516
4517void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004518display_unwatch_fd(struct display *display, int fd)
4519{
4520 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4521}
4522
4523void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004524display_run(struct display *display)
4525{
4526 struct task *task;
4527 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004528 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004529
Pekka Paalanen826d7952011-12-15 10:14:07 +02004530 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004531 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004532 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004533 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004534 struct task, link);
4535 wl_list_remove(&task->link);
4536 task->run(task, 0);
4537 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004538
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004539 wl_display_dispatch_pending(display->display);
4540
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004541 if (!display->running)
4542 break;
4543
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004544 ret = wl_display_flush(display->display);
4545 if (ret < 0 && errno == EAGAIN) {
4546 ep[0].events =
4547 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4548 ep[0].data.ptr = &display->display_task;
4549
4550 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4551 display->display_fd, &ep[0]);
4552 } else if (ret < 0) {
4553 break;
4554 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004555
4556 count = epoll_wait(display->epoll_fd,
4557 ep, ARRAY_LENGTH(ep), -1);
4558 for (i = 0; i < count; i++) {
4559 task = ep[i].data.ptr;
4560 task->run(task, ep[i].events);
4561 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004562 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004563}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004564
4565void
4566display_exit(struct display *display)
4567{
4568 display->running = 0;
4569}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004570
4571void
4572keysym_modifiers_add(struct wl_array *modifiers_map,
4573 const char *name)
4574{
4575 size_t len = strlen(name) + 1;
4576 char *p;
4577
4578 p = wl_array_add(modifiers_map, len);
4579
4580 if (p == NULL)
4581 return;
4582
4583 strncpy(p, name, len);
4584}
4585
4586static xkb_mod_index_t
4587keysym_modifiers_get_index(struct wl_array *modifiers_map,
4588 const char *name)
4589{
4590 xkb_mod_index_t index = 0;
4591 char *p = modifiers_map->data;
4592
4593 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4594 if (strcmp(p, name) == 0)
4595 return index;
4596
4597 index++;
4598 p += strlen(p) + 1;
4599 }
4600
4601 return XKB_MOD_INVALID;
4602}
4603
4604xkb_mod_mask_t
4605keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4606 const char *name)
4607{
4608 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4609
4610 if (index == XKB_MOD_INVALID)
4611 return XKB_MOD_INVALID;
4612
4613 return 1 << index;
4614}