blob: e0b1879f7cbeaa6ac4d629e21baa9fc074952a97 [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;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200196 struct toysurface *toysurface;
197
198 struct rectangle allocation;
199 struct rectangle server_allocation;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200200};
201
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500202struct window {
203 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500204 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100205 struct wl_list window_output_list;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500208 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200209 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400210 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500211 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500212 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400213 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400214 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400215 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400216 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400217 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400218 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400219 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400220 int focus_count;
221
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400222 enum window_buffer_type buffer_type;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +0200223 enum wl_output_transform buffer_transform;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400224 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500225
Pekka Paalanen99436862012-11-19 17:15:59 +0200226 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200227 int fullscreen_method;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400228
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500229 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500230 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400231 window_data_handler_t data_handler;
232 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500233 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400234 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200235 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400236
Pekka Paalanen4e373742013-02-13 16:17:13 +0200237 struct surface *main_surface;
238 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300239 struct wl_callback *frame_cb;
240
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200241 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500242 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500244 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400245 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500246};
247
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500248struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500249 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300250 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500251 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252 struct wl_list link;
253 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500254 widget_resize_handler_t resize_handler;
255 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500256 widget_enter_handler_t enter_handler;
257 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500258 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500259 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200260 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400261 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500262 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500264 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265};
266
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400267struct input {
268 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100269 struct wl_seat *seat;
270 struct wl_pointer *pointer;
271 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400272 struct window *pointer_focus;
273 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300274 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300275 uint32_t cursor_anim_start;
276 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300277 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400278 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400279 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400280 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400281 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400282 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400283
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500284 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500285 struct widget *grab;
286 uint32_t grab_button;
287
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400288 struct wl_data_device *data_device;
289 struct data_offer *drag_offer;
290 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100291
292 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100293 struct xkb_keymap *keymap;
294 struct xkb_state *state;
295 xkb_mod_mask_t control_mask;
296 xkb_mod_mask_t alt_mask;
297 xkb_mod_mask_t shift_mask;
298 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400299
300 struct task repeat_task;
301 int repeat_timer_fd;
302 uint32_t repeat_sym;
303 uint32_t repeat_key;
304 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400305};
306
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500307struct output {
308 struct display *display;
309 struct wl_output *output;
310 struct rectangle allocation;
311 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600312 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200313
314 display_output_handler_t destroy_handler;
315 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500316};
317
Martin Minarik1998b152012-05-10 02:04:35 +0200318enum frame_button_action {
319 FRAME_BUTTON_NULL = 0,
320 FRAME_BUTTON_ICON = 1,
321 FRAME_BUTTON_CLOSE = 2,
322 FRAME_BUTTON_MINIMIZE = 3,
323 FRAME_BUTTON_MAXIMIZE = 4,
324};
325
326enum frame_button_pointer {
327 FRAME_BUTTON_DEFAULT = 0,
328 FRAME_BUTTON_OVER = 1,
329 FRAME_BUTTON_ACTIVE = 2,
330};
331
332enum frame_button_align {
333 FRAME_BUTTON_RIGHT = 0,
334 FRAME_BUTTON_LEFT = 1,
335};
336
337enum frame_button_decoration {
338 FRAME_BUTTON_NONE = 0,
339 FRAME_BUTTON_FANCY = 1,
340};
341
342struct frame_button {
343 struct widget *widget;
344 struct frame *frame;
345 cairo_surface_t *icon;
346 enum frame_button_action type;
347 enum frame_button_pointer state;
348 struct wl_list link; /* buttons_list */
349 enum frame_button_align align;
350 enum frame_button_decoration decoration;
351};
352
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500353struct frame {
354 struct widget *widget;
355 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200356 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500357};
358
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500359struct menu {
360 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500361 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500362 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500363 const char **entries;
364 uint32_t time;
365 int current;
366 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400367 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500368 menu_func_t func;
369};
370
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300371struct tooltip {
372 struct widget *parent;
373 struct window *window;
374 struct widget *widget;
375 char *entry;
376 struct task tooltip_task;
377 int tooltip_fd;
378 float x, y;
379};
380
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700381struct shm_pool {
382 struct wl_shm_pool *pool;
383 size_t size;
384 size_t used;
385 void *data;
386};
387
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400388enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300389 CURSOR_DEFAULT = 100,
390 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400391};
392
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500393enum window_location {
394 WINDOW_INTERIOR = 0,
395 WINDOW_RESIZING_TOP = 1,
396 WINDOW_RESIZING_BOTTOM = 2,
397 WINDOW_RESIZING_LEFT = 4,
398 WINDOW_RESIZING_TOP_LEFT = 5,
399 WINDOW_RESIZING_BOTTOM_LEFT = 6,
400 WINDOW_RESIZING_RIGHT = 8,
401 WINDOW_RESIZING_TOP_RIGHT = 9,
402 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
403 WINDOW_RESIZING_MASK = 15,
404 WINDOW_EXTERIOR = 16,
405 WINDOW_TITLEBAR = 17,
406 WINDOW_CLIENT_AREA = 18,
407};
408
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200409static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500411#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200413struct egl_window_surface {
414 struct toysurface base;
415 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100416 struct display *display;
417 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200418 struct wl_egl_window *egl_window;
419 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100420};
421
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200422static struct egl_window_surface *
423to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100424{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200425 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100426}
427
428static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200429egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200430 int width, int height, uint32_t flags)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100431{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200432 struct egl_window_surface *surface = to_egl_window_surface(base);
433
434 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
435 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
436
437 return cairo_surface_reference(surface->cairo_surface);
438}
439
440static void
441egl_window_surface_swap(struct toysurface *base,
442 struct rectangle *server_allocation)
443{
444 struct egl_window_surface *surface = to_egl_window_surface(base);
445
446 cairo_gl_surface_swapbuffers(surface->cairo_surface);
447 wl_egl_window_get_attached_size(surface->egl_window,
448 &server_allocation->width,
449 &server_allocation->height);
450}
451
452static int
453egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
454{
455 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200456 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100457
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200458 device = cairo_surface_get_device(surface->cairo_surface);
459 if (!device)
460 return -1;
461
462 if (!ctx) {
463 if (device == surface->display->argb_device)
464 ctx = surface->display->argb_ctx;
465 else
466 assert(0);
467 }
468
469 cairo_device_flush(device);
470 cairo_device_acquire(device);
471 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
472 surface->egl_surface, ctx))
473 fprintf(stderr, "failed to make surface current\n");
474
475 return 0;
476}
477
478static void
479egl_window_surface_release(struct toysurface *base)
480{
481 struct egl_window_surface *surface = to_egl_window_surface(base);
482 cairo_device_t *device;
483
484 device = cairo_surface_get_device(surface->cairo_surface);
485 if (!device)
486 return;
487
488 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
489 surface->display->argb_ctx))
490 fprintf(stderr, "failed to make context current\n");
491
492 cairo_device_release(device);
493}
494
495static void
496egl_window_surface_destroy(struct toysurface *base)
497{
498 struct egl_window_surface *surface = to_egl_window_surface(base);
499 struct display *d = surface->display;
500
501 cairo_surface_destroy(surface->cairo_surface);
502 eglDestroySurface(d->dpy, surface->egl_surface);
503 wl_egl_window_destroy(surface->egl_window);
504 surface->surface = NULL;
505
506 free(surface);
507}
508
509static struct toysurface *
510egl_window_surface_create(struct display *display,
511 struct wl_surface *wl_surface,
512 uint32_t flags,
513 struct rectangle *rectangle)
514{
515 struct egl_window_surface *surface;
516
Pekka Paalanenb3627362012-11-19 17:16:00 +0200517 if (display->dpy == EGL_NO_DISPLAY)
518 return NULL;
519
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200520 surface = calloc(1, sizeof *surface);
521 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522 return NULL;
523
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 surface->base.prepare = egl_window_surface_prepare;
525 surface->base.swap = egl_window_surface_swap;
526 surface->base.acquire = egl_window_surface_acquire;
527 surface->base.release = egl_window_surface_release;
528 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 surface->display = display;
531 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400532
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533 surface->egl_window = wl_egl_window_create(surface->surface,
534 rectangle->width,
535 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100536
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 surface->egl_surface = eglCreateWindowSurface(display->dpy,
538 display->argb_config,
539 surface->egl_window,
540 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 surface->cairo_surface =
543 cairo_gl_surface_create_for_egl(display->argb_device,
544 surface->egl_surface,
545 rectangle->width,
546 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200548 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100549}
550
Pekka Paalanenb3627362012-11-19 17:16:00 +0200551#else
552
553static struct toysurface *
554egl_window_surface_create(struct display *display,
555 struct wl_surface *wl_surface,
556 uint32_t flags,
557 struct rectangle *rectangle)
558{
559 return NULL;
560}
561
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562#endif
563
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200564struct shm_surface_data {
565 struct wl_buffer *buffer;
566 struct shm_pool *pool;
567};
568
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400569struct wl_buffer *
570display_get_buffer_for_surface(struct display *display,
571 cairo_surface_t *surface)
572{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200573 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200575 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576
577 return data->buffer;
578}
579
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500580static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700581shm_pool_destroy(struct shm_pool *pool);
582
583static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584shm_surface_data_destroy(void *p)
585{
586 struct shm_surface_data *data = p;
587
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200588 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700589 if (data->pool)
590 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300591
592 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593}
594
Kristian Høgsberg16626282012-04-03 11:21:27 -0400595static struct wl_shm_pool *
596make_shm_pool(struct display *display, int size, void **data)
597{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400598 struct wl_shm_pool *pool;
599 int fd;
600
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300601 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400602 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300603 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
604 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400605 return NULL;
606 }
607
608 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400609 if (*data == MAP_FAILED) {
610 fprintf(stderr, "mmap failed: %m\n");
611 close(fd);
612 return NULL;
613 }
614
615 pool = wl_shm_create_pool(display->shm, fd, size);
616
617 close(fd);
618
619 return pool;
620}
621
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700622static struct shm_pool *
623shm_pool_create(struct display *display, size_t size)
624{
625 struct shm_pool *pool = malloc(sizeof *pool);
626
627 if (!pool)
628 return NULL;
629
630 pool->pool = make_shm_pool(display, size, &pool->data);
631 if (!pool->pool) {
632 free(pool);
633 return NULL;
634 }
635
636 pool->size = size;
637 pool->used = 0;
638
639 return pool;
640}
641
642static void *
643shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
644{
645 if (pool->used + size > pool->size)
646 return NULL;
647
648 *offset = pool->used;
649 pool->used += size;
650
651 return (char *) pool->data + *offset;
652}
653
654/* destroy the pool. this does not unmap the memory though */
655static void
656shm_pool_destroy(struct shm_pool *pool)
657{
658 munmap(pool->data, pool->size);
659 wl_shm_pool_destroy(pool->pool);
660 free(pool);
661}
662
663/* Start allocating from the beginning of the pool again */
664static void
665shm_pool_reset(struct shm_pool *pool)
666{
667 pool->used = 0;
668}
669
670static int
671data_length_for_shm_surface(struct rectangle *rect)
672{
673 int stride;
674
675 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
676 rect->width);
677 return stride * rect->height;
678}
679
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500680static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700681display_create_shm_surface_from_pool(struct display *display,
682 struct rectangle *rectangle,
683 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684{
685 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400686 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400687 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700688 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400689 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690
691 data = malloc(sizeof *data);
692 if (data == NULL)
693 return NULL;
694
695 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
696 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700697 length = stride * rectangle->height;
698 data->pool = NULL;
699 map = shm_pool_allocate(pool, length, &offset);
700
701 if (!map) {
702 free(data);
703 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400704 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400706 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400707 CAIRO_FORMAT_ARGB32,
708 rectangle->width,
709 rectangle->height,
710 stride);
711
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200712 cairo_surface_set_user_data(surface, &shm_surface_data_key,
713 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400714
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400715 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500716 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400717 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500718 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400719
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200720 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
721 rectangle->width,
722 rectangle->height,
723 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725 return surface;
726}
727
728static cairo_surface_t *
729display_create_shm_surface(struct display *display,
730 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200731 struct shm_pool *alternate_pool,
732 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733{
734 struct shm_surface_data *data;
735 struct shm_pool *pool;
736 cairo_surface_t *surface;
737
Pekka Paalanen99436862012-11-19 17:15:59 +0200738 if (alternate_pool) {
739 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700740 surface = display_create_shm_surface_from_pool(display,
741 rectangle,
742 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200743 alternate_pool);
744 if (surface) {
745 data = cairo_surface_get_user_data(surface,
746 &shm_surface_data_key);
747 goto out;
748 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700749 }
750
751 pool = shm_pool_create(display,
752 data_length_for_shm_surface(rectangle));
753 if (!pool)
754 return NULL;
755
756 surface =
757 display_create_shm_surface_from_pool(display, rectangle,
758 flags, pool);
759
760 if (!surface) {
761 shm_pool_destroy(pool);
762 return NULL;
763 }
764
765 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200766 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700767 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400768
Pekka Paalanen99436862012-11-19 17:15:59 +0200769out:
770 if (data_ret)
771 *data_ret = data;
772
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400773 return surface;
774}
775
nobled7b87cb02011-02-01 18:51:47 +0000776static int
777check_size(struct rectangle *rect)
778{
779 if (rect->width && rect->height)
780 return 0;
781
782 fprintf(stderr, "tried to create surface of "
783 "width: %d, height: %d\n", rect->width, rect->height);
784 return -1;
785}
786
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787cairo_surface_t *
788display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400790 struct rectangle *rectangle,
791 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792{
nobled7b87cb02011-02-01 18:51:47 +0000793 if (check_size(rectangle) < 0)
794 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200795
796 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200797 return display_create_shm_surface(display, rectangle, flags,
798 NULL, NULL);
799}
800
Pekka Paalanena4eda732012-11-19 17:16:02 +0200801struct shm_surface_leaf {
802 cairo_surface_t *cairo_surface;
803 /* 'data' is automatically destroyed, when 'cairo_surface' is */
804 struct shm_surface_data *data;
805
806 struct shm_pool *resize_pool;
807 int busy;
808};
809
810static void
811shm_surface_leaf_release(struct shm_surface_leaf *leaf)
812{
813 if (leaf->cairo_surface)
814 cairo_surface_destroy(leaf->cairo_surface);
815 /* leaf->data already destroyed via cairo private */
816
817 if (leaf->resize_pool)
818 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200819
820 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200821}
822
Pekka Paalanen99436862012-11-19 17:15:59 +0200823struct shm_surface {
824 struct toysurface base;
825 struct display *display;
826 struct wl_surface *surface;
827 uint32_t flags;
828 int dx, dy;
829
Pekka Paalanena4eda732012-11-19 17:16:02 +0200830 struct shm_surface_leaf leaf[2];
831 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200832};
833
834static struct shm_surface *
835to_shm_surface(struct toysurface *base)
836{
837 return container_of(base, struct shm_surface, base);
838}
839
Pekka Paalanena4eda732012-11-19 17:16:02 +0200840static void
841shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
842{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200843 struct shm_surface *surface = data;
Pekka Paalanena4eda732012-11-19 17:16:02 +0200844
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200845 if (surface->leaf[0].data->buffer == buffer)
846 surface->leaf[0].busy = 0;
847 else if (surface->leaf[1].data->buffer == buffer)
848 surface->leaf[1].busy = 0;
849 else
850 assert(0 && "shm_surface_buffer_release: unknown buffer");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200851
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200852 if (!surface->leaf[0].busy && !surface->leaf[1].busy)
853 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200854}
855
856static const struct wl_buffer_listener shm_surface_buffer_listener = {
857 shm_surface_buffer_release
858};
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860static cairo_surface_t *
861shm_surface_prepare(struct toysurface *base, int dx, int dy,
Pekka Paalanenec076692012-11-30 13:37:27 +0200862 int width, int height, uint32_t flags)
Pekka Paalanen99436862012-11-19 17:15:59 +0200863{
Pekka Paalanenec076692012-11-30 13:37:27 +0200864 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 struct shm_surface *surface = to_shm_surface(base);
866 struct rectangle rect = { 0, 0, width, height };
Pekka Paalanena4eda732012-11-19 17:16:02 +0200867 struct shm_surface_leaf *leaf;
Pekka Paalanen99436862012-11-19 17:15:59 +0200868
869 surface->dx = dx;
870 surface->dy = dy;
871
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200872 /* See shm_surface_buffer_release() */
873 if (!surface->leaf[0].busy && !surface->leaf[1].busy &&
874 surface->leaf[1].cairo_surface) {
875 fprintf(stderr, "window.c:%s: TODO: release leaf[1]\n",
876 __func__);
877 }
878
Pekka Paalanena4eda732012-11-19 17:16:02 +0200879 /* pick a free buffer from the two */
880 if (!surface->leaf[0].busy)
881 leaf = &surface->leaf[0];
882 else if (!surface->leaf[1].busy)
883 leaf = &surface->leaf[1];
884 else {
885 fprintf(stderr, "%s: both buffers are held by the server.\n",
886 __func__);
887 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200888 }
889
Pekka Paalanena4eda732012-11-19 17:16:02 +0200890 if (!resize_hint && leaf->resize_pool) {
891 cairo_surface_destroy(leaf->cairo_surface);
892 leaf->cairo_surface = NULL;
893 shm_pool_destroy(leaf->resize_pool);
894 leaf->resize_pool = NULL;
895 }
896
897 if (leaf->cairo_surface &&
898 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
899 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +0200900 goto out;
901
Pekka Paalanena4eda732012-11-19 17:16:02 +0200902 if (leaf->cairo_surface)
903 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200904
Pekka Paalanena4eda732012-11-19 17:16:02 +0200905 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 /* Create a big pool to allocate from, while continuously
907 * resizing. Mmapping a new pool in the server
908 * is relatively expensive, so reusing a pool performs
909 * better, but may temporarily reserve unneeded memory.
910 */
911 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +0200912 leaf->resize_pool = shm_pool_create(surface->display,
913 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +0200914 }
915
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +0200917 display_create_shm_surface(surface->display, &rect,
918 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +0200919 leaf->resize_pool,
920 &leaf->data);
921 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200922 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200923
924out:
Pekka Paalanena4eda732012-11-19 17:16:02 +0200925 surface->current = leaf;
926
927 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200928}
929
930static void
931shm_surface_swap(struct toysurface *base,
932 struct rectangle *server_allocation)
933{
934 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200935 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200936
937 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200939 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +0200940 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +0200941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +0200943 surface->dx, surface->dy);
944 wl_surface_damage(surface->surface, 0, 0,
945 server_allocation->width, server_allocation->height);
946 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947
948 leaf->busy = 1;
949 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +0200950}
951
952static int
953shm_surface_acquire(struct toysurface *base, EGLContext ctx)
954{
955 return -1;
956}
957
958static void
959shm_surface_release(struct toysurface *base)
960{
961}
962
963static void
964shm_surface_destroy(struct toysurface *base)
965{
966 struct shm_surface *surface = to_shm_surface(base);
967
Pekka Paalanena4eda732012-11-19 17:16:02 +0200968 shm_surface_leaf_release(&surface->leaf[0]);
969 shm_surface_leaf_release(&surface->leaf[1]);
Pekka Paalanen99436862012-11-19 17:15:59 +0200970
971 free(surface);
972}
973
974static struct toysurface *
975shm_surface_create(struct display *display, struct wl_surface *wl_surface,
976 uint32_t flags, struct rectangle *rectangle)
977{
978 struct shm_surface *surface;
979
980 surface = calloc(1, sizeof *surface);
981 if (!surface)
982 return NULL;
983
984 surface->base.prepare = shm_surface_prepare;
985 surface->base.swap = shm_surface_swap;
986 surface->base.acquire = shm_surface_acquire;
987 surface->base.release = shm_surface_release;
988 surface->base.destroy = shm_surface_destroy;
989
990 surface->display = display;
991 surface->surface = wl_surface;
992 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +0200993
994 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400995}
996
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200997/*
998 * The following correspondences between file names and cursors was copied
999 * from: https://bugs.kde.org/attachment.cgi?id=67313
1000 */
1001
1002static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001003 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001004 "sw-resize"
1005};
1006
1007static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001008 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001009 "se-resize"
1010};
1011
1012static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001013 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001014 "s-resize"
1015};
1016
1017static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001018 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001019 "closedhand",
1020 "208530c400c041818281048008011002"
1021};
1022
1023static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001024 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001025 "default",
1026 "top_left_arrow",
1027 "left-arrow"
1028};
1029
1030static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001031 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001032 "w-resize"
1033};
1034
1035static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001036 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001037 "e-resize"
1038};
1039
1040static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001041 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001042 "nw-resize"
1043};
1044
1045static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001046 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001047 "ne-resize"
1048};
1049
1050static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001051 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001052 "n-resize"
1053};
1054
1055static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001056 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001057 "ibeam",
1058 "text"
1059};
1060
1061static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001062 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001063 "pointer",
1064 "pointing_hand",
1065 "e29285e634086352946a0e7090d73106"
1066};
1067
1068static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001069 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001070 "wait",
1071 "0426c94ea35c87780ff01dc239897213"
1072};
1073
1074struct cursor_alternatives {
1075 const char **names;
1076 size_t count;
1077};
1078
1079static const struct cursor_alternatives cursors[] = {
1080 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1081 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1082 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1083 {grabbings, ARRAY_LENGTH(grabbings)},
1084 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1085 {left_sides, ARRAY_LENGTH(left_sides)},
1086 {right_sides, ARRAY_LENGTH(right_sides)},
1087 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1088 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1089 {top_sides, ARRAY_LENGTH(top_sides)},
1090 {xterms, ARRAY_LENGTH(xterms)},
1091 {hand1s, ARRAY_LENGTH(hand1s)},
1092 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001093};
1094
1095static void
1096create_cursors(struct display *display)
1097{
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001098 char *config_file;
1099 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001100 unsigned int i, j;
1101 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001102 struct config_key shell_keys[] = {
1103 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1104 };
1105 struct config_section cs[] = {
1106 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1107 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001108
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001109 config_file = config_file_path("weston.ini");
1110 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1111 free(config_file);
1112
1113 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001114 display->cursors =
1115 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1116
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001117 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001118 cursor = NULL;
1119 for (j = 0; !cursor && j < cursors[i].count; ++j)
1120 cursor = wl_cursor_theme_get_cursor(
1121 display->cursor_theme, cursors[i].names[j]);
1122
1123 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001124 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001125 cursors[i].names[0]);
1126
1127 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001128 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001129}
1130
1131static void
1132destroy_cursors(struct display *display)
1133{
1134 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001135 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001136}
1137
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001138struct wl_cursor_image *
1139display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001140{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001141 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001142
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001143 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001144}
1145
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001146static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001147window_get_resize_dx_dy(struct window *window, int *x, int *y)
1148{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001149 struct surface *surface = window->main_surface;
1150
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001151 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001152 *x = surface->server_allocation.width -
1153 surface->allocation.width;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001154 else
1155 *x = 0;
1156
1157 if (window->resize_edges & WINDOW_RESIZING_TOP)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001158 *y = surface->server_allocation.height -
1159 surface->allocation.height;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +02001160 else
1161 *y = 0;
1162
1163 window->resize_edges = 0;
1164}
1165
1166static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001167surface_attach_surface(struct surface *surface)
1168{
1169 surface->toysurface->swap(surface->toysurface,
1170 &surface->server_allocation);
1171}
1172
1173static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001174window_attach_surface(struct window *window)
1175{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001176 struct display *display = window->display;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001177
Kristian Høgsberg407ef642012-04-27 17:17:12 -04001178 if (window->type == TYPE_NONE) {
1179 window->type = TYPE_TOPLEVEL;
1180 if (display->shell)
1181 wl_shell_surface_set_toplevel(window->shell_surface);
1182 }
1183
Pekka Paalanen512dde82012-10-10 12:49:27 +03001184 if (window->opaque_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001185 wl_surface_set_opaque_region(window->main_surface->surface,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001186 window->opaque_region);
1187 wl_region_destroy(window->opaque_region);
1188 window->opaque_region = NULL;
1189 }
1190
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001191 if (window->input_region) {
Pekka Paalanen4e373742013-02-13 16:17:13 +02001192 wl_surface_set_input_region(window->main_surface->surface,
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001193 window->input_region);
1194 wl_region_destroy(window->input_region);
1195 window->input_region = NULL;
1196 }
1197
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001198 surface_attach_surface(window->main_surface);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001199}
1200
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001201int
1202window_has_focus(struct window *window)
1203{
1204 return window->focus_count > 0;
1205}
1206
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001207static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001208window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001209{
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001210 if (!window->cairo_surface)
1211 return;
1212
1213 window_attach_surface(window);
1214 cairo_surface_destroy(window->cairo_surface);
1215 window->cairo_surface = NULL;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001216}
1217
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001218struct display *
1219window_get_display(struct window *window)
1220{
1221 return window->display;
1222}
1223
Pekka Paalanence36f6d2012-11-19 15:32:47 +02001224static void
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001225window_create_surface(struct window *window)
1226{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001227 struct surface *surface = window->main_surface;
1228 struct rectangle allocation = surface->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001229 uint32_t flags = 0;
Pekka Paalanen99436862012-11-19 17:15:59 +02001230 int dx, dy;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001231
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04001232 if (!window->transparent)
1233 flags = SURFACE_OPAQUE;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001234
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001235 switch (window->buffer_transform) {
1236 case WL_OUTPUT_TRANSFORM_90:
1237 case WL_OUTPUT_TRANSFORM_270:
1238 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1239 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001240 allocation.width = surface->allocation.height;
1241 allocation.height = surface->allocation.width;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001242 break;
1243 default:
1244 break;
1245 }
1246
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001247 if (!surface->toysurface &&
Pekka Paalanenb3627362012-11-19 17:16:00 +02001248 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1249 window->display->dpy) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001250 surface->toysurface =
Pekka Paalanenb3627362012-11-19 17:16:00 +02001251 egl_window_surface_create(window->display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001252 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001253 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001254 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001255 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001256
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001257 if (!surface->toysurface)
1258 surface->toysurface = shm_surface_create(window->display,
1259 surface->surface,
1260 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001261
Pekka Paalanenec076692012-11-30 13:37:27 +02001262 if (window->resizing)
1263 flags = SURFACE_HINT_RESIZE;
1264 else
1265 flags = 0;
1266
Pekka Paalanen99436862012-11-19 17:15:59 +02001267 window_get_resize_dx_dy(window, &dx, &dy);
1268 window->cairo_surface =
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001269 surface->toysurface->prepare(surface->toysurface, dx, dy,
1270 allocation.width,
1271 allocation.height,
1272 flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001273}
1274
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001275int
1276window_get_buffer_transform(struct window *window)
1277{
1278 return window->buffer_transform;
1279}
1280
1281void
1282window_set_buffer_transform(struct window *window,
1283 enum wl_output_transform transform)
1284{
1285 window->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001286 wl_surface_set_buffer_transform(window->main_surface->surface,
1287 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001288}
1289
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001290static void frame_destroy(struct frame *frame);
1291
Pekka Paalanen4e373742013-02-13 16:17:13 +02001292static void
1293surface_destroy(struct surface *surface)
1294{
1295 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001296
1297 if (surface->toysurface)
1298 surface->toysurface->destroy(surface->toysurface);
1299
Pekka Paalanen4e373742013-02-13 16:17:13 +02001300 free(surface);
1301}
1302
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001303void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001304window_destroy(struct window *window)
1305{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001306 struct display *display = window->display;
1307 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001308 struct window_output *window_output;
1309 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001310
1311 if (window->redraw_scheduled)
1312 wl_list_remove(&window->redraw_task.link);
1313
1314 wl_list_for_each(input, &display->input_list, link) {
1315 if (input->pointer_focus == window)
1316 input->pointer_focus = NULL;
1317 if (input->keyboard_focus == window)
1318 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001319 if (input->focus_widget &&
1320 input->focus_widget->window == window)
1321 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001322 }
1323
Rob Bradford7507b572012-05-15 17:55:34 +01001324 wl_list_for_each_safe(window_output, window_output_tmp,
1325 &window->window_output_list, link) {
1326 free (window_output);
1327 }
1328
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001329 if (window->input_region)
1330 wl_region_destroy(window->input_region);
1331 if (window->opaque_region)
1332 wl_region_destroy(window->opaque_region);
1333
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001334 if (window->frame)
1335 frame_destroy(window->frame);
1336
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001337 if (window->shell_surface)
1338 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001339
1340 surface_destroy(window->main_surface);
1341
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001342 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001343
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001344 if (window->frame_cb)
1345 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001346 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001347 free(window);
1348}
1349
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001350static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001351widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001352{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001353 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001354
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001355 wl_list_for_each(child, &widget->child_list, link) {
1356 target = widget_find_widget(child, x, y);
1357 if (target)
1358 return target;
1359 }
1360
1361 if (widget->allocation.x <= x &&
1362 x < widget->allocation.x + widget->allocation.width &&
1363 widget->allocation.y <= y &&
1364 y < widget->allocation.y + widget->allocation.height) {
1365 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001366 }
1367
1368 return NULL;
1369}
1370
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001371static struct widget *
1372widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001373{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001374 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001375
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376 widget = malloc(sizeof *widget);
1377 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001378 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001379 widget->user_data = data;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380 widget->allocation = window->main_surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001381 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001382 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001383 widget->tooltip = NULL;
1384 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001385 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001386
1387 return widget;
1388}
1389
1390struct widget *
1391window_add_widget(struct window *window, void *data)
1392{
1393 window->widget = widget_create(window, data);
1394 wl_list_init(&window->widget->link);
1395
1396 return window->widget;
1397}
1398
1399struct widget *
1400widget_add_widget(struct widget *parent, void *data)
1401{
1402 struct widget *widget;
1403
1404 widget = widget_create(parent->window, data);
1405 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001406
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001407 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001408}
1409
1410void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001411widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001412{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001413 struct display *display = widget->window->display;
1414 struct input *input;
1415
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001416 if (widget->tooltip) {
1417 free(widget->tooltip);
1418 widget->tooltip = NULL;
1419 }
1420
Pekka Paalanene156fb62012-01-19 13:51:38 +02001421 wl_list_for_each(input, &display->input_list, link) {
1422 if (input->focus_widget == widget)
1423 input->focus_widget = NULL;
1424 }
1425
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001426 wl_list_remove(&widget->link);
1427 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001428}
1429
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001430void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001431widget_set_default_cursor(struct widget *widget, int cursor)
1432{
1433 widget->default_cursor = cursor;
1434}
1435
1436void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001437widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001438{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001439 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001440}
1441
1442void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001443widget_set_size(struct widget *widget, int32_t width, int32_t height)
1444{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001445 widget->allocation.width = width;
1446 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001447}
1448
1449void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001450widget_set_allocation(struct widget *widget,
1451 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001453 widget->allocation.x = x;
1454 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001455 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001456}
1457
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001458void
1459widget_set_transparent(struct widget *widget, int transparent)
1460{
1461 widget->opaque = !transparent;
1462}
1463
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001464void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001465widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001467 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468}
1469
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001470void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001471widget_set_resize_handler(struct widget *widget,
1472 widget_resize_handler_t handler)
1473{
1474 widget->resize_handler = handler;
1475}
1476
1477void
1478widget_set_redraw_handler(struct widget *widget,
1479 widget_redraw_handler_t handler)
1480{
1481 widget->redraw_handler = handler;
1482}
1483
1484void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001485widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001486{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001487 widget->enter_handler = handler;
1488}
1489
1490void
1491widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1492{
1493 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001494}
1495
1496void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001497widget_set_motion_handler(struct widget *widget,
1498 widget_motion_handler_t handler)
1499{
1500 widget->motion_handler = handler;
1501}
1502
1503void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001504widget_set_button_handler(struct widget *widget,
1505 widget_button_handler_t handler)
1506{
1507 widget->button_handler = handler;
1508}
1509
1510void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001511widget_set_axis_handler(struct widget *widget,
1512 widget_axis_handler_t handler)
1513{
1514 widget->axis_handler = handler;
1515}
1516
1517void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001518widget_schedule_redraw(struct widget *widget)
1519{
1520 window_schedule_redraw(widget->window);
1521}
1522
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001523cairo_surface_t *
1524window_get_surface(struct window *window)
1525{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001526 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001527}
1528
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001529struct wl_surface *
1530window_get_wl_surface(struct window *window)
1531{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001532 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001533}
1534
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001535struct wl_shell_surface *
1536window_get_wl_shell_surface(struct window *window)
1537{
1538 return window->shell_surface;
1539}
1540
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001542tooltip_redraw_handler(struct widget *widget, void *data)
1543{
1544 cairo_t *cr;
1545 const int32_t r = 3;
1546 struct tooltip *tooltip = data;
1547 int32_t width, height;
1548 struct window *window = widget->window;
1549
1550 cr = cairo_create(window->cairo_surface);
1551 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1552 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1553 cairo_paint(cr);
1554
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001555 width = window->main_surface->allocation.width;
1556 height = window->main_surface->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001557 rounded_rect(cr, 0, 0, width, height, r);
1558
1559 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1560 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1561 cairo_fill(cr);
1562
1563 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1564 cairo_move_to(cr, 10, 16);
1565 cairo_show_text(cr, tooltip->entry);
1566 cairo_destroy(cr);
1567}
1568
1569static cairo_text_extents_t
1570get_text_extents(struct tooltip *tooltip)
1571{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001572 cairo_t *cr;
1573 cairo_text_extents_t extents;
1574
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001575 /* Use the dummy_surface because tooltip's surface was not
1576 * created yet, and parent does not have a valid surface
1577 * outside repaint, either.
1578 */
1579 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001580 cairo_text_extents(cr, tooltip->entry, &extents);
1581 cairo_destroy(cr);
1582
1583 return extents;
1584}
1585
1586static int
1587window_create_tooltip(struct tooltip *tooltip)
1588{
1589 struct widget *parent = tooltip->parent;
1590 struct display *display = parent->window->display;
1591 struct window *window;
1592 const int offset_y = 27;
1593 const int margin = 3;
1594 cairo_text_extents_t extents;
1595
1596 if (tooltip->widget)
1597 return 0;
1598
1599 window = window_create_transient(display, parent->window, tooltip->x,
1600 tooltip->y + offset_y,
1601 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1602 if (!window)
1603 return -1;
1604
1605 tooltip->window = window;
1606 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1607
1608 extents = get_text_extents(tooltip);
1609 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1610 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1611
1612 return 0;
1613}
1614
1615void
1616widget_destroy_tooltip(struct widget *parent)
1617{
1618 struct tooltip *tooltip = parent->tooltip;
1619
1620 parent->tooltip_count = 0;
1621 if (!tooltip)
1622 return;
1623
1624 if (tooltip->widget) {
1625 widget_destroy(tooltip->widget);
1626 window_destroy(tooltip->window);
1627 tooltip->widget = NULL;
1628 tooltip->window = NULL;
1629 }
1630
1631 close(tooltip->tooltip_fd);
1632 free(tooltip->entry);
1633 free(tooltip);
1634 parent->tooltip = NULL;
1635}
1636
1637static void
1638tooltip_func(struct task *task, uint32_t events)
1639{
1640 struct tooltip *tooltip =
1641 container_of(task, struct tooltip, tooltip_task);
1642 uint64_t exp;
1643
Martin Olsson8df662a2012-07-08 03:03:47 +02001644 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1645 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001646 window_create_tooltip(tooltip);
1647}
1648
1649#define TOOLTIP_TIMEOUT 500
1650static int
1651tooltip_timer_reset(struct tooltip *tooltip)
1652{
1653 struct itimerspec its;
1654
1655 its.it_interval.tv_sec = 0;
1656 its.it_interval.tv_nsec = 0;
1657 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1658 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1659 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1660 fprintf(stderr, "could not set timerfd\n: %m");
1661 return -1;
1662 }
1663
1664 return 0;
1665}
1666
1667int
1668widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1669{
1670 struct tooltip *tooltip = parent->tooltip;
1671
1672 parent->tooltip_count++;
1673 if (tooltip) {
1674 tooltip->x = x;
1675 tooltip->y = y;
1676 tooltip_timer_reset(tooltip);
1677 return 0;
1678 }
1679
1680 /* the handler might be triggered too fast via input device motion, so
1681 * we need this check here to make sure tooltip is fully initialized */
1682 if (parent->tooltip_count > 1)
1683 return 0;
1684
1685 tooltip = malloc(sizeof *tooltip);
1686 if (!tooltip)
1687 return -1;
1688
1689 parent->tooltip = tooltip;
1690 tooltip->parent = parent;
1691 tooltip->widget = NULL;
1692 tooltip->window = NULL;
1693 tooltip->x = x;
1694 tooltip->y = y;
1695 tooltip->entry = strdup(entry);
1696 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1697 if (tooltip->tooltip_fd < 0) {
1698 fprintf(stderr, "could not create timerfd\n: %m");
1699 return -1;
1700 }
1701
1702 tooltip->tooltip_task.run = tooltip_func;
1703 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1704 EPOLLIN, &tooltip->tooltip_task);
1705 tooltip_timer_reset(tooltip);
1706
1707 return 0;
1708}
1709
1710static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001711workspace_manager_state(void *data,
1712 struct workspace_manager *workspace_manager,
1713 uint32_t current,
1714 uint32_t count)
1715{
1716 struct display *display = data;
1717
1718 display->workspace = current;
1719 display->workspace_count = count;
1720}
1721
1722static const struct workspace_manager_listener workspace_manager_listener = {
1723 workspace_manager_state
1724};
1725
1726static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001727frame_resize_handler(struct widget *widget,
1728 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001729{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001730 struct frame *frame = data;
1731 struct widget *child = frame->child;
1732 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001733 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001734 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001735 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001736 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001737 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001738 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001739
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001740 switch (widget->window->type) {
1741 case TYPE_FULLSCREEN:
1742 decoration_width = 0;
1743 decoration_height = 0;
1744
1745 allocation.x = 0;
1746 allocation.y = 0;
1747 allocation.width = width;
1748 allocation.height = height;
1749 opaque_margin = 0;
1750
1751 wl_list_for_each(button, &frame->buttons_list, link)
1752 button->widget->opaque = 1;
1753 break;
1754 case TYPE_MAXIMIZED:
1755 decoration_width = t->width * 2;
1756 decoration_height = t->width + t->titlebar_height;
1757
1758 allocation.x = t->width;
1759 allocation.y = t->titlebar_height;
1760 allocation.width = width - decoration_width;
1761 allocation.height = height - decoration_height;
1762
1763 opaque_margin = 0;
1764
1765 wl_list_for_each(button, &frame->buttons_list, link)
1766 button->widget->opaque = 0;
1767 break;
1768 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001769 decoration_width = (t->width + t->margin) * 2;
1770 decoration_height = t->width +
1771 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001772
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001773 allocation.x = t->width + t->margin;
1774 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001775 allocation.width = width - decoration_width;
1776 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001777
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001778 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001779
1780 wl_list_for_each(button, &frame->buttons_list, link)
1781 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001782 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001783 }
1784
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001785 widget_set_allocation(child, allocation.x, allocation.y,
1786 allocation.width, allocation.height);
1787
1788 if (child->resize_handler)
1789 child->resize_handler(child,
1790 allocation.width,
1791 allocation.height,
1792 child->user_data);
1793
Scott Moreauf7e498c2012-05-14 11:39:29 -06001794 width = child->allocation.width + decoration_width;
1795 height = child->allocation.height + decoration_height;
1796
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001797 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1798
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001799 widget->window->input_region =
1800 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001801 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001802 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001803 shadow_margin, shadow_margin,
1804 width - 2 * shadow_margin,
1805 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001806 } else {
1807 wl_region_add(widget->window->input_region,
1808 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001809 }
1810
Scott Moreauf7e498c2012-05-14 11:39:29 -06001811 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001812
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02001813 if (child->opaque)
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001814 wl_region_add(widget->window->opaque_region,
1815 opaque_margin, opaque_margin,
1816 widget->allocation.width - 2 * opaque_margin,
1817 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02001818
1819 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001820 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1821 x_l = t->width + shadow_margin;
1822 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001823 wl_list_for_each(button, &frame->buttons_list, link) {
1824 const int button_padding = 4;
1825 w = cairo_image_surface_get_width(button->icon);
1826 h = cairo_image_surface_get_height(button->icon);
1827
1828 if (button->decoration == FRAME_BUTTON_FANCY)
1829 w += 10;
1830
1831 if (button->align == FRAME_BUTTON_LEFT) {
1832 widget_set_allocation(button->widget,
1833 x_l, y , w + 1, h + 1);
1834 x_l += w;
1835 x_l += button_padding;
1836 } else {
1837 x_r -= w;
1838 widget_set_allocation(button->widget,
1839 x_r, y , w + 1, h + 1);
1840 x_r -= button_padding;
1841 }
1842 }
1843}
1844
1845static int
1846frame_button_enter_handler(struct widget *widget,
1847 struct input *input, float x, float y, void *data)
1848{
1849 struct frame_button *frame_button = data;
1850
1851 widget_schedule_redraw(frame_button->widget);
1852 frame_button->state = FRAME_BUTTON_OVER;
1853
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001854 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001855}
1856
1857static void
1858frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1859{
1860 struct frame_button *frame_button = data;
1861
1862 widget_schedule_redraw(frame_button->widget);
1863 frame_button->state = FRAME_BUTTON_DEFAULT;
1864}
1865
1866static void
1867frame_button_button_handler(struct widget *widget,
1868 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001869 uint32_t button,
1870 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001871{
1872 struct frame_button *frame_button = data;
1873 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001874 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001875
1876 if (button != BTN_LEFT)
1877 return;
1878
1879 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001880 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001881 frame_button->state = FRAME_BUTTON_ACTIVE;
1882 widget_schedule_redraw(frame_button->widget);
1883
1884 if (frame_button->type == FRAME_BUTTON_ICON)
1885 window_show_frame_menu(window, input, time);
1886 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001887 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001888 frame_button->state = FRAME_BUTTON_DEFAULT;
1889 widget_schedule_redraw(frame_button->widget);
1890 break;
1891 }
1892
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001893 if (!was_pressed)
1894 return;
1895
Martin Minarik1998b152012-05-10 02:04:35 +02001896 switch (frame_button->type) {
1897 case FRAME_BUTTON_CLOSE:
1898 if (window->close_handler)
1899 window->close_handler(window->parent,
1900 window->user_data);
1901 else
1902 display_exit(window->display);
1903 break;
1904 case FRAME_BUTTON_MINIMIZE:
1905 fprintf(stderr,"Minimize stub\n");
1906 break;
1907 case FRAME_BUTTON_MAXIMIZE:
1908 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1909 break;
1910 default:
1911 /* Unknown operation */
1912 break;
1913 }
1914}
1915
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001916static int
1917frame_button_motion_handler(struct widget *widget,
1918 struct input *input, uint32_t time,
1919 float x, float y, void *data)
1920{
1921 struct frame_button *frame_button = data;
1922 enum frame_button_pointer previous_button_state = frame_button->state;
1923
1924 /* only track state for a pressed button */
1925 if (input->grab != widget)
1926 return CURSOR_LEFT_PTR;
1927
1928 if (x > widget->allocation.x &&
1929 x < (widget->allocation.x + widget->allocation.width) &&
1930 y > widget->allocation.y &&
1931 y < (widget->allocation.y + widget->allocation.height)) {
1932 frame_button->state = FRAME_BUTTON_ACTIVE;
1933 } else {
1934 frame_button->state = FRAME_BUTTON_DEFAULT;
1935 }
1936
1937 if (frame_button->state != previous_button_state)
1938 widget_schedule_redraw(frame_button->widget);
1939
1940 return CURSOR_LEFT_PTR;
1941}
1942
Martin Minarik1998b152012-05-10 02:04:35 +02001943static void
1944frame_button_redraw_handler(struct widget *widget, void *data)
1945{
1946 struct frame_button *frame_button = data;
1947 cairo_t *cr;
1948 int width, height, x, y;
1949 struct window *window = widget->window;
1950
1951 x = widget->allocation.x;
1952 y = widget->allocation.y;
1953 width = widget->allocation.width;
1954 height = widget->allocation.height;
1955
1956 if (!width)
1957 return;
1958 if (!height)
1959 return;
1960 if (widget->opaque)
1961 return;
1962
1963 cr = cairo_create(window->cairo_surface);
1964
1965 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1966 cairo_set_line_width(cr, 1);
1967
1968 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1969 cairo_rectangle (cr, x, y, 25, 16);
1970
1971 cairo_stroke_preserve(cr);
1972
1973 switch (frame_button->state) {
1974 case FRAME_BUTTON_DEFAULT:
1975 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1976 break;
1977 case FRAME_BUTTON_OVER:
1978 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1979 break;
1980 case FRAME_BUTTON_ACTIVE:
1981 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1982 break;
1983 }
1984
1985 cairo_fill (cr);
1986
1987 x += 4;
1988 }
1989
1990 cairo_set_source_surface(cr, frame_button->icon, x, y);
1991 cairo_paint(cr);
1992
1993 cairo_destroy(cr);
1994}
1995
1996static struct widget *
1997frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1998 enum frame_button_align align, enum frame_button_decoration style)
1999{
2000 struct frame_button *frame_button;
2001 const char *icon = data;
2002
2003 frame_button = malloc (sizeof *frame_button);
2004 memset(frame_button, 0, sizeof *frame_button);
2005
2006 frame_button->icon = cairo_image_surface_create_from_png(icon);
2007 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2008 frame_button->frame = frame;
2009 frame_button->type = type;
2010 frame_button->align = align;
2011 frame_button->decoration = style;
2012
2013 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2014
2015 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2016 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2017 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2018 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002019 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002020 return frame_button->widget;
2021}
2022
2023static void
2024frame_button_destroy(struct frame_button *frame_button)
2025{
2026 widget_destroy(frame_button->widget);
2027 wl_list_remove(&frame_button->link);
2028 cairo_surface_destroy(frame_button->icon);
2029 free(frame_button);
2030
2031 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032}
2033
2034static void
2035frame_redraw_handler(struct widget *widget, void *data)
2036{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002037 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002038 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002039 struct theme *t = window->display->theme;
2040 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002041
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002042 if (window->type == TYPE_FULLSCREEN)
2043 return;
2044
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002045 cr = cairo_create(window->cairo_surface);
2046
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002047 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002048 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002049 if (window->type == TYPE_MAXIMIZED)
2050 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002051 theme_render_frame(t, cr, widget->allocation.width,
2052 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002053
2054 cairo_destroy(cr);
2055}
2056
2057static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002058frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002059{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002060 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002061 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002062 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002063
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002064 location = theme_get_location(t, input->sx, input->sy,
2065 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002066 frame->widget->allocation.height,
2067 window->type == TYPE_MAXIMIZED ?
2068 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002069
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002070 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002071 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002072 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002073 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002074 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002075 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002076 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002077 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002078 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002079 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002080 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002081 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002082 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002083 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002084 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002085 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002086 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002087 case THEME_LOCATION_EXTERIOR:
2088 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002089 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002090 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002091 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002092}
2093
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002094static void
2095frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002096{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002097 struct display *display;
2098
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002099 switch (index) {
2100 case 0: /* close */
2101 if (window->close_handler)
2102 window->close_handler(window->parent,
2103 window->user_data);
2104 else
2105 display_exit(window->display);
2106 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002107 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002108 display = window->display;
2109 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002110 workspace_manager_move_surface(
2111 display->workspace_manager,
2112 window->main_surface->surface,
2113 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002114 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002115 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002116 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002117 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002118 workspace_manager_move_surface(
2119 display->workspace_manager,
2120 window->main_surface->surface,
2121 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002122 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002123 case 3: /* fullscreen */
2124 /* we don't have a way to get out of fullscreen for now */
2125 if (window->fullscreen_handler)
2126 window->fullscreen_handler(window, window->user_data);
2127 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128 }
2129}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002130
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002131void
2132window_show_frame_menu(struct window *window,
2133 struct input *input, uint32_t time)
2134{
2135 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002136 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002137
2138 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002139 "Close",
2140 "Move to workspace above", "Move to workspace below",
2141 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002142 };
2143
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002144 if (window->fullscreen_handler)
2145 count = ARRAY_LENGTH(entries);
2146 else
2147 count = ARRAY_LENGTH(entries) - 1;
2148
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002149 input_get_position(input, &x, &y);
2150 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002151 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002152}
2153
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002154static int
2155frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002156 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157{
2158 return frame_get_pointer_image_for_location(data, input);
2159}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002160
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002161static int
2162frame_motion_handler(struct widget *widget,
2163 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002164 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002165{
2166 return frame_get_pointer_image_for_location(data, input);
2167}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002168
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170frame_button_handler(struct widget *widget,
2171 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002172 uint32_t button, enum wl_pointer_button_state state,
2173 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174
2175{
2176 struct frame *frame = data;
2177 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002178 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002180
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002181 location = theme_get_location(display->theme, input->sx, input->sy,
2182 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002183 frame->widget->allocation.height,
2184 window->type == TYPE_MAXIMIZED ?
2185 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186
Daniel Stone4dbadb12012-05-30 16:31:51 +01002187 if (window->display->shell && button == BTN_LEFT &&
2188 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002190 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191 if (!window->shell_surface)
2192 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002193 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002195 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002196 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002198 case THEME_LOCATION_RESIZING_TOP:
2199 case THEME_LOCATION_RESIZING_BOTTOM:
2200 case THEME_LOCATION_RESIZING_LEFT:
2201 case THEME_LOCATION_RESIZING_RIGHT:
2202 case THEME_LOCATION_RESIZING_TOP_LEFT:
2203 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2204 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2205 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002206 if (!window->shell_surface)
2207 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002208 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002209
Pekka Paalanen99436862012-11-19 17:15:59 +02002210 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002212 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002213 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214 break;
2215 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002216 } else if (button == BTN_RIGHT &&
2217 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002218 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219 }
2220}
2221
2222struct widget *
2223frame_create(struct window *window, void *data)
2224{
2225 struct frame *frame;
2226
2227 frame = malloc(sizeof *frame);
2228 memset(frame, 0, sizeof *frame);
2229
2230 frame->widget = window_add_widget(window, frame);
2231 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002232
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002233 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2234 widget_set_resize_handler(frame->widget, frame_resize_handler);
2235 widget_set_enter_handler(frame->widget, frame_enter_handler);
2236 widget_set_motion_handler(frame->widget, frame_motion_handler);
2237 widget_set_button_handler(frame->widget, frame_button_handler);
2238
Martin Minarik1998b152012-05-10 02:04:35 +02002239 /* Create empty list for frame buttons */
2240 wl_list_init(&frame->buttons_list);
2241
2242 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2243 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2244
2245 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2246 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2247
2248 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2249 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2250
2251 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2252 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2253
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002254 window->frame = frame;
2255
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002256 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002257}
2258
Kristian Høgsberga1627922012-06-20 17:30:03 -04002259void
2260frame_set_child_size(struct widget *widget, int child_width, int child_height)
2261{
2262 struct display *display = widget->window->display;
2263 struct theme *t = display->theme;
2264 int decoration_width, decoration_height;
2265 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002266 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002267
2268 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002269 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002270 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002271 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002272
2273 width = child_width + decoration_width;
2274 height = child_height + decoration_height;
2275 } else {
2276 width = child_width;
2277 height = child_height;
2278 }
2279
2280 window_schedule_resize(widget->window, width, height);
2281}
2282
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002283static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002284frame_destroy(struct frame *frame)
2285{
Martin Minarik1998b152012-05-10 02:04:35 +02002286 struct frame_button *button, *tmp;
2287
2288 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2289 frame_button_destroy(button);
2290
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002291 /* frame->child must be destroyed by the application */
2292 widget_destroy(frame->widget);
2293 free(frame);
2294}
2295
2296static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002297input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002298 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002299{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002300 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002301 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002302
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002303 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002304 return;
2305
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002306 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002307 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002308 widget = old;
2309 if (input->grab)
2310 widget = input->grab;
2311 if (widget->leave_handler)
2312 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002313 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002314 }
2315
2316 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002317 widget = focus;
2318 if (input->grab)
2319 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002320 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002321 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002322 cursor = widget->enter_handler(focus, input, x, y,
2323 widget->user_data);
2324 else
2325 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002326
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002327 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002328 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002329}
2330
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002331void
2332input_grab(struct input *input, struct widget *widget, uint32_t button)
2333{
2334 input->grab = widget;
2335 input->grab_button = button;
2336}
2337
2338void
2339input_ungrab(struct input *input)
2340{
2341 struct widget *widget;
2342
2343 input->grab = NULL;
2344 if (input->pointer_focus) {
2345 widget = widget_find_widget(input->pointer_focus->widget,
2346 input->sx, input->sy);
2347 input_set_focus_widget(input, widget, input->sx, input->sy);
2348 }
2349}
2350
2351static void
2352input_remove_pointer_focus(struct input *input)
2353{
2354 struct window *window = input->pointer_focus;
2355
2356 if (!window)
2357 return;
2358
2359 input_set_focus_widget(input, NULL, 0, 0);
2360
2361 input->pointer_focus = NULL;
2362 input->current_cursor = CURSOR_UNSET;
2363}
2364
2365static void
2366pointer_handle_enter(void *data, struct wl_pointer *pointer,
2367 uint32_t serial, struct wl_surface *surface,
2368 wl_fixed_t sx_w, wl_fixed_t sy_w)
2369{
2370 struct input *input = data;
2371 struct window *window;
2372 struct widget *widget;
2373 float sx = wl_fixed_to_double(sx_w);
2374 float sy = wl_fixed_to_double(sy_w);
2375
2376 if (!surface) {
2377 /* enter event for a window we've just destroyed */
2378 return;
2379 }
2380
2381 input->display->serial = serial;
2382 input->pointer_enter_serial = serial;
2383 input->pointer_focus = wl_surface_get_user_data(surface);
2384 window = input->pointer_focus;
2385
Pekka Paalanen99436862012-11-19 17:15:59 +02002386 if (window->resizing) {
2387 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002388 /* Schedule a redraw to free the pool */
2389 window_schedule_redraw(window);
2390 }
2391
2392 input->sx = sx;
2393 input->sy = sy;
2394
2395 widget = widget_find_widget(window->widget, sx, sy);
2396 input_set_focus_widget(input, widget, sx, sy);
2397}
2398
2399static void
2400pointer_handle_leave(void *data, struct wl_pointer *pointer,
2401 uint32_t serial, struct wl_surface *surface)
2402{
2403 struct input *input = data;
2404
2405 input->display->serial = serial;
2406 input_remove_pointer_focus(input);
2407}
2408
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002409static void
Daniel Stone37816df2012-05-16 18:45:18 +01002410pointer_handle_motion(void *data, struct wl_pointer *pointer,
2411 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002412{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002413 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002414 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002415 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002416 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002417 float sx = wl_fixed_to_double(sx_w);
2418 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002419
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002420 input->sx = sx;
2421 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002422
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002423 if (!window)
2424 return;
2425
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002426 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002427 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002428 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002429 }
2430
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002431 if (input->grab)
2432 widget = input->grab;
2433 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002434 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002435 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002436 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002437 input, time, sx, sy,
2438 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002439 else
2440 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002441
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002442 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002443}
2444
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002445static void
Daniel Stone37816df2012-05-16 18:45:18 +01002446pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002447 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002448{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002449 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002450 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002451 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002452
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002453 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002454 if (input->focus_widget && input->grab == NULL &&
2455 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002456 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002457
Neil Roberts6b28aad2012-01-23 19:11:18 +00002458 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002460 (*widget->button_handler)(widget,
2461 input, time,
2462 button, state,
2463 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002464
Daniel Stone4dbadb12012-05-30 16:31:51 +01002465 if (input->grab && input->grab_button == button &&
2466 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002467 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002468}
2469
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002470static void
Daniel Stone37816df2012-05-16 18:45:18 +01002471pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002472 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002473{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002474 struct input *input = data;
2475 struct widget *widget;
2476
2477 widget = input->focus_widget;
2478 if (input->grab)
2479 widget = input->grab;
2480 if (widget && widget->axis_handler)
2481 (*widget->axis_handler)(widget,
2482 input, time,
2483 axis, value,
2484 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002485}
2486
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002487static const struct wl_pointer_listener pointer_listener = {
2488 pointer_handle_enter,
2489 pointer_handle_leave,
2490 pointer_handle_motion,
2491 pointer_handle_button,
2492 pointer_handle_axis,
2493};
2494
2495static void
2496input_remove_keyboard_focus(struct input *input)
2497{
2498 struct window *window = input->keyboard_focus;
2499 struct itimerspec its;
2500
2501 its.it_interval.tv_sec = 0;
2502 its.it_interval.tv_nsec = 0;
2503 its.it_value.tv_sec = 0;
2504 its.it_value.tv_nsec = 0;
2505 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2506
2507 if (!window)
2508 return;
2509
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002510 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002511 if (window->keyboard_focus_handler)
2512 (*window->keyboard_focus_handler)(window, NULL,
2513 window->user_data);
2514
2515 input->keyboard_focus = NULL;
2516}
2517
2518static void
2519keyboard_repeat_func(struct task *task, uint32_t events)
2520{
2521 struct input *input =
2522 container_of(task, struct input, repeat_task);
2523 struct window *window = input->keyboard_focus;
2524 uint64_t exp;
2525
2526 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2527 /* If we change the timer between the fd becoming
2528 * readable and getting here, there'll be nothing to
2529 * read and we get EAGAIN. */
2530 return;
2531
2532 if (window && window->key_handler) {
2533 (*window->key_handler)(window, input, input->repeat_time,
2534 input->repeat_key, input->repeat_sym,
2535 WL_KEYBOARD_KEY_STATE_PRESSED,
2536 window->user_data);
2537 }
2538}
2539
2540static void
2541keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2542 uint32_t format, int fd, uint32_t size)
2543{
2544 struct input *input = data;
2545 char *map_str;
2546
2547 if (!data) {
2548 close(fd);
2549 return;
2550 }
2551
2552 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2553 close(fd);
2554 return;
2555 }
2556
2557 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2558 if (map_str == MAP_FAILED) {
2559 close(fd);
2560 return;
2561 }
2562
2563 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2564 map_str,
2565 XKB_KEYMAP_FORMAT_TEXT_V1,
2566 0);
2567 munmap(map_str, size);
2568 close(fd);
2569
2570 if (!input->xkb.keymap) {
2571 fprintf(stderr, "failed to compile keymap\n");
2572 return;
2573 }
2574
2575 input->xkb.state = xkb_state_new(input->xkb.keymap);
2576 if (!input->xkb.state) {
2577 fprintf(stderr, "failed to create XKB state\n");
2578 xkb_map_unref(input->xkb.keymap);
2579 input->xkb.keymap = NULL;
2580 return;
2581 }
2582
2583 input->xkb.control_mask =
2584 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2585 input->xkb.alt_mask =
2586 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2587 input->xkb.shift_mask =
2588 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2589}
2590
2591static void
2592keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2593 uint32_t serial, struct wl_surface *surface,
2594 struct wl_array *keys)
2595{
2596 struct input *input = data;
2597 struct window *window;
2598
2599 input->display->serial = serial;
2600 input->keyboard_focus = wl_surface_get_user_data(surface);
2601
2602 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002603 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002604 if (window->keyboard_focus_handler)
2605 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002606 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002607}
2608
2609static void
2610keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2611 uint32_t serial, struct wl_surface *surface)
2612{
2613 struct input *input = data;
2614
2615 input->display->serial = serial;
2616 input_remove_keyboard_focus(input);
2617}
2618
Scott Moreau210d0792012-03-22 10:47:01 -06002619static void
Daniel Stone37816df2012-05-16 18:45:18 +01002620keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002621 uint32_t serial, uint32_t time, uint32_t key,
2622 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002623{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002624 struct input *input = data;
2625 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002626 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002627 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002628 const xkb_keysym_t *syms;
2629 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002630 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002631
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002632 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002633 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002634 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002635 return;
2636
Daniel Stone97f68542012-05-30 16:32:01 +01002637 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002638
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002639 sym = XKB_KEY_NoSymbol;
2640 if (num_syms == 1)
2641 sym = syms[0];
2642
2643 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002644 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002645 window_set_maximized(window,
2646 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002647 } else if (sym == XKB_KEY_F11 &&
2648 window->fullscreen_handler &&
2649 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2650 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002651 } else if (sym == XKB_KEY_F4 &&
2652 input->modifiers == MOD_ALT_MASK &&
2653 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2654 if (window->close_handler)
2655 window->close_handler(window->parent,
2656 window->user_data);
2657 else
2658 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002659 } else if (window->key_handler) {
2660 (*window->key_handler)(window, input, time, key,
2661 sym, state, window->user_data);
2662 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002663
2664 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2665 key == input->repeat_key) {
2666 its.it_interval.tv_sec = 0;
2667 its.it_interval.tv_nsec = 0;
2668 its.it_value.tv_sec = 0;
2669 its.it_value.tv_nsec = 0;
2670 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2671 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2672 input->repeat_sym = sym;
2673 input->repeat_key = key;
2674 input->repeat_time = time;
2675 its.it_interval.tv_sec = 0;
2676 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2677 its.it_value.tv_sec = 0;
2678 its.it_value.tv_nsec = 400 * 1000 * 1000;
2679 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2680 }
2681}
2682
2683static void
Daniel Stone351eb612012-05-31 15:27:47 -04002684keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2685 uint32_t serial, uint32_t mods_depressed,
2686 uint32_t mods_latched, uint32_t mods_locked,
2687 uint32_t group)
2688{
2689 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002690 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002691
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002692 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2693 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002694 mask = xkb_state_serialize_mods(input->xkb.state,
2695 XKB_STATE_DEPRESSED |
2696 XKB_STATE_LATCHED);
2697 input->modifiers = 0;
2698 if (mask & input->xkb.control_mask)
2699 input->modifiers |= MOD_CONTROL_MASK;
2700 if (mask & input->xkb.alt_mask)
2701 input->modifiers |= MOD_ALT_MASK;
2702 if (mask & input->xkb.shift_mask)
2703 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002704}
2705
Daniel Stone37816df2012-05-16 18:45:18 +01002706static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002707 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002708 keyboard_handle_enter,
2709 keyboard_handle_leave,
2710 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002711 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002712};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002713
2714static void
Daniel Stone37816df2012-05-16 18:45:18 +01002715seat_handle_capabilities(void *data, struct wl_seat *seat,
2716 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002717{
Daniel Stone37816df2012-05-16 18:45:18 +01002718 struct input *input = data;
2719
2720 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2721 input->pointer = wl_seat_get_pointer(seat);
2722 wl_pointer_set_user_data(input->pointer, input);
2723 wl_pointer_add_listener(input->pointer, &pointer_listener,
2724 input);
2725 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2726 wl_pointer_destroy(input->pointer);
2727 input->pointer = NULL;
2728 }
2729
2730 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2731 input->keyboard = wl_seat_get_keyboard(seat);
2732 wl_keyboard_set_user_data(input->keyboard, input);
2733 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2734 input);
2735 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2736 wl_keyboard_destroy(input->keyboard);
2737 input->keyboard = NULL;
2738 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002739}
2740
Daniel Stone37816df2012-05-16 18:45:18 +01002741static const struct wl_seat_listener seat_listener = {
2742 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002743};
2744
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002745void
2746input_get_position(struct input *input, int32_t *x, int32_t *y)
2747{
2748 *x = input->sx;
2749 *y = input->sy;
2750}
2751
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002752struct display *
2753input_get_display(struct input *input)
2754{
2755 return input->display;
2756}
2757
Daniel Stone37816df2012-05-16 18:45:18 +01002758struct wl_seat *
2759input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002760{
Daniel Stone37816df2012-05-16 18:45:18 +01002761 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002762}
2763
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002764uint32_t
2765input_get_modifiers(struct input *input)
2766{
2767 return input->modifiers;
2768}
2769
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002770struct widget *
2771input_get_focus_widget(struct input *input)
2772{
2773 return input->focus_widget;
2774}
2775
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002776struct data_offer {
2777 struct wl_data_offer *offer;
2778 struct input *input;
2779 struct wl_array types;
2780 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002781
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002782 struct task io_task;
2783 int fd;
2784 data_func_t func;
2785 int32_t x, y;
2786 void *user_data;
2787};
2788
2789static void
2790data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2791{
2792 struct data_offer *offer = data;
2793 char **p;
2794
2795 p = wl_array_add(&offer->types, sizeof *p);
2796 *p = strdup(type);
2797}
2798
2799static const struct wl_data_offer_listener data_offer_listener = {
2800 data_offer_offer,
2801};
2802
2803static void
2804data_offer_destroy(struct data_offer *offer)
2805{
2806 char **p;
2807
2808 offer->refcount--;
2809 if (offer->refcount == 0) {
2810 wl_data_offer_destroy(offer->offer);
2811 for (p = offer->types.data; *p; p++)
2812 free(*p);
2813 wl_array_release(&offer->types);
2814 free(offer);
2815 }
2816}
2817
2818static void
2819data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002820 struct wl_data_device *data_device,
2821 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002822{
2823 struct data_offer *offer;
2824
2825 offer = malloc(sizeof *offer);
2826
2827 wl_array_init(&offer->types);
2828 offer->refcount = 1;
2829 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002830 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002831 wl_data_offer_add_listener(offer->offer,
2832 &data_offer_listener, offer);
2833}
2834
2835static void
2836data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002837 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002838 wl_fixed_t x_w, wl_fixed_t y_w,
2839 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002840{
2841 struct input *input = data;
2842 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002843 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002844 float x = wl_fixed_to_double(x_w);
2845 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002846 char **p;
2847
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002848 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002849 window = wl_surface_get_user_data(surface);
2850 input->pointer_focus = window;
2851
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002852 if (offer) {
2853 input->drag_offer = wl_data_offer_get_user_data(offer);
2854
2855 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2856 *p = NULL;
2857
2858 types_data = input->drag_offer->types.data;
2859 } else {
2860 input->drag_offer = NULL;
2861 types_data = NULL;
2862 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002863
2864 window = input->pointer_focus;
2865 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002866 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002867 window->user_data);
2868}
2869
2870static void
2871data_device_leave(void *data, struct wl_data_device *data_device)
2872{
2873 struct input *input = data;
2874
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002875 if (input->drag_offer) {
2876 data_offer_destroy(input->drag_offer);
2877 input->drag_offer = NULL;
2878 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002879}
2880
2881static void
2882data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002883 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002884{
2885 struct input *input = data;
2886 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002887 float x = wl_fixed_to_double(x_w);
2888 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002889 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002890
2891 input->sx = x;
2892 input->sy = y;
2893
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002894 if (input->drag_offer)
2895 types_data = input->drag_offer->types.data;
2896 else
2897 types_data = NULL;
2898
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002899 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002900 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002901 window->user_data);
2902}
2903
2904static void
2905data_device_drop(void *data, struct wl_data_device *data_device)
2906{
2907 struct input *input = data;
2908 struct window *window = input->pointer_focus;
2909
2910 if (window->drop_handler)
2911 window->drop_handler(window, input,
2912 input->sx, input->sy, window->user_data);
2913}
2914
2915static void
2916data_device_selection(void *data,
2917 struct wl_data_device *wl_data_device,
2918 struct wl_data_offer *offer)
2919{
2920 struct input *input = data;
2921 char **p;
2922
2923 if (input->selection_offer)
2924 data_offer_destroy(input->selection_offer);
2925
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002926 if (offer) {
2927 input->selection_offer = wl_data_offer_get_user_data(offer);
2928 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2929 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002930 } else {
2931 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002932 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002933}
2934
2935static const struct wl_data_device_listener data_device_listener = {
2936 data_device_data_offer,
2937 data_device_enter,
2938 data_device_leave,
2939 data_device_motion,
2940 data_device_drop,
2941 data_device_selection
2942};
2943
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002944static void
2945input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002946{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002947 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002948 struct wl_cursor *cursor;
2949 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002950
Daniel Stone80972742012-11-07 17:51:39 +11002951 if (!input->pointer)
2952 return;
2953
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002954 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002955 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002956 return;
2957
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002958 if (index >= (int) cursor->image_count) {
2959 fprintf(stderr, "cursor index out of range\n");
2960 return;
2961 }
2962
2963 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002964 buffer = wl_cursor_image_get_buffer(image);
2965 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002966 return;
2967
Kristian Høgsbergae277372012-08-01 09:41:08 -04002968 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002969 input->pointer_surface,
2970 image->hotspot_x, image->hotspot_y);
2971 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2972 wl_surface_damage(input->pointer_surface, 0, 0,
2973 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002974 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002975}
2976
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002977static const struct wl_callback_listener pointer_surface_listener;
2978
2979static void
2980pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2981 uint32_t time)
2982{
2983 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002984 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002985 int i;
2986
2987 if (callback) {
2988 assert(callback == input->cursor_frame_cb);
2989 wl_callback_destroy(callback);
2990 input->cursor_frame_cb = NULL;
2991 }
2992
Daniel Stone80972742012-11-07 17:51:39 +11002993 if (!input->pointer)
2994 return;
2995
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002996 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002997 wl_pointer_set_cursor(input->pointer,
2998 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002999 NULL, 0, 0);
3000 return;
3001 }
3002
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003003 if (input->current_cursor == CURSOR_UNSET)
3004 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003005 cursor = input->display->cursors[input->current_cursor];
3006 if (!cursor)
3007 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003008
3009 /* FIXME We don't have the current time on the first call so we set
3010 * the animation start to the time of the first frame callback. */
3011 if (time == 0)
3012 input->cursor_anim_start = 0;
3013 else if (input->cursor_anim_start == 0)
3014 input->cursor_anim_start = time;
3015
3016 if (time == 0 || input->cursor_anim_start == 0)
3017 i = 0;
3018 else
3019 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3020
Pekka Paalanenbc106382012-10-10 12:49:31 +03003021 if (cursor->image_count > 1) {
3022 input->cursor_frame_cb =
3023 wl_surface_frame(input->pointer_surface);
3024 wl_callback_add_listener(input->cursor_frame_cb,
3025 &pointer_surface_listener, input);
3026 }
3027
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003028 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003029}
3030
3031static const struct wl_callback_listener pointer_surface_listener = {
3032 pointer_surface_frame_callback
3033};
3034
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003035void
3036input_set_pointer_image(struct input *input, int pointer)
3037{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003038 int force = 0;
3039
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003040 if (!input->pointer)
3041 return;
3042
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003043 if (input->pointer_enter_serial > input->cursor_serial)
3044 force = 1;
3045
3046 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003047 return;
3048
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003049 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003050 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003051 if (!input->cursor_frame_cb)
3052 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003053 else if (force) {
3054 /* The current frame callback may be stuck if, for instance,
3055 * the set cursor request was processed by the server after
3056 * this client lost the focus. In this case the cursor surface
3057 * might not be mapped and the frame callback wouldn't ever
3058 * complete. Send a set_cursor and attach to try to map the
3059 * cursor surface again so that the callback will finish */
3060 input_set_pointer_image_index(input, 0);
3061 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003062}
3063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003064struct wl_data_device *
3065input_get_data_device(struct input *input)
3066{
3067 return input->data_device;
3068}
3069
3070void
3071input_set_selection(struct input *input,
3072 struct wl_data_source *source, uint32_t time)
3073{
3074 wl_data_device_set_selection(input->data_device, source, time);
3075}
3076
3077void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003078input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003079{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003080 wl_data_offer_accept(input->drag_offer->offer,
3081 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003082}
3083
3084static void
3085offer_io_func(struct task *task, uint32_t events)
3086{
3087 struct data_offer *offer =
3088 container_of(task, struct data_offer, io_task);
3089 unsigned int len;
3090 char buffer[4096];
3091
3092 len = read(offer->fd, buffer, sizeof buffer);
3093 offer->func(buffer, len,
3094 offer->x, offer->y, offer->user_data);
3095
3096 if (len == 0) {
3097 close(offer->fd);
3098 data_offer_destroy(offer);
3099 }
3100}
3101
3102static void
3103data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3104 data_func_t func, void *user_data)
3105{
3106 int p[2];
3107
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003108 if (pipe2(p, O_CLOEXEC) == -1)
3109 return;
3110
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003111 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3112 close(p[1]);
3113
3114 offer->io_task.run = offer_io_func;
3115 offer->fd = p[0];
3116 offer->func = func;
3117 offer->refcount++;
3118 offer->user_data = user_data;
3119
3120 display_watch_fd(offer->input->display,
3121 offer->fd, EPOLLIN, &offer->io_task);
3122}
3123
3124void
3125input_receive_drag_data(struct input *input, const char *mime_type,
3126 data_func_t func, void *data)
3127{
3128 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3129 input->drag_offer->x = input->sx;
3130 input->drag_offer->y = input->sy;
3131}
3132
3133int
3134input_receive_selection_data(struct input *input, const char *mime_type,
3135 data_func_t func, void *data)
3136{
3137 char **p;
3138
3139 if (input->selection_offer == NULL)
3140 return -1;
3141
3142 for (p = input->selection_offer->types.data; *p; p++)
3143 if (strcmp(mime_type, *p) == 0)
3144 break;
3145
3146 if (*p == NULL)
3147 return -1;
3148
3149 data_offer_receive_data(input->selection_offer,
3150 mime_type, func, data);
3151 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003152}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003153
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003154int
3155input_receive_selection_data_to_fd(struct input *input,
3156 const char *mime_type, int fd)
3157{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003158 if (input->selection_offer)
3159 wl_data_offer_receive(input->selection_offer->offer,
3160 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003161
3162 return 0;
3163}
3164
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003165void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003166window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003167{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003168 if (!window->shell_surface)
3169 return;
3170
Daniel Stone37816df2012-05-16 18:45:18 +01003171 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003172}
3173
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003174static void
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003175surface_resize(struct surface *surface, struct widget *widget)
3176{
3177 if (surface->allocation.width != widget->allocation.width ||
3178 surface->allocation.height != widget->allocation.height) {
3179 surface->allocation = widget->allocation;
3180 window_schedule_redraw(surface->window);
3181 }
3182}
3183
3184static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003185idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003186{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003187 struct widget *widget;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003188 struct wl_compositor *compositor = window->display->compositor;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003189
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003190 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003191 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003192 widget_set_allocation(widget,
3193 window->pending_allocation.x,
3194 window->pending_allocation.y,
3195 window->pending_allocation.width,
3196 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003197
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003198 if (window->input_region) {
3199 wl_region_destroy(window->input_region);
3200 window->input_region = NULL;
3201 }
3202
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003203 if (window->opaque_region)
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003204 wl_region_destroy(window->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003205
3206 window->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003207
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003208 if (widget->resize_handler)
3209 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003210 widget->allocation.width,
3211 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003212 widget->user_data);
3213
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003214 surface_resize(window->main_surface, widget);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003215
3216 if (widget->opaque)
3217 wl_region_add(window->opaque_region, 0, 0,
3218 widget->allocation.width,
3219 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003220}
3221
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003222void
3223window_schedule_resize(struct window *window, int width, int height)
3224{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003225 window->pending_allocation.x = 0;
3226 window->pending_allocation.y = 0;
3227 window->pending_allocation.width = width;
3228 window->pending_allocation.height = height;
3229
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003230 if (window->min_allocation.width == 0)
3231 window->min_allocation = window->pending_allocation;
3232 if (window->pending_allocation.width < window->min_allocation.width)
3233 window->pending_allocation.width = window->min_allocation.width;
3234 if (window->pending_allocation.height < window->min_allocation.height)
3235 window->pending_allocation.height = window->min_allocation.height;
3236
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003237 window->resize_needed = 1;
3238 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003239}
3240
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003241void
3242widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3243{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003244 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003245}
3246
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003247static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003248handle_ping(void *data, struct wl_shell_surface *shell_surface,
3249 uint32_t serial)
3250{
3251 wl_shell_surface_pong(shell_surface, serial);
3252}
3253
3254static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003255handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003256 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003257{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003258 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003259
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003260 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003261 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003262}
3263
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003264static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003265menu_destroy(struct menu *menu)
3266{
3267 widget_destroy(menu->widget);
3268 window_destroy(menu->window);
3269 free(menu);
3270}
3271
3272static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003273handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3274{
3275 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003276 struct menu *menu = window->widget->user_data;
3277
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003278 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003279 * device. Or just use wl_callback. And this really needs to
3280 * be a window vfunc that the menu can set. And we need the
3281 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003282
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003283 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003284 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003285 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003286}
3287
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003288static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003289 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003290 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003291 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003292};
3293
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003294void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003295window_get_allocation(struct window *window,
3296 struct rectangle *allocation)
3297{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003298 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003299}
3300
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003301static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003302widget_redraw(struct widget *widget)
3303{
3304 struct widget *child;
3305
3306 if (widget->redraw_handler)
3307 widget->redraw_handler(widget, widget->user_data);
3308 wl_list_for_each(child, &widget->child_list, link)
3309 widget_redraw(child);
3310}
3311
3312static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003313frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3314{
3315 struct window *window = data;
3316
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003317 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003318 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003319 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003320 window->redraw_scheduled = 0;
3321 if (window->redraw_needed)
3322 window_schedule_redraw(window);
3323}
3324
3325static const struct wl_callback_listener listener = {
3326 frame_callback
3327};
3328
3329static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003330idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003331{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003332 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003333
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003334 if (window->resize_needed)
3335 idle_resize(window);
3336
Kristian Høgsberg5d129902012-01-10 10:49:41 -05003337 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003338 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003339 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003340 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003341
Pekka Paalanen4e373742013-02-13 16:17:13 +02003342 window->frame_cb = wl_surface_frame(window->main_surface->surface);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03003343 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003344 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003345}
3346
3347void
3348window_schedule_redraw(struct window *window)
3349{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003350 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003351 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003352 window->redraw_task.run = idle_redraw;
3353 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003354 window->redraw_scheduled = 1;
3355 }
3356}
3357
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003358int
3359window_is_fullscreen(struct window *window)
3360{
3361 return window->type == TYPE_FULLSCREEN;
3362}
3363
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003364void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003365window_set_fullscreen(struct window *window, int fullscreen)
3366{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003367 if (!window->display->shell)
3368 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003369
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003370 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003371 return;
3372
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003373 if (fullscreen) {
3374 window->type = TYPE_FULLSCREEN;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003375 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003376 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003377 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003378 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003379 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003380 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003381 wl_shell_surface_set_toplevel(window->shell_surface);
3382 window_schedule_resize(window,
3383 window->saved_allocation.width,
3384 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003385 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003386}
3387
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003388void
3389window_set_fullscreen_method(struct window *window,
3390 enum wl_shell_surface_fullscreen_method method)
3391{
3392 window->fullscreen_method = method;
3393}
3394
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003395int
3396window_is_maximized(struct window *window)
3397{
3398 return window->type == TYPE_MAXIMIZED;
3399}
3400
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003401void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003402window_set_maximized(struct window *window, int maximized)
3403{
3404 if (!window->display->shell)
3405 return;
3406
3407 if ((window->type == TYPE_MAXIMIZED) == maximized)
3408 return;
3409
3410 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003411 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003412 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3413 window->type = TYPE_MAXIMIZED;
3414 } else {
3415 wl_shell_surface_set_toplevel(window->shell_surface);
3416 window->type = TYPE_TOPLEVEL;
3417 window_schedule_resize(window,
3418 window->saved_allocation.width,
3419 window->saved_allocation.height);
3420 }
3421}
3422
3423void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003424window_set_user_data(struct window *window, void *data)
3425{
3426 window->user_data = data;
3427}
3428
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003429void *
3430window_get_user_data(struct window *window)
3431{
3432 return window->user_data;
3433}
3434
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003435void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003436window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003437 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003438{
3439 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003440}
3441
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003442void
3443window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003444 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003445{
3446 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003447}
3448
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003449void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003450window_set_data_handler(struct window *window, window_data_handler_t handler)
3451{
3452 window->data_handler = handler;
3453}
3454
3455void
3456window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3457{
3458 window->drop_handler = handler;
3459}
3460
3461void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003462window_set_close_handler(struct window *window,
3463 window_close_handler_t handler)
3464{
3465 window->close_handler = handler;
3466}
3467
3468void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003469window_set_fullscreen_handler(struct window *window,
3470 window_fullscreen_handler_t handler)
3471{
3472 window->fullscreen_handler = handler;
3473}
3474
3475void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003476window_set_output_handler(struct window *window,
3477 window_output_handler_t handler)
3478{
3479 window->output_handler = handler;
3480}
3481
3482void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003483window_set_title(struct window *window, const char *title)
3484{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003485 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003486 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003487 if (window->shell_surface)
3488 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003489}
3490
3491const char *
3492window_get_title(struct window *window)
3493{
3494 return window->title;
3495}
3496
3497void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003498window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3499{
3500 struct text_cursor_position *text_cursor_position =
3501 window->display->text_cursor_position;
3502
Scott Moreau9295ce02012-06-01 12:46:10 -06003503 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003504 return;
3505
3506 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003507 window->main_surface->surface,
3508 wl_fixed_from_int(x),
3509 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003510}
3511
3512void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003513window_damage(struct window *window, int32_t x, int32_t y,
3514 int32_t width, int32_t height)
3515{
Pekka Paalanen4e373742013-02-13 16:17:13 +02003516 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003517}
3518
Casey Dahlin9074db52012-04-19 22:50:09 -04003519static void
3520surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003521 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003522{
Rob Bradford7507b572012-05-15 17:55:34 +01003523 struct window *window = data;
3524 struct output *output;
3525 struct output *output_found = NULL;
3526 struct window_output *window_output;
3527
3528 wl_list_for_each(output, &window->display->output_list, link) {
3529 if (output->output == wl_output) {
3530 output_found = output;
3531 break;
3532 }
3533 }
3534
3535 if (!output_found)
3536 return;
3537
3538 window_output = malloc (sizeof *window_output);
3539 window_output->output = output_found;
3540
3541 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003542
3543 if (window->output_handler)
3544 window->output_handler(window, output_found, 1,
3545 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04003546}
3547
3548static void
3549surface_leave(void *data,
3550 struct wl_surface *wl_surface, struct wl_output *output)
3551{
Rob Bradford7507b572012-05-15 17:55:34 +01003552 struct window *window = data;
3553 struct window_output *window_output;
3554 struct window_output *window_output_found = NULL;
3555
3556 wl_list_for_each(window_output, &window->window_output_list, link) {
3557 if (window_output->output->output == output) {
3558 window_output_found = window_output;
3559 break;
3560 }
3561 }
3562
3563 if (window_output_found) {
3564 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02003565
3566 if (window->output_handler)
3567 window->output_handler(window, window_output->output,
3568 0, window->user_data);
3569
Rob Bradford7507b572012-05-15 17:55:34 +01003570 free(window_output_found);
3571 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003572}
3573
3574static const struct wl_surface_listener surface_listener = {
3575 surface_enter,
3576 surface_leave
3577};
3578
Pekka Paalanen4e373742013-02-13 16:17:13 +02003579static struct surface *
3580surface_create(struct window *window)
3581{
3582 struct display *display = window->display;
3583 struct surface *surface;
3584
3585 surface = calloc(1, sizeof *surface);
3586 if (!surface)
3587 return NULL;
3588
3589 surface->window = window;
3590 surface->surface = wl_compositor_create_surface(display->compositor);
3591 wl_surface_add_listener(surface->surface, &surface_listener, window);
3592
3593 return surface;
3594}
3595
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003596static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003597window_create_internal(struct display *display,
3598 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003599{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003600 struct window *window;
3601
3602 window = malloc(sizeof *window);
3603 if (window == NULL)
3604 return NULL;
3605
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003606 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003607 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003608 window->parent = parent;
Pekka Paalanen4e373742013-02-13 16:17:13 +02003609 window->main_surface = surface_create(window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003610 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003611 window->shell_surface =
3612 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen4e373742013-02-13 16:17:13 +02003613 window->main_surface->surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003614 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003615
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003616 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003617 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003618 window->input_region = NULL;
3619 window->opaque_region = NULL;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003620 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003621
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05003622 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003623#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003624 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003625#else
3626 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3627#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003628 else
3629 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003630
Pekka Paalanen4e373742013-02-13 16:17:13 +02003631 wl_surface_set_user_data(window->main_surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003632 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003633 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003634
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003635 if (window->shell_surface) {
3636 wl_shell_surface_set_user_data(window->shell_surface, window);
3637 wl_shell_surface_add_listener(window->shell_surface,
3638 &shell_surface_listener, window);
3639 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003640
Rob Bradford7507b572012-05-15 17:55:34 +01003641 wl_list_init (&window->window_output_list);
3642
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003643 return window;
3644}
3645
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003646struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003647window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003648{
3649 struct window *window;
3650
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003651 window = window_create_internal(display, NULL, TYPE_NONE);
3652 if (!window)
3653 return NULL;
3654
3655 return window;
3656}
3657
3658struct window *
3659window_create_custom(struct display *display)
3660{
3661 struct window *window;
3662
3663 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003664 if (!window)
3665 return NULL;
3666
3667 return window;
3668}
3669
3670struct window *
3671window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003672 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003673{
3674 struct window *window;
3675
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003676 window = window_create_internal(parent->display,
3677 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003678 if (!window)
3679 return NULL;
3680
3681 window->x = x;
3682 window->y = y;
3683
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003684 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02003685 wl_shell_surface_set_transient(
3686 window->shell_surface,
3687 window->parent->main_surface->surface,
3688 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003689
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003690 return window;
3691}
3692
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003693static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003694menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003695{
3696 int next;
3697
3698 next = (sy - 8) / 20;
3699 if (menu->current != next) {
3700 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003701 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003702 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003703}
3704
3705static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003706menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003707 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003708 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003709{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003710 struct menu *menu = data;
3711
3712 if (widget == menu->widget)
3713 menu_set_item(data, y);
3714
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003715 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003716}
3717
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003718static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003719menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003720 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003721{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003722 struct menu *menu = data;
3723
3724 if (widget == menu->widget)
3725 menu_set_item(data, y);
3726
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003727 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003728}
3729
3730static void
3731menu_leave_handler(struct widget *widget, struct input *input, void *data)
3732{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003733 struct menu *menu = data;
3734
3735 if (widget == menu->widget)
3736 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003737}
3738
3739static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003740menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003741 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003742 uint32_t button, enum wl_pointer_button_state state,
3743 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003744
3745{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003746 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003747
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003748 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3749 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003750 /* Either relase after press-drag-release or
3751 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003752 menu->func(menu->window->parent,
3753 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003754 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003755 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003756 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003757 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04003758 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003759}
3760
3761static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003762menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003763{
3764 cairo_t *cr;
3765 const int32_t r = 3, margin = 3;
3766 struct menu *menu = data;
3767 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003768 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003769
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003770 cr = cairo_create(window->cairo_surface);
3771 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3772 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3773 cairo_paint(cr);
3774
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003775 width = window->main_surface->allocation.width;
3776 height = window->main_surface->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003777 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003778
3779 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003780 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3781 cairo_fill(cr);
3782
3783 for (i = 0; i < menu->count; i++) {
3784 if (i == menu->current) {
3785 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3786 cairo_rectangle(cr, margin, i * 20 + margin,
3787 width - 2 * margin, 20);
3788 cairo_fill(cr);
3789 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3790 cairo_move_to(cr, 10, i * 20 + 16);
3791 cairo_show_text(cr, menu->entries[i]);
3792 } else {
3793 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3794 cairo_move_to(cr, 10, i * 20 + 16);
3795 cairo_show_text(cr, menu->entries[i]);
3796 }
3797 }
3798
3799 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003800}
3801
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003802void
3803window_show_menu(struct display *display,
3804 struct input *input, uint32_t time, struct window *parent,
3805 int32_t x, int32_t y,
3806 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003807{
3808 struct window *window;
3809 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003810 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003811
3812 menu = malloc(sizeof *menu);
3813 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003814 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003815
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003816 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003817 if (!window) {
3818 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003819 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003820 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003821
3822 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003823 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003824 menu->entries = entries;
3825 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04003826 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003827 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003828 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003829 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003830 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003831 window->type = TYPE_MENU;
3832 window->x = x;
3833 window->y = y;
3834
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003835 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003836 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003837 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02003838 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003839 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003840
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003841 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003842 widget_set_enter_handler(menu->widget, menu_enter_handler);
3843 widget_set_leave_handler(menu->widget, menu_leave_handler);
3844 widget_set_motion_handler(menu->widget, menu_motion_handler);
3845 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003846
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003847 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003848 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003849}
3850
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003851void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003852window_set_buffer_type(struct window *window, enum window_buffer_type type)
3853{
3854 window->buffer_type = type;
3855}
3856
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003857
3858static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003859display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003860 struct wl_output *wl_output,
3861 int x, int y,
3862 int physical_width,
3863 int physical_height,
3864 int subpixel,
3865 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003866 const char *model,
3867 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003868{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003869 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003870
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003871 output->allocation.x = x;
3872 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003873 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003874}
3875
3876static void
3877display_handle_mode(void *data,
3878 struct wl_output *wl_output,
3879 uint32_t flags,
3880 int width,
3881 int height,
3882 int refresh)
3883{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003884 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003885 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003886
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003887 if (flags & WL_OUTPUT_MODE_CURRENT) {
3888 output->allocation.width = width;
3889 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003890 if (display->output_configure_handler)
3891 (*display->output_configure_handler)(
3892 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003893 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003894}
3895
3896static const struct wl_output_listener output_listener = {
3897 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003898 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003899};
3900
3901static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003902display_add_output(struct display *d, uint32_t id)
3903{
3904 struct output *output;
3905
3906 output = malloc(sizeof *output);
3907 if (output == NULL)
3908 return;
3909
3910 memset(output, 0, sizeof *output);
3911 output->display = d;
3912 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003913 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003914 wl_list_insert(d->output_list.prev, &output->link);
3915
3916 wl_output_add_listener(output->output, &output_listener, output);
3917}
3918
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003919static void
3920output_destroy(struct output *output)
3921{
3922 if (output->destroy_handler)
3923 (*output->destroy_handler)(output, output->user_data);
3924
3925 wl_output_destroy(output->output);
3926 wl_list_remove(&output->link);
3927 free(output);
3928}
3929
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003930void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003931display_set_global_handler(struct display *display,
3932 display_global_handler_t handler)
3933{
3934 struct global *global;
3935
3936 display->global_handler = handler;
3937 if (!handler)
3938 return;
3939
3940 wl_list_for_each(global, &display->global_list, link)
3941 display->global_handler(display,
3942 global->name, global->interface,
3943 global->version, display->user_data);
3944}
3945
3946void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003947display_set_output_configure_handler(struct display *display,
3948 display_output_handler_t handler)
3949{
3950 struct output *output;
3951
3952 display->output_configure_handler = handler;
3953 if (!handler)
3954 return;
3955
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003956 wl_list_for_each(output, &display->output_list, link) {
3957 if (output->allocation.width == 0 &&
3958 output->allocation.height == 0)
3959 continue;
3960
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003961 (*display->output_configure_handler)(output,
3962 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003963 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003964}
3965
3966void
3967output_set_user_data(struct output *output, void *data)
3968{
3969 output->user_data = data;
3970}
3971
3972void *
3973output_get_user_data(struct output *output)
3974{
3975 return output->user_data;
3976}
3977
3978void
3979output_set_destroy_handler(struct output *output,
3980 display_output_handler_t handler)
3981{
3982 output->destroy_handler = handler;
3983 /* FIXME: implement this, once we have way to remove outputs */
3984}
3985
3986void
Scott Moreau4e072362012-09-29 02:03:11 -06003987output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003988{
Scott Moreau4e072362012-09-29 02:03:11 -06003989 struct rectangle allocation = output->allocation;
3990
3991 switch (output->transform) {
3992 case WL_OUTPUT_TRANSFORM_90:
3993 case WL_OUTPUT_TRANSFORM_270:
3994 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3995 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3996 /* Swap width and height */
3997 allocation.width = output->allocation.height;
3998 allocation.height = output->allocation.width;
3999 break;
4000 }
4001
4002 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004003}
4004
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004005struct wl_output *
4006output_get_wl_output(struct output *output)
4007{
4008 return output->output;
4009}
4010
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004011enum wl_output_transform
4012output_get_transform(struct output *output)
4013{
4014 return output->transform;
4015}
4016
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004017static void
Daniel Stone97f68542012-05-30 16:32:01 +01004018fini_xkb(struct input *input)
4019{
4020 xkb_state_unref(input->xkb.state);
4021 xkb_map_unref(input->xkb.keymap);
4022}
4023
4024static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004025display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004026{
4027 struct input *input;
4028
4029 input = malloc(sizeof *input);
4030 if (input == NULL)
4031 return;
4032
4033 memset(input, 0, sizeof *input);
4034 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004035 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004036 input->pointer_focus = NULL;
4037 input->keyboard_focus = NULL;
4038 wl_list_insert(d->input_list.prev, &input->link);
4039
Daniel Stone37816df2012-05-16 18:45:18 +01004040 wl_seat_add_listener(input->seat, &seat_listener, input);
4041 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004042
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004043 input->data_device =
4044 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004045 input->seat);
4046 wl_data_device_add_listener(input->data_device, &data_device_listener,
4047 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004048
4049 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004050
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004051 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4052 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004053 input->repeat_task.run = keyboard_repeat_func;
4054 display_watch_fd(d, input->repeat_timer_fd,
4055 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004056}
4057
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004058static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004059input_destroy(struct input *input)
4060{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004061 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004062 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004063
4064 if (input->drag_offer)
4065 data_offer_destroy(input->drag_offer);
4066
4067 if (input->selection_offer)
4068 data_offer_destroy(input->selection_offer);
4069
4070 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004071 fini_xkb(input);
4072
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004073 wl_surface_destroy(input->pointer_surface);
4074
Pekka Paalanene1207c72011-12-16 12:02:09 +02004075 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004076 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004077 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004078 free(input);
4079}
4080
4081static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004082init_workspace_manager(struct display *d, uint32_t id)
4083{
4084 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004085 wl_registry_bind(d->registry, id,
4086 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004087 if (d->workspace_manager != NULL)
4088 workspace_manager_add_listener(d->workspace_manager,
4089 &workspace_manager_listener,
4090 d);
4091}
4092
4093static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004094registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4095 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004096{
4097 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004098 struct global *global;
4099
4100 global = malloc(sizeof *global);
4101 global->name = id;
4102 global->interface = strdup(interface);
4103 global->version = version;
4104 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004105
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004106 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004107 d->compositor = wl_registry_bind(registry, id,
4108 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004109 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004110 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004111 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004112 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004113 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004114 d->shell = wl_registry_bind(registry,
4115 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004116 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004117 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004118 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4119 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004120 wl_registry_bind(registry, id,
4121 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004122 } else if (strcmp(interface, "text_cursor_position") == 0) {
4123 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004124 wl_registry_bind(registry, id,
4125 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004126 } else if (strcmp(interface, "workspace_manager") == 0) {
4127 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004128 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004129
4130 if (d->global_handler)
4131 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004132}
4133
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004134static void
4135registry_handle_global_remove(void *data, struct wl_registry *registry,
4136 uint32_t name)
4137{
4138 struct display *d = data;
4139 struct global *global;
4140 struct global *tmp;
4141
4142 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4143 if (global->name != name)
4144 continue;
4145
4146 /* XXX: Should destroy bound globals, and call
4147 * the counterpart of display::global_handler
4148 */
4149 wl_list_remove(&global->link);
4150 free(global->interface);
4151 free(global);
4152 }
4153}
4154
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004155void *
4156display_bind(struct display *display, uint32_t name,
4157 const struct wl_interface *interface, uint32_t version)
4158{
4159 return wl_registry_bind(display->registry, name, interface, version);
4160}
4161
4162static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004163 registry_handle_global,
4164 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004165};
4166
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004167#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004168static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004169init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004170{
4171 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004172 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004173
Rob Clark6396ed32012-03-11 19:48:41 -05004174#ifdef USE_CAIRO_GLESV2
4175# define GL_BIT EGL_OPENGL_ES2_BIT
4176#else
4177# define GL_BIT EGL_OPENGL_BIT
4178#endif
4179
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004180 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004181 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004182 EGL_RED_SIZE, 1,
4183 EGL_GREEN_SIZE, 1,
4184 EGL_BLUE_SIZE, 1,
4185 EGL_ALPHA_SIZE, 1,
4186 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004187 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004188 EGL_NONE
4189 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004190
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004191#ifdef USE_CAIRO_GLESV2
4192 static const EGLint context_attribs[] = {
4193 EGL_CONTEXT_CLIENT_VERSION, 2,
4194 EGL_NONE
4195 };
4196 EGLint api = EGL_OPENGL_ES_API;
4197#else
4198 EGLint *context_attribs = NULL;
4199 EGLint api = EGL_OPENGL_API;
4200#endif
4201
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004202 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004203 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004204 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004205 return -1;
4206 }
4207
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004208 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004209 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004210 return -1;
4211 }
4212
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004213 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4214 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004215 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004216 return -1;
4217 }
4218
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004219 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004220 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004221 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004222 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004223 return -1;
4224 }
4225
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004226 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004227 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004228 return -1;
4229 }
4230
Benjamin Franzke0c991632011-09-27 21:57:31 +02004231 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4232 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004233 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004234 return -1;
4235 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004236
4237 return 0;
4238}
4239
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004240static void
4241fini_egl(struct display *display)
4242{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004243 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004244
4245 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4246 EGL_NO_CONTEXT);
4247
4248 eglTerminate(display->dpy);
4249 eglReleaseThread();
4250}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004251#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004252
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004253static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004254init_dummy_surface(struct display *display)
4255{
4256 int len;
4257 void *data;
4258
4259 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4260 data = malloc(len);
4261 display->dummy_surface =
4262 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4263 1, 1, len);
4264 display->dummy_surface_data = data;
4265}
4266
4267static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004268handle_display_data(struct task *task, uint32_t events)
4269{
4270 struct display *display =
4271 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004272 struct epoll_event ep;
4273 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004274
4275 display->display_fd_events = events;
4276
4277 if (events & EPOLLERR || events & EPOLLHUP) {
4278 display_exit(display);
4279 return;
4280 }
4281
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004282 if (events & EPOLLIN) {
4283 ret = wl_display_dispatch(display->display);
4284 if (ret == -1) {
4285 display_exit(display);
4286 return;
4287 }
4288 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004289
4290 if (events & EPOLLOUT) {
4291 ret = wl_display_flush(display->display);
4292 if (ret == 0) {
4293 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4294 ep.data.ptr = &display->display_task;
4295 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4296 display->display_fd, &ep);
4297 } else if (ret == -1 && errno != EAGAIN) {
4298 display_exit(display);
4299 return;
4300 }
4301 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004302}
4303
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004304struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004305display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004306{
4307 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004308
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004309 d = malloc(sizeof *d);
4310 if (d == NULL)
4311 return NULL;
4312
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004313 memset(d, 0, sizeof *d);
4314
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004315 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004316 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004317 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004318 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004319 return NULL;
4320 }
4321
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004322 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004323 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004324 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004325 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4326 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004327
4328 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004329 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004330 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004331 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004332
Daniel Stone97f68542012-05-30 16:32:01 +01004333 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004334 if (d->xkb_context == NULL) {
4335 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004336 return NULL;
4337 }
4338
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004339 d->workspace = 0;
4340 d->workspace_count = 1;
4341
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004342 d->registry = wl_display_get_registry(d->display);
4343 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004344
4345 if (wl_display_dispatch(d->display) < 0) {
4346 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4347 return NULL;
4348 }
4349
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004350#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02004351 if (init_egl(d) < 0)
4352 fprintf(stderr, "EGL does not seem to work, "
4353 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004354#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05004355
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004356 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04004357
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004358 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04004359
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004360 wl_list_init(&d->window_list);
4361
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004362 init_dummy_surface(d);
4363
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004364 return d;
4365}
4366
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004367static void
4368display_destroy_outputs(struct display *display)
4369{
4370 struct output *tmp;
4371 struct output *output;
4372
4373 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4374 output_destroy(output);
4375}
4376
Pekka Paalanene1207c72011-12-16 12:02:09 +02004377static void
4378display_destroy_inputs(struct display *display)
4379{
4380 struct input *tmp;
4381 struct input *input;
4382
4383 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4384 input_destroy(input);
4385}
4386
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004387void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004388display_destroy(struct display *display)
4389{
Pekka Paalanenc2052982011-12-16 11:41:32 +02004390 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07004391 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4392 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02004393
4394 if (!wl_list_empty(&display->deferred_list))
4395 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4396
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004397 cairo_surface_destroy(display->dummy_surface);
4398 free(display->dummy_surface_data);
4399
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004400 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004401 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004402
Daniel Stone97f68542012-05-30 16:32:01 +01004403 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004404
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04004405 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03004406 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02004407
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004408#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004409 if (display->argb_device)
4410 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004411#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004412
Pekka Paalanenc2052982011-12-16 11:41:32 +02004413 if (display->shell)
4414 wl_shell_destroy(display->shell);
4415
4416 if (display->shm)
4417 wl_shm_destroy(display->shm);
4418
4419 if (display->data_device_manager)
4420 wl_data_device_manager_destroy(display->data_device_manager);
4421
4422 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02004423 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02004424
4425 close(display->epoll_fd);
4426
U. Artie Eoff44874d92012-10-02 21:12:35 -07004427 if (!(display->display_fd_events & EPOLLERR) &&
4428 !(display->display_fd_events & EPOLLHUP))
4429 wl_display_flush(display->display);
4430
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05004431 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004432 free(display);
4433}
4434
4435void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004436display_set_user_data(struct display *display, void *data)
4437{
4438 display->user_data = data;
4439}
4440
4441void *
4442display_get_user_data(struct display *display)
4443{
4444 return display->user_data;
4445}
4446
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04004447struct wl_display *
4448display_get_display(struct display *display)
4449{
4450 return display->display;
4451}
4452
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004453struct output *
4454display_get_output(struct display *display)
4455{
4456 return container_of(display->output_list.next, struct output, link);
4457}
4458
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004459struct wl_compositor *
4460display_get_compositor(struct display *display)
4461{
4462 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004463}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004464
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004465uint32_t
4466display_get_serial(struct display *display)
4467{
4468 return display->serial;
4469}
4470
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004471EGLDisplay
4472display_get_egl_display(struct display *d)
4473{
4474 return d->dpy;
4475}
4476
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004477struct wl_data_source *
4478display_create_data_source(struct display *display)
4479{
4480 return wl_data_device_manager_create_data_source(display->data_device_manager);
4481}
4482
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004483EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004484display_get_argb_egl_config(struct display *d)
4485{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004486 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004487}
4488
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004489struct wl_shell *
4490display_get_shell(struct display *display)
4491{
4492 return display->shell;
4493}
4494
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004495int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004496display_acquire_window_surface(struct display *display,
4497 struct window *window,
4498 EGLContext ctx)
4499{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004500 struct surface *surface = window->main_surface;
4501
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004502 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004503 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004504
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004505 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004506}
4507
4508void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004509display_release_window_surface(struct display *display,
4510 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004511{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004512 struct surface *surface = window->main_surface;
4513
Pekka Paalanen03fc3162012-11-19 17:15:58 +02004514 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004515 return;
4516
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004517 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004518}
4519
4520void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004521display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004522{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004523 wl_list_insert(&display->deferred_list, &task->link);
4524}
4525
4526void
4527display_watch_fd(struct display *display,
4528 int fd, uint32_t events, struct task *task)
4529{
4530 struct epoll_event ep;
4531
4532 ep.events = events;
4533 ep.data.ptr = task;
4534 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4535}
4536
4537void
Dima Ryazanova85292e2012-11-29 00:27:09 -08004538display_unwatch_fd(struct display *display, int fd)
4539{
4540 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4541}
4542
4543void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004544display_run(struct display *display)
4545{
4546 struct task *task;
4547 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004548 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004549
Pekka Paalanen826d7952011-12-15 10:14:07 +02004550 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004551 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004552 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004553 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004554 struct task, link);
4555 wl_list_remove(&task->link);
4556 task->run(task, 0);
4557 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004558
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004559 wl_display_dispatch_pending(display->display);
4560
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004561 if (!display->running)
4562 break;
4563
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004564 ret = wl_display_flush(display->display);
4565 if (ret < 0 && errno == EAGAIN) {
4566 ep[0].events =
4567 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4568 ep[0].data.ptr = &display->display_task;
4569
4570 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4571 display->display_fd, &ep[0]);
4572 } else if (ret < 0) {
4573 break;
4574 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004575
4576 count = epoll_wait(display->epoll_fd,
4577 ep, ARRAY_LENGTH(ep), -1);
4578 for (i = 0; i < count; i++) {
4579 task = ep[i].data.ptr;
4580 task->run(task, ep[i].events);
4581 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004582 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004583}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004584
4585void
4586display_exit(struct display *display)
4587{
4588 display->running = 0;
4589}
Jan Arne Petersencd997062012-11-18 19:06:44 +01004590
4591void
4592keysym_modifiers_add(struct wl_array *modifiers_map,
4593 const char *name)
4594{
4595 size_t len = strlen(name) + 1;
4596 char *p;
4597
4598 p = wl_array_add(modifiers_map, len);
4599
4600 if (p == NULL)
4601 return;
4602
4603 strncpy(p, name, len);
4604}
4605
4606static xkb_mod_index_t
4607keysym_modifiers_get_index(struct wl_array *modifiers_map,
4608 const char *name)
4609{
4610 xkb_mod_index_t index = 0;
4611 char *p = modifiers_map->data;
4612
4613 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4614 if (strcmp(p, name) == 0)
4615 return index;
4616
4617 index++;
4618 p += strlen(p) + 1;
4619 }
4620
4621 return XKB_MOD_INVALID;
4622}
4623
4624xkb_mod_mask_t
4625keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4626 const char *name)
4627{
4628 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4629
4630 if (index == XKB_MOD_INVALID)
4631 return XKB_MOD_INVALID;
4632
4633 return 1 << index;
4634}