blob: 5dcd6cbef4f8a0b596d053e6ec9b8e0d14c99a1d [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
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øgsberg8def2642011-01-14 17:41:33 -050055#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040057#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050058
Daniel Stone9d4f0302012-02-15 16:33:21 +000059#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030060#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020063#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040064#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060065#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020066#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030067#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050070
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070071struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030072
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040073struct global {
74 uint32_t name;
75 char *interface;
76 uint32_t version;
77 struct wl_list link;
78};
79
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050080struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050081 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040082 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050083 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040084 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040085 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040086 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060087 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020088 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040089 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050090 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020091 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020092 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040093 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040094
95 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -070096 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040097 struct task display_task;
98
99 int epoll_fd;
100 struct wl_list deferred_list;
101
Pekka Paalanen826d7952011-12-15 10:14:07 +0200102 int running;
103
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400104 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400105 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400106 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500107 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400108
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400109 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400110
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300111 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300112 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400113
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500114 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
115 PFNEGLCREATEIMAGEKHRPROC create_image;
116 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200117
118 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;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500127};
128
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400130 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400131 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400132 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500133 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400134 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500135 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 TYPE_CUSTOM
137};
Rob Bradford7507b572012-05-15 17:55:34 +0100138
139struct window_output {
140 struct output *output;
141 struct wl_list link;
142};
143
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500144struct window {
145 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500146 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100147 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500148 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200149 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500150 struct wl_region *input_region;
151 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500152 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500153 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400154 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500155 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500156 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400157 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400158 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400159 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400160 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400161 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400162 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400163 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400164 int focus_count;
165
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400166 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500167
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400168 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500169
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700170 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400171
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500172 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500173 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400174 window_data_handler_t data_handler;
175 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500176 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400177 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400178
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300179 struct wl_callback *frame_cb;
180
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200181 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500182 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500183
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500184 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400185 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500186};
187
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500188struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500189 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300190 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500191 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400192 struct wl_list link;
193 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194 widget_resize_handler_t resize_handler;
195 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500196 widget_enter_handler_t enter_handler;
197 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500198 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500199 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200200 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400201 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500202 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300203 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400204};
205
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400206struct input {
207 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100208 struct wl_seat *seat;
209 struct wl_pointer *pointer;
210 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400211 struct window *pointer_focus;
212 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300213 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300214 uint32_t cursor_anim_start;
215 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300216 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400217 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400218 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400219 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400220 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400221 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400222
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500223 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500224 struct widget *grab;
225 uint32_t grab_button;
226
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400227 struct wl_data_device *data_device;
228 struct data_offer *drag_offer;
229 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100230
231 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100232 struct xkb_keymap *keymap;
233 struct xkb_state *state;
234 xkb_mod_mask_t control_mask;
235 xkb_mod_mask_t alt_mask;
236 xkb_mod_mask_t shift_mask;
237 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400238
239 struct task repeat_task;
240 int repeat_timer_fd;
241 uint32_t repeat_sym;
242 uint32_t repeat_key;
243 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400244};
245
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500246struct output {
247 struct display *display;
248 struct wl_output *output;
249 struct rectangle allocation;
250 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600251 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200252
253 display_output_handler_t destroy_handler;
254 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500255};
256
Martin Minarik1998b152012-05-10 02:04:35 +0200257enum frame_button_action {
258 FRAME_BUTTON_NULL = 0,
259 FRAME_BUTTON_ICON = 1,
260 FRAME_BUTTON_CLOSE = 2,
261 FRAME_BUTTON_MINIMIZE = 3,
262 FRAME_BUTTON_MAXIMIZE = 4,
263};
264
265enum frame_button_pointer {
266 FRAME_BUTTON_DEFAULT = 0,
267 FRAME_BUTTON_OVER = 1,
268 FRAME_BUTTON_ACTIVE = 2,
269};
270
271enum frame_button_align {
272 FRAME_BUTTON_RIGHT = 0,
273 FRAME_BUTTON_LEFT = 1,
274};
275
276enum frame_button_decoration {
277 FRAME_BUTTON_NONE = 0,
278 FRAME_BUTTON_FANCY = 1,
279};
280
281struct frame_button {
282 struct widget *widget;
283 struct frame *frame;
284 cairo_surface_t *icon;
285 enum frame_button_action type;
286 enum frame_button_pointer state;
287 struct wl_list link; /* buttons_list */
288 enum frame_button_align align;
289 enum frame_button_decoration decoration;
290};
291
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500292struct frame {
293 struct widget *widget;
294 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200295 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500296};
297
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500298struct menu {
299 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500300 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500301 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500302 const char **entries;
303 uint32_t time;
304 int current;
305 int count;
306 menu_func_t func;
307};
308
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300309struct tooltip {
310 struct widget *parent;
311 struct window *window;
312 struct widget *widget;
313 char *entry;
314 struct task tooltip_task;
315 int tooltip_fd;
316 float x, y;
317};
318
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700319struct shm_pool {
320 struct wl_shm_pool *pool;
321 size_t size;
322 size_t used;
323 void *data;
324};
325
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400326enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300327 CURSOR_DEFAULT = 100,
328 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400329};
330
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500331enum window_location {
332 WINDOW_INTERIOR = 0,
333 WINDOW_RESIZING_TOP = 1,
334 WINDOW_RESIZING_BOTTOM = 2,
335 WINDOW_RESIZING_LEFT = 4,
336 WINDOW_RESIZING_TOP_LEFT = 5,
337 WINDOW_RESIZING_BOTTOM_LEFT = 6,
338 WINDOW_RESIZING_RIGHT = 8,
339 WINDOW_RESIZING_TOP_RIGHT = 9,
340 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
341 WINDOW_RESIZING_MASK = 15,
342 WINDOW_EXTERIOR = 16,
343 WINDOW_TITLEBAR = 17,
344 WINDOW_CLIENT_AREA = 18,
345};
346
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347static const cairo_user_data_key_t surface_data_key;
348struct surface_data {
349 struct wl_buffer *buffer;
350};
351
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500352#define MULT(_d,c,a,t) \
353 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
354
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500355#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100357struct egl_window_surface_data {
358 struct display *display;
359 struct wl_surface *surface;
360 struct wl_egl_window *window;
361 EGLSurface surf;
362};
363
364static void
365egl_window_surface_data_destroy(void *p)
366{
367 struct egl_window_surface_data *data = p;
368 struct display *d = data->display;
369
370 eglDestroySurface(d->dpy, data->surf);
371 wl_egl_window_destroy(data->window);
372 data->surface = NULL;
373
374 free(p);
375}
376
377static cairo_surface_t *
378display_create_egl_window_surface(struct display *display,
379 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381 struct rectangle *rectangle)
382{
383 cairo_surface_t *cairo_surface;
384 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200386 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387
388 data = malloc(sizeof *data);
389 if (data == NULL)
390 return NULL;
391
392 data->display = display;
393 data->surface = surface;
394
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500395 config = display->argb_config;
396 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400397
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400398 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100399 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400400 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100401
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400402 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500403 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100404
Benjamin Franzke0c991632011-09-27 21:57:31 +0200405 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100406 data->surf,
407 rectangle->width,
408 rectangle->height);
409
410 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
411 data, egl_window_surface_data_destroy);
412
413 return cairo_surface;
414}
415
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416#endif
417
418struct wl_buffer *
419display_get_buffer_for_surface(struct display *display,
420 cairo_surface_t *surface)
421{
422 struct surface_data *data;
423
424 data = cairo_surface_get_user_data (surface, &surface_data_key);
425
426 return data->buffer;
427}
428
429struct shm_surface_data {
430 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432};
433
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500434static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700435shm_pool_destroy(struct shm_pool *pool);
436
437static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438shm_surface_data_destroy(void *p)
439{
440 struct shm_surface_data *data = p;
441
442 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700443 if (data->pool)
444 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300445
446 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447}
448
Kristian Høgsberg16626282012-04-03 11:21:27 -0400449static struct wl_shm_pool *
450make_shm_pool(struct display *display, int size, void **data)
451{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 struct wl_shm_pool *pool;
453 int fd;
454
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300455 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400456 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300457 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
458 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400459 return NULL;
460 }
461
462 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400463 if (*data == MAP_FAILED) {
464 fprintf(stderr, "mmap failed: %m\n");
465 close(fd);
466 return NULL;
467 }
468
469 pool = wl_shm_create_pool(display->shm, fd, size);
470
471 close(fd);
472
473 return pool;
474}
475
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700476static struct shm_pool *
477shm_pool_create(struct display *display, size_t size)
478{
479 struct shm_pool *pool = malloc(sizeof *pool);
480
481 if (!pool)
482 return NULL;
483
484 pool->pool = make_shm_pool(display, size, &pool->data);
485 if (!pool->pool) {
486 free(pool);
487 return NULL;
488 }
489
490 pool->size = size;
491 pool->used = 0;
492
493 return pool;
494}
495
496static void *
497shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
498{
499 if (pool->used + size > pool->size)
500 return NULL;
501
502 *offset = pool->used;
503 pool->used += size;
504
505 return (char *) pool->data + *offset;
506}
507
508/* destroy the pool. this does not unmap the memory though */
509static void
510shm_pool_destroy(struct shm_pool *pool)
511{
512 munmap(pool->data, pool->size);
513 wl_shm_pool_destroy(pool->pool);
514 free(pool);
515}
516
517/* Start allocating from the beginning of the pool again */
518static void
519shm_pool_reset(struct shm_pool *pool)
520{
521 pool->used = 0;
522}
523
524static int
525data_length_for_shm_surface(struct rectangle *rect)
526{
527 int stride;
528
529 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530 rect->width);
531 return stride * rect->height;
532}
533
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500534static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700535display_create_shm_surface_from_pool(struct display *display,
536 struct rectangle *rectangle,
537 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538{
539 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400540 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400541 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700542 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400543 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
545 data = malloc(sizeof *data);
546 if (data == NULL)
547 return NULL;
548
549 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
550 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700551 length = stride * rectangle->height;
552 data->pool = NULL;
553 map = shm_pool_allocate(pool, length, &offset);
554
555 if (!map) {
556 free(data);
557 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400558 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400560 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561 CAIRO_FORMAT_ARGB32,
562 rectangle->width,
563 rectangle->height,
564 stride);
565
566 cairo_surface_set_user_data (surface, &surface_data_key,
567 data, shm_surface_data_destroy);
568
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500570 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400571 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500572 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400573
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700574 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400575 rectangle->width,
576 rectangle->height,
577 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700579 return surface;
580}
581
582static cairo_surface_t *
583display_create_shm_surface(struct display *display,
584 struct rectangle *rectangle, uint32_t flags,
585 struct window *window)
586{
587 struct shm_surface_data *data;
588 struct shm_pool *pool;
589 cairo_surface_t *surface;
590
591 if (window && window->pool) {
592 shm_pool_reset(window->pool);
593 surface = display_create_shm_surface_from_pool(display,
594 rectangle,
595 flags,
596 window->pool);
597 if (surface)
598 return surface;
599 }
600
601 pool = shm_pool_create(display,
602 data_length_for_shm_surface(rectangle));
603 if (!pool)
604 return NULL;
605
606 surface =
607 display_create_shm_surface_from_pool(display, rectangle,
608 flags, pool);
609
610 if (!surface) {
611 shm_pool_destroy(pool);
612 return NULL;
613 }
614
615 /* make sure we destroy the pool when the surface is destroyed */
616 data = cairo_surface_get_user_data(surface, &surface_data_key);
617 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618
619 return surface;
620}
621
nobled7b87cb02011-02-01 18:51:47 +0000622static int
623check_size(struct rectangle *rect)
624{
625 if (rect->width && rect->height)
626 return 0;
627
628 fprintf(stderr, "tried to create surface of "
629 "width: %d, height: %d\n", rect->width, rect->height);
630 return -1;
631}
632
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633cairo_surface_t *
634display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400636 struct rectangle *rectangle,
637 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638{
nobled7b87cb02011-02-01 18:51:47 +0000639 if (check_size(rectangle) < 0)
640 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300642 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400643 return display_create_egl_window_surface(display,
644 surface,
645 flags,
646 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400648 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649}
650
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200651/*
652 * The following correspondences between file names and cursors was copied
653 * from: https://bugs.kde.org/attachment.cgi?id=67313
654 */
655
656static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300657 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200658 "sw-resize"
659};
660
661static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300662 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200663 "se-resize"
664};
665
666static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300667 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200668 "s-resize"
669};
670
671static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300672 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200673 "closedhand",
674 "208530c400c041818281048008011002"
675};
676
677static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300678 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200679 "default",
680 "top_left_arrow",
681 "left-arrow"
682};
683
684static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300685 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200686 "w-resize"
687};
688
689static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300690 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200691 "e-resize"
692};
693
694static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300695 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200696 "nw-resize"
697};
698
699static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300700 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200701 "ne-resize"
702};
703
704static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300705 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200706 "n-resize"
707};
708
709static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300710 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200711 "ibeam",
712 "text"
713};
714
715static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300716 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200717 "pointer",
718 "pointing_hand",
719 "e29285e634086352946a0e7090d73106"
720};
721
722static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400723 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200724 "wait",
725 "0426c94ea35c87780ff01dc239897213"
726};
727
728struct cursor_alternatives {
729 const char **names;
730 size_t count;
731};
732
733static const struct cursor_alternatives cursors[] = {
734 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
735 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
736 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
737 {grabbings, ARRAY_LENGTH(grabbings)},
738 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
739 {left_sides, ARRAY_LENGTH(left_sides)},
740 {right_sides, ARRAY_LENGTH(right_sides)},
741 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
742 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
743 {top_sides, ARRAY_LENGTH(top_sides)},
744 {xterms, ARRAY_LENGTH(xterms)},
745 {hand1s, ARRAY_LENGTH(hand1s)},
746 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300747};
748
749static void
750create_cursors(struct display *display)
751{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100752 char *config_file;
753 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200754 unsigned int i, j;
755 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100756 struct config_key shell_keys[] = {
757 { "cursor-theme", CONFIG_KEY_STRING, &theme },
758 };
759 struct config_section cs[] = {
760 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
761 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300762
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100763 config_file = config_file_path("weston.ini");
764 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
765 free(config_file);
766
767 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300768 display->cursors =
769 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
770
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300771 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200772 cursor = NULL;
773 for (j = 0; !cursor && j < cursors[i].count; ++j)
774 cursor = wl_cursor_theme_get_cursor(
775 display->cursor_theme, cursors[i].names[j]);
776
777 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300778 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200779 cursors[i].names[0]);
780
781 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300782 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300783}
784
785static void
786destroy_cursors(struct display *display)
787{
788 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800789 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300790}
791
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300792struct wl_cursor_image *
793display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400794{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200795 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400796
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300797 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400798}
799
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400800static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200801window_get_resize_dx_dy(struct window *window, int *x, int *y)
802{
803 if (window->resize_edges & WINDOW_RESIZING_LEFT)
804 *x = window->server_allocation.width - window->allocation.width;
805 else
806 *x = 0;
807
808 if (window->resize_edges & WINDOW_RESIZING_TOP)
809 *y = window->server_allocation.height -
810 window->allocation.height;
811 else
812 *y = 0;
813
814 window->resize_edges = 0;
815}
816
817static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500818window_attach_surface(struct window *window)
819{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400820 struct display *display = window->display;
821 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500825 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400827 if (window->type == TYPE_NONE) {
828 window->type = TYPE_TOPLEVEL;
829 if (display->shell)
830 wl_shell_surface_set_toplevel(window->shell_surface);
831 }
832
Pekka Paalanen512dde82012-10-10 12:49:27 +0300833 if (window->opaque_region) {
834 wl_surface_set_opaque_region(window->surface,
835 window->opaque_region);
836 wl_region_destroy(window->opaque_region);
837 window->opaque_region = NULL;
838 }
839
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300840 if (window->input_region) {
841 wl_surface_set_input_region(window->surface,
842 window->input_region);
843 wl_region_destroy(window->input_region);
844 window->input_region = NULL;
845 }
846
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000848#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
850 data = cairo_surface_get_user_data(window->cairo_surface,
851 &surface_data_key);
852
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 cairo_gl_surface_swapbuffers(window->cairo_surface);
854 wl_egl_window_get_attached_size(data->window,
855 &window->server_allocation.width,
856 &window->server_allocation.height);
857 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000858#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 buffer =
861 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400862 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200864 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100865 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400866 wl_surface_damage(window->surface, 0, 0,
867 window->allocation.width,
868 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300869 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100870 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400871 cairo_surface_destroy(window->cairo_surface);
872 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100873 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000874 default:
875 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500877}
878
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400879int
880window_has_focus(struct window *window)
881{
882 return window->focus_count > 0;
883}
884
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500885void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400886window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400888 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100889 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500890}
891
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892void
893window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500895 cairo_surface_reference(surface);
896
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897 if (window->cairo_surface != NULL)
898 cairo_surface_destroy(window->cairo_surface);
899
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500900 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901}
902
Benjamin Franzke22d54812011-07-16 19:50:32 +0000903#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904static void
905window_resize_cairo_window_surface(struct window *window)
906{
907 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909
910 data = cairo_surface_get_user_data(window->cairo_surface,
911 &surface_data_key);
912
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200913 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914 wl_egl_window_resize(data->window,
915 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200916 window->allocation.height,
917 x,y);
918
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919 cairo_gl_surface_set_size(window->cairo_surface,
920 window->allocation.width,
921 window->allocation.height);
922}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000923#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100924
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400925struct display *
926window_get_display(struct window *window)
927{
928 return window->display;
929}
930
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400931void
932window_create_surface(struct window *window)
933{
934 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400935 uint32_t flags = 0;
936
937 if (!window->transparent)
938 flags = SURFACE_OPAQUE;
939
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500941#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
943 if (window->cairo_surface) {
944 window_resize_cairo_window_surface(window);
945 return;
946 }
947 surface = display_create_surface(window->display,
948 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951#endif
952 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400954 &window->allocation,
955 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800957 default:
958 surface = NULL;
959 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400961
962 window_set_surface(window, surface);
963 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400964}
965
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200966static void frame_destroy(struct frame *frame);
967
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400968void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500969window_destroy(struct window *window)
970{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971 struct display *display = window->display;
972 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100973 struct window_output *window_output;
974 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200975
976 if (window->redraw_scheduled)
977 wl_list_remove(&window->redraw_task.link);
978
979 wl_list_for_each(input, &display->input_list, link) {
980 if (input->pointer_focus == window)
981 input->pointer_focus = NULL;
982 if (input->keyboard_focus == window)
983 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500984 if (input->focus_widget &&
985 input->focus_widget->window == window)
986 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200987 }
988
Rob Bradford7507b572012-05-15 17:55:34 +0100989 wl_list_for_each_safe(window_output, window_output_tmp,
990 &window->window_output_list, link) {
991 free (window_output);
992 }
993
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500994 if (window->input_region)
995 wl_region_destroy(window->input_region);
996 if (window->opaque_region)
997 wl_region_destroy(window->opaque_region);
998
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200999 if (window->frame)
1000 frame_destroy(window->frame);
1001
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001002 if (window->shell_surface)
1003 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001004 wl_surface_destroy(window->surface);
1005 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001006
1007 if (window->cairo_surface != NULL)
1008 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001009
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001010 if (window->frame_cb)
1011 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001012 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001013 free(window);
1014}
1015
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001016static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021 wl_list_for_each(child, &widget->child_list, link) {
1022 target = widget_find_widget(child, x, y);
1023 if (target)
1024 return target;
1025 }
1026
1027 if (widget->allocation.x <= x &&
1028 x < widget->allocation.x + widget->allocation.width &&
1029 widget->allocation.y <= y &&
1030 y < widget->allocation.y + widget->allocation.height) {
1031 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001032 }
1033
1034 return NULL;
1035}
1036
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001037static struct widget *
1038widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 widget = malloc(sizeof *widget);
1043 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001044 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001046 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001047 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001048 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001049 widget->tooltip = NULL;
1050 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001051
1052 return widget;
1053}
1054
1055struct widget *
1056window_add_widget(struct window *window, void *data)
1057{
1058 window->widget = widget_create(window, data);
1059 wl_list_init(&window->widget->link);
1060
1061 return window->widget;
1062}
1063
1064struct widget *
1065widget_add_widget(struct widget *parent, void *data)
1066{
1067 struct widget *widget;
1068
1069 widget = widget_create(parent->window, data);
1070 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
1075void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001076widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001078 struct display *display = widget->window->display;
1079 struct input *input;
1080
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001081 if (widget->tooltip) {
1082 free(widget->tooltip);
1083 widget->tooltip = NULL;
1084 }
1085
Pekka Paalanene156fb62012-01-19 13:51:38 +02001086 wl_list_for_each(input, &display->input_list, link) {
1087 if (input->focus_widget == widget)
1088 input->focus_widget = NULL;
1089 }
1090
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001091 wl_list_remove(&widget->link);
1092 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
1101void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001102widget_set_size(struct widget *widget, int32_t width, int32_t height)
1103{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001104 widget->allocation.width = width;
1105 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001106}
1107
1108void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109widget_set_allocation(struct widget *widget,
1110 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001112 widget->allocation.x = x;
1113 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001114 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001115}
1116
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001117void
1118widget_set_transparent(struct widget *widget, int transparent)
1119{
1120 widget->opaque = !transparent;
1121}
1122
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001124widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001125{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001126 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001127}
1128
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001129void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001130widget_set_resize_handler(struct widget *widget,
1131 widget_resize_handler_t handler)
1132{
1133 widget->resize_handler = handler;
1134}
1135
1136void
1137widget_set_redraw_handler(struct widget *widget,
1138 widget_redraw_handler_t handler)
1139{
1140 widget->redraw_handler = handler;
1141}
1142
1143void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001144widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001146 widget->enter_handler = handler;
1147}
1148
1149void
1150widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1151{
1152 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001153}
1154
1155void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001156widget_set_motion_handler(struct widget *widget,
1157 widget_motion_handler_t handler)
1158{
1159 widget->motion_handler = handler;
1160}
1161
1162void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001163widget_set_button_handler(struct widget *widget,
1164 widget_button_handler_t handler)
1165{
1166 widget->button_handler = handler;
1167}
1168
1169void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001170widget_set_axis_handler(struct widget *widget,
1171 widget_axis_handler_t handler)
1172{
1173 widget->axis_handler = handler;
1174}
1175
1176void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001177widget_schedule_redraw(struct widget *widget)
1178{
1179 window_schedule_redraw(widget->window);
1180}
1181
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001182cairo_surface_t *
1183window_get_surface(struct window *window)
1184{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001185 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001186}
1187
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001188struct wl_surface *
1189window_get_wl_surface(struct window *window)
1190{
1191 return window->surface;
1192}
1193
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001194struct wl_shell_surface *
1195window_get_wl_shell_surface(struct window *window)
1196{
1197 return window->shell_surface;
1198}
1199
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001200static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001201tooltip_redraw_handler(struct widget *widget, void *data)
1202{
1203 cairo_t *cr;
1204 const int32_t r = 3;
1205 struct tooltip *tooltip = data;
1206 int32_t width, height;
1207 struct window *window = widget->window;
1208
1209 cr = cairo_create(window->cairo_surface);
1210 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1211 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1212 cairo_paint(cr);
1213
1214 width = window->allocation.width;
1215 height = window->allocation.height;
1216 rounded_rect(cr, 0, 0, width, height, r);
1217
1218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1219 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1220 cairo_fill(cr);
1221
1222 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1223 cairo_move_to(cr, 10, 16);
1224 cairo_show_text(cr, tooltip->entry);
1225 cairo_destroy(cr);
1226}
1227
1228static cairo_text_extents_t
1229get_text_extents(struct tooltip *tooltip)
1230{
1231 struct window *window;
1232 cairo_t *cr;
1233 cairo_text_extents_t extents;
1234
1235 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1236 * created yet */
1237 window = tooltip->widget->window->parent;
1238 cr = cairo_create(window->cairo_surface);
1239 cairo_text_extents(cr, tooltip->entry, &extents);
1240 cairo_destroy(cr);
1241
1242 return extents;
1243}
1244
1245static int
1246window_create_tooltip(struct tooltip *tooltip)
1247{
1248 struct widget *parent = tooltip->parent;
1249 struct display *display = parent->window->display;
1250 struct window *window;
1251 const int offset_y = 27;
1252 const int margin = 3;
1253 cairo_text_extents_t extents;
1254
1255 if (tooltip->widget)
1256 return 0;
1257
1258 window = window_create_transient(display, parent->window, tooltip->x,
1259 tooltip->y + offset_y,
1260 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1261 if (!window)
1262 return -1;
1263
1264 tooltip->window = window;
1265 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1266
1267 extents = get_text_extents(tooltip);
1268 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1269 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1270
1271 return 0;
1272}
1273
1274void
1275widget_destroy_tooltip(struct widget *parent)
1276{
1277 struct tooltip *tooltip = parent->tooltip;
1278
1279 parent->tooltip_count = 0;
1280 if (!tooltip)
1281 return;
1282
1283 if (tooltip->widget) {
1284 widget_destroy(tooltip->widget);
1285 window_destroy(tooltip->window);
1286 tooltip->widget = NULL;
1287 tooltip->window = NULL;
1288 }
1289
1290 close(tooltip->tooltip_fd);
1291 free(tooltip->entry);
1292 free(tooltip);
1293 parent->tooltip = NULL;
1294}
1295
1296static void
1297tooltip_func(struct task *task, uint32_t events)
1298{
1299 struct tooltip *tooltip =
1300 container_of(task, struct tooltip, tooltip_task);
1301 uint64_t exp;
1302
Martin Olsson8df662a2012-07-08 03:03:47 +02001303 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1304 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001305 window_create_tooltip(tooltip);
1306}
1307
1308#define TOOLTIP_TIMEOUT 500
1309static int
1310tooltip_timer_reset(struct tooltip *tooltip)
1311{
1312 struct itimerspec its;
1313
1314 its.it_interval.tv_sec = 0;
1315 its.it_interval.tv_nsec = 0;
1316 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1317 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1318 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1319 fprintf(stderr, "could not set timerfd\n: %m");
1320 return -1;
1321 }
1322
1323 return 0;
1324}
1325
1326int
1327widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1328{
1329 struct tooltip *tooltip = parent->tooltip;
1330
1331 parent->tooltip_count++;
1332 if (tooltip) {
1333 tooltip->x = x;
1334 tooltip->y = y;
1335 tooltip_timer_reset(tooltip);
1336 return 0;
1337 }
1338
1339 /* the handler might be triggered too fast via input device motion, so
1340 * we need this check here to make sure tooltip is fully initialized */
1341 if (parent->tooltip_count > 1)
1342 return 0;
1343
1344 tooltip = malloc(sizeof *tooltip);
1345 if (!tooltip)
1346 return -1;
1347
1348 parent->tooltip = tooltip;
1349 tooltip->parent = parent;
1350 tooltip->widget = NULL;
1351 tooltip->window = NULL;
1352 tooltip->x = x;
1353 tooltip->y = y;
1354 tooltip->entry = strdup(entry);
1355 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1356 if (tooltip->tooltip_fd < 0) {
1357 fprintf(stderr, "could not create timerfd\n: %m");
1358 return -1;
1359 }
1360
1361 tooltip->tooltip_task.run = tooltip_func;
1362 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1363 EPOLLIN, &tooltip->tooltip_task);
1364 tooltip_timer_reset(tooltip);
1365
1366 return 0;
1367}
1368
1369static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001370workspace_manager_state(void *data,
1371 struct workspace_manager *workspace_manager,
1372 uint32_t current,
1373 uint32_t count)
1374{
1375 struct display *display = data;
1376
1377 display->workspace = current;
1378 display->workspace_count = count;
1379}
1380
1381static const struct workspace_manager_listener workspace_manager_listener = {
1382 workspace_manager_state
1383};
1384
1385static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386frame_resize_handler(struct widget *widget,
1387 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001388{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389 struct frame *frame = data;
1390 struct widget *child = frame->child;
1391 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001392 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001393 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001394 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001395 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001396 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001397 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001399 switch (widget->window->type) {
1400 case TYPE_FULLSCREEN:
1401 decoration_width = 0;
1402 decoration_height = 0;
1403
1404 allocation.x = 0;
1405 allocation.y = 0;
1406 allocation.width = width;
1407 allocation.height = height;
1408 opaque_margin = 0;
1409
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
1412 break;
1413 case TYPE_MAXIMIZED:
1414 decoration_width = t->width * 2;
1415 decoration_height = t->width + t->titlebar_height;
1416
1417 allocation.x = t->width;
1418 allocation.y = t->titlebar_height;
1419 allocation.width = width - decoration_width;
1420 allocation.height = height - decoration_height;
1421
1422 opaque_margin = 0;
1423
1424 wl_list_for_each(button, &frame->buttons_list, link)
1425 button->widget->opaque = 0;
1426 break;
1427 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 decoration_width = (t->width + t->margin) * 2;
1429 decoration_height = t->width +
1430 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001432 allocation.x = t->width + t->margin;
1433 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001434 allocation.width = width - decoration_width;
1435 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001436
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001437 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001438
1439 wl_list_for_each(button, &frame->buttons_list, link)
1440 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001441 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001442 }
1443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 widget_set_allocation(child, allocation.x, allocation.y,
1445 allocation.width, allocation.height);
1446
1447 if (child->resize_handler)
1448 child->resize_handler(child,
1449 allocation.width,
1450 allocation.height,
1451 child->user_data);
1452
Scott Moreauf7e498c2012-05-14 11:39:29 -06001453 width = child->allocation.width + decoration_width;
1454 height = child->allocation.height + decoration_height;
1455
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001456 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1457
Kristian Høgsberg023be102012-07-31 11:59:12 -04001458 if (widget->window->type != TYPE_FULLSCREEN) {
1459 widget->window->input_region =
1460 wl_compositor_create_region(display->compositor);
1461 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001462 shadow_margin, shadow_margin,
1463 width - 2 * shadow_margin,
1464 height - 2 * shadow_margin);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001465 }
1466
Scott Moreauf7e498c2012-05-14 11:39:29 -06001467 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001468
1469 if (child->opaque) {
1470 widget->window->opaque_region =
1471 wl_compositor_create_region(display->compositor);
1472 wl_region_add(widget->window->opaque_region,
1473 opaque_margin, opaque_margin,
1474 widget->allocation.width - 2 * opaque_margin,
1475 widget->allocation.height - 2 * opaque_margin);
1476 }
Martin Minarik1998b152012-05-10 02:04:35 +02001477
1478 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001479 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1480 x_l = t->width + shadow_margin;
1481 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001482 wl_list_for_each(button, &frame->buttons_list, link) {
1483 const int button_padding = 4;
1484 w = cairo_image_surface_get_width(button->icon);
1485 h = cairo_image_surface_get_height(button->icon);
1486
1487 if (button->decoration == FRAME_BUTTON_FANCY)
1488 w += 10;
1489
1490 if (button->align == FRAME_BUTTON_LEFT) {
1491 widget_set_allocation(button->widget,
1492 x_l, y , w + 1, h + 1);
1493 x_l += w;
1494 x_l += button_padding;
1495 } else {
1496 x_r -= w;
1497 widget_set_allocation(button->widget,
1498 x_r, y , w + 1, h + 1);
1499 x_r -= button_padding;
1500 }
1501 }
1502}
1503
1504static int
1505frame_button_enter_handler(struct widget *widget,
1506 struct input *input, float x, float y, void *data)
1507{
1508 struct frame_button *frame_button = data;
1509
1510 widget_schedule_redraw(frame_button->widget);
1511 frame_button->state = FRAME_BUTTON_OVER;
1512
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001513 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001514}
1515
1516static void
1517frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1518{
1519 struct frame_button *frame_button = data;
1520
1521 widget_schedule_redraw(frame_button->widget);
1522 frame_button->state = FRAME_BUTTON_DEFAULT;
1523}
1524
1525static void
1526frame_button_button_handler(struct widget *widget,
1527 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001528 uint32_t button,
1529 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001530{
1531 struct frame_button *frame_button = data;
1532 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001533 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001534
1535 if (button != BTN_LEFT)
1536 return;
1537
1538 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001539 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001540 frame_button->state = FRAME_BUTTON_ACTIVE;
1541 widget_schedule_redraw(frame_button->widget);
1542
1543 if (frame_button->type == FRAME_BUTTON_ICON)
1544 window_show_frame_menu(window, input, time);
1545 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001546 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001547 frame_button->state = FRAME_BUTTON_DEFAULT;
1548 widget_schedule_redraw(frame_button->widget);
1549 break;
1550 }
1551
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001552 if (!was_pressed)
1553 return;
1554
Martin Minarik1998b152012-05-10 02:04:35 +02001555 switch (frame_button->type) {
1556 case FRAME_BUTTON_CLOSE:
1557 if (window->close_handler)
1558 window->close_handler(window->parent,
1559 window->user_data);
1560 else
1561 display_exit(window->display);
1562 break;
1563 case FRAME_BUTTON_MINIMIZE:
1564 fprintf(stderr,"Minimize stub\n");
1565 break;
1566 case FRAME_BUTTON_MAXIMIZE:
1567 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1568 break;
1569 default:
1570 /* Unknown operation */
1571 break;
1572 }
1573}
1574
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001575static int
1576frame_button_motion_handler(struct widget *widget,
1577 struct input *input, uint32_t time,
1578 float x, float y, void *data)
1579{
1580 struct frame_button *frame_button = data;
1581 enum frame_button_pointer previous_button_state = frame_button->state;
1582
1583 /* only track state for a pressed button */
1584 if (input->grab != widget)
1585 return CURSOR_LEFT_PTR;
1586
1587 if (x > widget->allocation.x &&
1588 x < (widget->allocation.x + widget->allocation.width) &&
1589 y > widget->allocation.y &&
1590 y < (widget->allocation.y + widget->allocation.height)) {
1591 frame_button->state = FRAME_BUTTON_ACTIVE;
1592 } else {
1593 frame_button->state = FRAME_BUTTON_DEFAULT;
1594 }
1595
1596 if (frame_button->state != previous_button_state)
1597 widget_schedule_redraw(frame_button->widget);
1598
1599 return CURSOR_LEFT_PTR;
1600}
1601
Martin Minarik1998b152012-05-10 02:04:35 +02001602static void
1603frame_button_redraw_handler(struct widget *widget, void *data)
1604{
1605 struct frame_button *frame_button = data;
1606 cairo_t *cr;
1607 int width, height, x, y;
1608 struct window *window = widget->window;
1609
1610 x = widget->allocation.x;
1611 y = widget->allocation.y;
1612 width = widget->allocation.width;
1613 height = widget->allocation.height;
1614
1615 if (!width)
1616 return;
1617 if (!height)
1618 return;
1619 if (widget->opaque)
1620 return;
1621
1622 cr = cairo_create(window->cairo_surface);
1623
1624 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1625 cairo_set_line_width(cr, 1);
1626
1627 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1628 cairo_rectangle (cr, x, y, 25, 16);
1629
1630 cairo_stroke_preserve(cr);
1631
1632 switch (frame_button->state) {
1633 case FRAME_BUTTON_DEFAULT:
1634 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1635 break;
1636 case FRAME_BUTTON_OVER:
1637 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1638 break;
1639 case FRAME_BUTTON_ACTIVE:
1640 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1641 break;
1642 }
1643
1644 cairo_fill (cr);
1645
1646 x += 4;
1647 }
1648
1649 cairo_set_source_surface(cr, frame_button->icon, x, y);
1650 cairo_paint(cr);
1651
1652 cairo_destroy(cr);
1653}
1654
1655static struct widget *
1656frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1657 enum frame_button_align align, enum frame_button_decoration style)
1658{
1659 struct frame_button *frame_button;
1660 const char *icon = data;
1661
1662 frame_button = malloc (sizeof *frame_button);
1663 memset(frame_button, 0, sizeof *frame_button);
1664
1665 frame_button->icon = cairo_image_surface_create_from_png(icon);
1666 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1667 frame_button->frame = frame;
1668 frame_button->type = type;
1669 frame_button->align = align;
1670 frame_button->decoration = style;
1671
1672 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1673
1674 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1675 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1676 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1677 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001678 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001679 return frame_button->widget;
1680}
1681
1682static void
1683frame_button_destroy(struct frame_button *frame_button)
1684{
1685 widget_destroy(frame_button->widget);
1686 wl_list_remove(&frame_button->link);
1687 cairo_surface_destroy(frame_button->icon);
1688 free(frame_button);
1689
1690 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001691}
1692
1693static void
1694frame_redraw_handler(struct widget *widget, void *data)
1695{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001696 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001698 struct theme *t = window->display->theme;
1699 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001700
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001701 if (window->type == TYPE_FULLSCREEN)
1702 return;
1703
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001704 cr = cairo_create(window->cairo_surface);
1705
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001706 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001707 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001708 if (window->type == TYPE_MAXIMIZED)
1709 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001710 theme_render_frame(t, cr, widget->allocation.width,
1711 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001712
1713 cairo_destroy(cr);
1714}
1715
1716static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001717frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001718{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001719 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001720 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001721 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001722
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001723 location = theme_get_location(t, input->sx, input->sy,
1724 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001725 frame->widget->allocation.height,
1726 window->type == TYPE_MAXIMIZED ?
1727 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001728
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001729 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001730 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001731 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001732 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001733 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001734 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001735 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001736 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001738 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001739 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001740 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001741 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001742 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001743 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001744 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001745 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001746 case THEME_LOCATION_EXTERIOR:
1747 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001748 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001749 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001750 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001751}
1752
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001753static void
1754frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001755{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001756 struct display *display;
1757
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001758 switch (index) {
1759 case 0: /* close */
1760 if (window->close_handler)
1761 window->close_handler(window->parent,
1762 window->user_data);
1763 else
1764 display_exit(window->display);
1765 break;
1766 case 1: /* fullscreen */
1767 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001768 if (window->fullscreen_handler)
1769 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001770 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001771 case 2: /* move to workspace above */
1772 display = window->display;
1773 if (display->workspace > 0)
1774 workspace_manager_move_surface(display->workspace_manager,
1775 window->surface,
1776 display->workspace - 1);
1777 break;
1778 case 3: /* move to workspace below */
1779 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001780 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001781 workspace_manager_move_surface(display->workspace_manager,
1782 window->surface,
1783 display->workspace + 1);
1784 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001785 }
1786}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001787
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001788void
1789window_show_frame_menu(struct window *window,
1790 struct input *input, uint32_t time)
1791{
1792 int32_t x, y;
1793
1794 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001795 "Close", "Fullscreen",
1796 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001797 };
1798
1799 input_get_position(input, &x, &y);
1800 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001801 x - 10, y - 10, frame_menu_func, entries,
1802 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001803}
1804
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001805static int
1806frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001807 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808{
1809 return frame_get_pointer_image_for_location(data, input);
1810}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001811
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001812static int
1813frame_motion_handler(struct widget *widget,
1814 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001815 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001816{
1817 return frame_get_pointer_image_for_location(data, input);
1818}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001819
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001820static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001821frame_button_handler(struct widget *widget,
1822 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001823 uint32_t button, enum wl_pointer_button_state state,
1824 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825
1826{
1827 struct frame *frame = data;
1828 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001829 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001831
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001832 location = theme_get_location(display->theme, input->sx, input->sy,
1833 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001834 frame->widget->allocation.height,
1835 window->type == TYPE_MAXIMIZED ?
1836 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001837
Daniel Stone4dbadb12012-05-30 16:31:51 +01001838 if (window->display->shell && button == BTN_LEFT &&
1839 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001840 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001841 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001842 if (!window->shell_surface)
1843 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001844 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001845 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001846 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001847 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001849 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001850 case THEME_LOCATION_RESIZING_TOP:
1851 case THEME_LOCATION_RESIZING_BOTTOM:
1852 case THEME_LOCATION_RESIZING_LEFT:
1853 case THEME_LOCATION_RESIZING_RIGHT:
1854 case THEME_LOCATION_RESIZING_TOP_LEFT:
1855 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1856 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1857 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001858 if (!window->shell_surface)
1859 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001860 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001861
1862 if (!display->dpy) {
1863 /* If we're using shm, allocate a big
1864 pool to create buffers out of while
1865 we resize. We should probably base
1866 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001867 window->pool =
1868 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001869 }
1870
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001871 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001872 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001873 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001874 break;
1875 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001876 } else if (button == BTN_RIGHT &&
1877 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001878 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001879 }
1880}
1881
1882struct widget *
1883frame_create(struct window *window, void *data)
1884{
1885 struct frame *frame;
1886
1887 frame = malloc(sizeof *frame);
1888 memset(frame, 0, sizeof *frame);
1889
1890 frame->widget = window_add_widget(window, frame);
1891 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001892
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001893 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1894 widget_set_resize_handler(frame->widget, frame_resize_handler);
1895 widget_set_enter_handler(frame->widget, frame_enter_handler);
1896 widget_set_motion_handler(frame->widget, frame_motion_handler);
1897 widget_set_button_handler(frame->widget, frame_button_handler);
1898
Martin Minarik1998b152012-05-10 02:04:35 +02001899 /* Create empty list for frame buttons */
1900 wl_list_init(&frame->buttons_list);
1901
1902 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1903 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1904
1905 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1906 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1907
1908 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1909 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1910
1911 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1912 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1913
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001914 window->frame = frame;
1915
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001916 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001917}
1918
Kristian Høgsberga1627922012-06-20 17:30:03 -04001919void
1920frame_set_child_size(struct widget *widget, int child_width, int child_height)
1921{
1922 struct display *display = widget->window->display;
1923 struct theme *t = display->theme;
1924 int decoration_width, decoration_height;
1925 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001926 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001927
1928 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001929 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001930 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001931 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001932
1933 width = child_width + decoration_width;
1934 height = child_height + decoration_height;
1935 } else {
1936 width = child_width;
1937 height = child_height;
1938 }
1939
1940 window_schedule_resize(widget->window, width, height);
1941}
1942
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001943static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001944frame_destroy(struct frame *frame)
1945{
Martin Minarik1998b152012-05-10 02:04:35 +02001946 struct frame_button *button, *tmp;
1947
1948 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1949 frame_button_destroy(button);
1950
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001951 /* frame->child must be destroyed by the application */
1952 widget_destroy(frame->widget);
1953 free(frame);
1954}
1955
1956static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001957input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001958 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001959{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001960 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001961 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001962
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001963 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001964 return;
1965
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001966 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001967 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001968 widget = old;
1969 if (input->grab)
1970 widget = input->grab;
1971 if (widget->leave_handler)
1972 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001973 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001974 }
1975
1976 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001977 widget = focus;
1978 if (input->grab)
1979 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001980 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001981 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001982 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001983 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001984
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001985 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001986 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001987}
1988
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001989void
1990input_grab(struct input *input, struct widget *widget, uint32_t button)
1991{
1992 input->grab = widget;
1993 input->grab_button = button;
1994}
1995
1996void
1997input_ungrab(struct input *input)
1998{
1999 struct widget *widget;
2000
2001 input->grab = NULL;
2002 if (input->pointer_focus) {
2003 widget = widget_find_widget(input->pointer_focus->widget,
2004 input->sx, input->sy);
2005 input_set_focus_widget(input, widget, input->sx, input->sy);
2006 }
2007}
2008
2009static void
2010input_remove_pointer_focus(struct input *input)
2011{
2012 struct window *window = input->pointer_focus;
2013
2014 if (!window)
2015 return;
2016
2017 input_set_focus_widget(input, NULL, 0, 0);
2018
2019 input->pointer_focus = NULL;
2020 input->current_cursor = CURSOR_UNSET;
2021}
2022
2023static void
2024pointer_handle_enter(void *data, struct wl_pointer *pointer,
2025 uint32_t serial, struct wl_surface *surface,
2026 wl_fixed_t sx_w, wl_fixed_t sy_w)
2027{
2028 struct input *input = data;
2029 struct window *window;
2030 struct widget *widget;
2031 float sx = wl_fixed_to_double(sx_w);
2032 float sy = wl_fixed_to_double(sy_w);
2033
2034 if (!surface) {
2035 /* enter event for a window we've just destroyed */
2036 return;
2037 }
2038
2039 input->display->serial = serial;
2040 input->pointer_enter_serial = serial;
2041 input->pointer_focus = wl_surface_get_user_data(surface);
2042 window = input->pointer_focus;
2043
2044 if (window->pool) {
2045 shm_pool_destroy(window->pool);
2046 window->pool = NULL;
2047 /* Schedule a redraw to free the pool */
2048 window_schedule_redraw(window);
2049 }
2050
2051 input->sx = sx;
2052 input->sy = sy;
2053
2054 widget = widget_find_widget(window->widget, sx, sy);
2055 input_set_focus_widget(input, widget, sx, sy);
2056}
2057
2058static void
2059pointer_handle_leave(void *data, struct wl_pointer *pointer,
2060 uint32_t serial, struct wl_surface *surface)
2061{
2062 struct input *input = data;
2063
2064 input->display->serial = serial;
2065 input_remove_pointer_focus(input);
2066}
2067
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002068static void
Daniel Stone37816df2012-05-16 18:45:18 +01002069pointer_handle_motion(void *data, struct wl_pointer *pointer,
2070 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002071{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002072 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002073 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002074 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002075 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002076 float sx = wl_fixed_to_double(sx_w);
2077 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002078
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002079 input->sx = sx;
2080 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002081
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002082 if (!window)
2083 return;
2084
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002085 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002086 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002087 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002088 }
2089
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002090 if (input->grab)
2091 widget = input->grab;
2092 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002093 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002094 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002095 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002096 input, time, sx, sy,
2097 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002098
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002099 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002100}
2101
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002102static void
Daniel Stone37816df2012-05-16 18:45:18 +01002103pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002104 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002105{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002106 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002107 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002108 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002109
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002110 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002111 if (input->focus_widget && input->grab == NULL &&
2112 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002113 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002114
Neil Roberts6b28aad2012-01-23 19:11:18 +00002115 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002117 (*widget->button_handler)(widget,
2118 input, time,
2119 button, state,
2120 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002121
Daniel Stone4dbadb12012-05-30 16:31:51 +01002122 if (input->grab && input->grab_button == button &&
2123 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002124 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002125}
2126
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002127static void
Daniel Stone37816df2012-05-16 18:45:18 +01002128pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002129 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002130{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002131 struct input *input = data;
2132 struct widget *widget;
2133
2134 widget = input->focus_widget;
2135 if (input->grab)
2136 widget = input->grab;
2137 if (widget && widget->axis_handler)
2138 (*widget->axis_handler)(widget,
2139 input, time,
2140 axis, value,
2141 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002142}
2143
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002144static const struct wl_pointer_listener pointer_listener = {
2145 pointer_handle_enter,
2146 pointer_handle_leave,
2147 pointer_handle_motion,
2148 pointer_handle_button,
2149 pointer_handle_axis,
2150};
2151
2152static void
2153input_remove_keyboard_focus(struct input *input)
2154{
2155 struct window *window = input->keyboard_focus;
2156 struct itimerspec its;
2157
2158 its.it_interval.tv_sec = 0;
2159 its.it_interval.tv_nsec = 0;
2160 its.it_value.tv_sec = 0;
2161 its.it_value.tv_nsec = 0;
2162 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2163
2164 if (!window)
2165 return;
2166
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002167 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002168 if (window->keyboard_focus_handler)
2169 (*window->keyboard_focus_handler)(window, NULL,
2170 window->user_data);
2171
2172 input->keyboard_focus = NULL;
2173}
2174
2175static void
2176keyboard_repeat_func(struct task *task, uint32_t events)
2177{
2178 struct input *input =
2179 container_of(task, struct input, repeat_task);
2180 struct window *window = input->keyboard_focus;
2181 uint64_t exp;
2182
2183 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2184 /* If we change the timer between the fd becoming
2185 * readable and getting here, there'll be nothing to
2186 * read and we get EAGAIN. */
2187 return;
2188
2189 if (window && window->key_handler) {
2190 (*window->key_handler)(window, input, input->repeat_time,
2191 input->repeat_key, input->repeat_sym,
2192 WL_KEYBOARD_KEY_STATE_PRESSED,
2193 window->user_data);
2194 }
2195}
2196
2197static void
2198keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2199 uint32_t format, int fd, uint32_t size)
2200{
2201 struct input *input = data;
2202 char *map_str;
2203
2204 if (!data) {
2205 close(fd);
2206 return;
2207 }
2208
2209 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2210 close(fd);
2211 return;
2212 }
2213
2214 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2215 if (map_str == MAP_FAILED) {
2216 close(fd);
2217 return;
2218 }
2219
2220 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2221 map_str,
2222 XKB_KEYMAP_FORMAT_TEXT_V1,
2223 0);
2224 munmap(map_str, size);
2225 close(fd);
2226
2227 if (!input->xkb.keymap) {
2228 fprintf(stderr, "failed to compile keymap\n");
2229 return;
2230 }
2231
2232 input->xkb.state = xkb_state_new(input->xkb.keymap);
2233 if (!input->xkb.state) {
2234 fprintf(stderr, "failed to create XKB state\n");
2235 xkb_map_unref(input->xkb.keymap);
2236 input->xkb.keymap = NULL;
2237 return;
2238 }
2239
2240 input->xkb.control_mask =
2241 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2242 input->xkb.alt_mask =
2243 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2244 input->xkb.shift_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2246}
2247
2248static void
2249keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2250 uint32_t serial, struct wl_surface *surface,
2251 struct wl_array *keys)
2252{
2253 struct input *input = data;
2254 struct window *window;
2255
2256 input->display->serial = serial;
2257 input->keyboard_focus = wl_surface_get_user_data(surface);
2258
2259 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002260 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002261 if (window->keyboard_focus_handler)
2262 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002263 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002264}
2265
2266static void
2267keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2268 uint32_t serial, struct wl_surface *surface)
2269{
2270 struct input *input = data;
2271
2272 input->display->serial = serial;
2273 input_remove_keyboard_focus(input);
2274}
2275
Scott Moreau210d0792012-03-22 10:47:01 -06002276static void
Daniel Stone37816df2012-05-16 18:45:18 +01002277keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002278 uint32_t serial, uint32_t time, uint32_t key,
2279 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002280{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002281 struct input *input = data;
2282 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002283 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002284 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002285 const xkb_keysym_t *syms;
2286 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002287 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002288
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002289 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002290 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002291 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002292 return;
2293
Daniel Stone97f68542012-05-30 16:32:01 +01002294 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002295
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002296 sym = XKB_KEY_NoSymbol;
2297 if (num_syms == 1)
2298 sym = syms[0];
2299
2300 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002301 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002302 window_set_maximized(window,
2303 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002304 } else if (sym == XKB_KEY_F11 &&
2305 window->fullscreen_handler &&
2306 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2307 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002308 } else if (sym == XKB_KEY_F4 &&
2309 input->modifiers == MOD_ALT_MASK &&
2310 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2311 if (window->close_handler)
2312 window->close_handler(window->parent,
2313 window->user_data);
2314 else
2315 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002316 } else if (window->key_handler) {
2317 (*window->key_handler)(window, input, time, key,
2318 sym, state, window->user_data);
2319 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002320
2321 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2322 key == input->repeat_key) {
2323 its.it_interval.tv_sec = 0;
2324 its.it_interval.tv_nsec = 0;
2325 its.it_value.tv_sec = 0;
2326 its.it_value.tv_nsec = 0;
2327 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2328 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2329 input->repeat_sym = sym;
2330 input->repeat_key = key;
2331 input->repeat_time = time;
2332 its.it_interval.tv_sec = 0;
2333 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2334 its.it_value.tv_sec = 0;
2335 its.it_value.tv_nsec = 400 * 1000 * 1000;
2336 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2337 }
2338}
2339
2340static void
Daniel Stone351eb612012-05-31 15:27:47 -04002341keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2342 uint32_t serial, uint32_t mods_depressed,
2343 uint32_t mods_latched, uint32_t mods_locked,
2344 uint32_t group)
2345{
2346 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002347 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002348
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002349 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2350 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002351 mask = xkb_state_serialize_mods(input->xkb.state,
2352 XKB_STATE_DEPRESSED |
2353 XKB_STATE_LATCHED);
2354 input->modifiers = 0;
2355 if (mask & input->xkb.control_mask)
2356 input->modifiers |= MOD_CONTROL_MASK;
2357 if (mask & input->xkb.alt_mask)
2358 input->modifiers |= MOD_ALT_MASK;
2359 if (mask & input->xkb.shift_mask)
2360 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002361}
2362
Daniel Stone37816df2012-05-16 18:45:18 +01002363static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002364 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002365 keyboard_handle_enter,
2366 keyboard_handle_leave,
2367 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002368 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002369};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002370
2371static void
Daniel Stone37816df2012-05-16 18:45:18 +01002372seat_handle_capabilities(void *data, struct wl_seat *seat,
2373 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002374{
Daniel Stone37816df2012-05-16 18:45:18 +01002375 struct input *input = data;
2376
2377 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2378 input->pointer = wl_seat_get_pointer(seat);
2379 wl_pointer_set_user_data(input->pointer, input);
2380 wl_pointer_add_listener(input->pointer, &pointer_listener,
2381 input);
2382 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2383 wl_pointer_destroy(input->pointer);
2384 input->pointer = NULL;
2385 }
2386
2387 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2388 input->keyboard = wl_seat_get_keyboard(seat);
2389 wl_keyboard_set_user_data(input->keyboard, input);
2390 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2391 input);
2392 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2393 wl_keyboard_destroy(input->keyboard);
2394 input->keyboard = NULL;
2395 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002396}
2397
Daniel Stone37816df2012-05-16 18:45:18 +01002398static const struct wl_seat_listener seat_listener = {
2399 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002400};
2401
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002402void
2403input_get_position(struct input *input, int32_t *x, int32_t *y)
2404{
2405 *x = input->sx;
2406 *y = input->sy;
2407}
2408
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002409struct display *
2410input_get_display(struct input *input)
2411{
2412 return input->display;
2413}
2414
Daniel Stone37816df2012-05-16 18:45:18 +01002415struct wl_seat *
2416input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002417{
Daniel Stone37816df2012-05-16 18:45:18 +01002418 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002419}
2420
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002421uint32_t
2422input_get_modifiers(struct input *input)
2423{
2424 return input->modifiers;
2425}
2426
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002427struct widget *
2428input_get_focus_widget(struct input *input)
2429{
2430 return input->focus_widget;
2431}
2432
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002433struct data_offer {
2434 struct wl_data_offer *offer;
2435 struct input *input;
2436 struct wl_array types;
2437 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002438
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002439 struct task io_task;
2440 int fd;
2441 data_func_t func;
2442 int32_t x, y;
2443 void *user_data;
2444};
2445
2446static void
2447data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2448{
2449 struct data_offer *offer = data;
2450 char **p;
2451
2452 p = wl_array_add(&offer->types, sizeof *p);
2453 *p = strdup(type);
2454}
2455
2456static const struct wl_data_offer_listener data_offer_listener = {
2457 data_offer_offer,
2458};
2459
2460static void
2461data_offer_destroy(struct data_offer *offer)
2462{
2463 char **p;
2464
2465 offer->refcount--;
2466 if (offer->refcount == 0) {
2467 wl_data_offer_destroy(offer->offer);
2468 for (p = offer->types.data; *p; p++)
2469 free(*p);
2470 wl_array_release(&offer->types);
2471 free(offer);
2472 }
2473}
2474
2475static void
2476data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002477 struct wl_data_device *data_device,
2478 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002479{
2480 struct data_offer *offer;
2481
2482 offer = malloc(sizeof *offer);
2483
2484 wl_array_init(&offer->types);
2485 offer->refcount = 1;
2486 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002487 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002488 wl_data_offer_add_listener(offer->offer,
2489 &data_offer_listener, offer);
2490}
2491
2492static void
2493data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002494 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002495 wl_fixed_t x_w, wl_fixed_t y_w,
2496 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002497{
2498 struct input *input = data;
2499 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002500 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002501 float x = wl_fixed_to_double(x_w);
2502 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002503 char **p;
2504
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002505 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002506 window = wl_surface_get_user_data(surface);
2507 input->pointer_focus = window;
2508
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002509 if (offer) {
2510 input->drag_offer = wl_data_offer_get_user_data(offer);
2511
2512 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2513 *p = NULL;
2514
2515 types_data = input->drag_offer->types.data;
2516 } else {
2517 input->drag_offer = NULL;
2518 types_data = NULL;
2519 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002520
2521 window = input->pointer_focus;
2522 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002523 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002524 window->user_data);
2525}
2526
2527static void
2528data_device_leave(void *data, struct wl_data_device *data_device)
2529{
2530 struct input *input = data;
2531
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002532 if (input->drag_offer) {
2533 data_offer_destroy(input->drag_offer);
2534 input->drag_offer = NULL;
2535 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002536}
2537
2538static void
2539data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002540 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002541{
2542 struct input *input = data;
2543 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002544 float x = wl_fixed_to_double(x_w);
2545 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002546 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002547
2548 input->sx = x;
2549 input->sy = y;
2550
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002551 if (input->drag_offer)
2552 types_data = input->drag_offer->types.data;
2553 else
2554 types_data = NULL;
2555
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002556 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002557 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002558 window->user_data);
2559}
2560
2561static void
2562data_device_drop(void *data, struct wl_data_device *data_device)
2563{
2564 struct input *input = data;
2565 struct window *window = input->pointer_focus;
2566
2567 if (window->drop_handler)
2568 window->drop_handler(window, input,
2569 input->sx, input->sy, window->user_data);
2570}
2571
2572static void
2573data_device_selection(void *data,
2574 struct wl_data_device *wl_data_device,
2575 struct wl_data_offer *offer)
2576{
2577 struct input *input = data;
2578 char **p;
2579
2580 if (input->selection_offer)
2581 data_offer_destroy(input->selection_offer);
2582
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002583 if (offer) {
2584 input->selection_offer = wl_data_offer_get_user_data(offer);
2585 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2586 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002587 } else {
2588 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002589 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002590}
2591
2592static const struct wl_data_device_listener data_device_listener = {
2593 data_device_data_offer,
2594 data_device_enter,
2595 data_device_leave,
2596 data_device_motion,
2597 data_device_drop,
2598 data_device_selection
2599};
2600
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002601static void
2602input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002603{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002604 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002605 struct wl_cursor *cursor;
2606 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002608 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002609 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002610 return;
2611
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002612 if (index >= (int) cursor->image_count) {
2613 fprintf(stderr, "cursor index out of range\n");
2614 return;
2615 }
2616
2617 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002618 buffer = wl_cursor_image_get_buffer(image);
2619 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002620 return;
2621
Kristian Høgsbergae277372012-08-01 09:41:08 -04002622 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002623 input->pointer_surface,
2624 image->hotspot_x, image->hotspot_y);
2625 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2626 wl_surface_damage(input->pointer_surface, 0, 0,
2627 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002628 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002629}
2630
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002631static const struct wl_callback_listener pointer_surface_listener;
2632
2633static void
2634pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2635 uint32_t time)
2636{
2637 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002638 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002639 int i;
2640
2641 if (callback) {
2642 assert(callback == input->cursor_frame_cb);
2643 wl_callback_destroy(callback);
2644 input->cursor_frame_cb = NULL;
2645 }
2646
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002647 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002648 wl_pointer_set_cursor(input->pointer,
2649 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002650 NULL, 0, 0);
2651 return;
2652 }
2653
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002654 if (input->current_cursor == CURSOR_UNSET)
2655 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002656 cursor = input->display->cursors[input->current_cursor];
2657 if (!cursor)
2658 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002659
2660 /* FIXME We don't have the current time on the first call so we set
2661 * the animation start to the time of the first frame callback. */
2662 if (time == 0)
2663 input->cursor_anim_start = 0;
2664 else if (input->cursor_anim_start == 0)
2665 input->cursor_anim_start = time;
2666
2667 if (time == 0 || input->cursor_anim_start == 0)
2668 i = 0;
2669 else
2670 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2671
Pekka Paalanenbc106382012-10-10 12:49:31 +03002672 if (cursor->image_count > 1) {
2673 input->cursor_frame_cb =
2674 wl_surface_frame(input->pointer_surface);
2675 wl_callback_add_listener(input->cursor_frame_cb,
2676 &pointer_surface_listener, input);
2677 }
2678
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002679 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002680}
2681
2682static const struct wl_callback_listener pointer_surface_listener = {
2683 pointer_surface_frame_callback
2684};
2685
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002686void
2687input_set_pointer_image(struct input *input, int pointer)
2688{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002689 int force = 0;
2690
2691 if (input->pointer_enter_serial > input->cursor_serial)
2692 force = 1;
2693
2694 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002695 return;
2696
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002697 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002698 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002699 if (!input->cursor_frame_cb)
2700 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002701 else if (force) {
2702 /* The current frame callback may be stuck if, for instance,
2703 * the set cursor request was processed by the server after
2704 * this client lost the focus. In this case the cursor surface
2705 * might not be mapped and the frame callback wouldn't ever
2706 * complete. Send a set_cursor and attach to try to map the
2707 * cursor surface again so that the callback will finish */
2708 input_set_pointer_image_index(input, 0);
2709 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002710}
2711
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002712struct wl_data_device *
2713input_get_data_device(struct input *input)
2714{
2715 return input->data_device;
2716}
2717
2718void
2719input_set_selection(struct input *input,
2720 struct wl_data_source *source, uint32_t time)
2721{
2722 wl_data_device_set_selection(input->data_device, source, time);
2723}
2724
2725void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002726input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002727{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002728 wl_data_offer_accept(input->drag_offer->offer,
2729 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002730}
2731
2732static void
2733offer_io_func(struct task *task, uint32_t events)
2734{
2735 struct data_offer *offer =
2736 container_of(task, struct data_offer, io_task);
2737 unsigned int len;
2738 char buffer[4096];
2739
2740 len = read(offer->fd, buffer, sizeof buffer);
2741 offer->func(buffer, len,
2742 offer->x, offer->y, offer->user_data);
2743
2744 if (len == 0) {
2745 close(offer->fd);
2746 data_offer_destroy(offer);
2747 }
2748}
2749
2750static void
2751data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2752 data_func_t func, void *user_data)
2753{
2754 int p[2];
2755
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002756 if (pipe2(p, O_CLOEXEC) == -1)
2757 return;
2758
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002759 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2760 close(p[1]);
2761
2762 offer->io_task.run = offer_io_func;
2763 offer->fd = p[0];
2764 offer->func = func;
2765 offer->refcount++;
2766 offer->user_data = user_data;
2767
2768 display_watch_fd(offer->input->display,
2769 offer->fd, EPOLLIN, &offer->io_task);
2770}
2771
2772void
2773input_receive_drag_data(struct input *input, const char *mime_type,
2774 data_func_t func, void *data)
2775{
2776 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2777 input->drag_offer->x = input->sx;
2778 input->drag_offer->y = input->sy;
2779}
2780
2781int
2782input_receive_selection_data(struct input *input, const char *mime_type,
2783 data_func_t func, void *data)
2784{
2785 char **p;
2786
2787 if (input->selection_offer == NULL)
2788 return -1;
2789
2790 for (p = input->selection_offer->types.data; *p; p++)
2791 if (strcmp(mime_type, *p) == 0)
2792 break;
2793
2794 if (*p == NULL)
2795 return -1;
2796
2797 data_offer_receive_data(input->selection_offer,
2798 mime_type, func, data);
2799 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002800}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002801
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002802int
2803input_receive_selection_data_to_fd(struct input *input,
2804 const char *mime_type, int fd)
2805{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002806 if (input->selection_offer)
2807 wl_data_offer_receive(input->selection_offer->offer,
2808 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002809
2810 return 0;
2811}
2812
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002813void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002814window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002815{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002816 if (!window->shell_surface)
2817 return;
2818
Daniel Stone37816df2012-05-16 18:45:18 +01002819 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002820}
2821
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002822static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002823idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002824{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002825 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002826
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002827 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002828 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002829 widget_set_allocation(widget,
2830 window->pending_allocation.x,
2831 window->pending_allocation.y,
2832 window->pending_allocation.width,
2833 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002834
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002835 if (window->input_region) {
2836 wl_region_destroy(window->input_region);
2837 window->input_region = NULL;
2838 }
2839
2840 if (window->opaque_region) {
2841 wl_region_destroy(window->opaque_region);
2842 window->opaque_region = NULL;
2843 }
2844
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002845 if (widget->resize_handler)
2846 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002847 widget->allocation.width,
2848 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002849 widget->user_data);
2850
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002851 if (window->allocation.width != widget->allocation.width ||
2852 window->allocation.height != widget->allocation.height) {
2853 window->allocation = widget->allocation;
2854 window_schedule_redraw(window);
2855 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002856}
2857
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002858void
2859window_schedule_resize(struct window *window, int width, int height)
2860{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002861 window->pending_allocation.x = 0;
2862 window->pending_allocation.y = 0;
2863 window->pending_allocation.width = width;
2864 window->pending_allocation.height = height;
2865
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002866 if (window->min_allocation.width == 0)
2867 window->min_allocation = window->pending_allocation;
2868 if (window->pending_allocation.width < window->min_allocation.width)
2869 window->pending_allocation.width = window->min_allocation.width;
2870 if (window->pending_allocation.height < window->min_allocation.height)
2871 window->pending_allocation.height = window->min_allocation.height;
2872
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002873 window->resize_needed = 1;
2874 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002875}
2876
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002877void
2878widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2879{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002880 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002881}
2882
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002883static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002884handle_ping(void *data, struct wl_shell_surface *shell_surface,
2885 uint32_t serial)
2886{
2887 wl_shell_surface_pong(shell_surface, serial);
2888}
2889
2890static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002891handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002892 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002893{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002894 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002895
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002896 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002897 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002898}
2899
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002900static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002901menu_destroy(struct menu *menu)
2902{
2903 widget_destroy(menu->widget);
2904 window_destroy(menu->window);
2905 free(menu);
2906}
2907
2908static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002909handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2910{
2911 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002912 struct menu *menu = window->widget->user_data;
2913
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002914 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002915 * device. Or just use wl_callback. And this really needs to
2916 * be a window vfunc that the menu can set. And we need the
2917 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002918
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002919 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002920 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002921 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922}
2923
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002924static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002925 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002926 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002927 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002928};
2929
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002930void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002931window_get_allocation(struct window *window,
2932 struct rectangle *allocation)
2933{
2934 *allocation = window->allocation;
2935}
2936
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002937static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002938widget_redraw(struct widget *widget)
2939{
2940 struct widget *child;
2941
2942 if (widget->redraw_handler)
2943 widget->redraw_handler(widget, widget->user_data);
2944 wl_list_for_each(child, &widget->child_list, link)
2945 widget_redraw(child);
2946}
2947
2948static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002949frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2950{
2951 struct window *window = data;
2952
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002953 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002954 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002955 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002956 window->redraw_scheduled = 0;
2957 if (window->redraw_needed)
2958 window_schedule_redraw(window);
2959}
2960
2961static const struct wl_callback_listener listener = {
2962 frame_callback
2963};
2964
2965static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002966idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002967{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002968 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002969
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002970 if (window->resize_needed)
2971 idle_resize(window);
2972
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002973 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002974 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002975 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002976 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002977
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002978 window->frame_cb = wl_surface_frame(window->surface);
2979 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002980 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002981}
2982
2983void
2984window_schedule_redraw(struct window *window)
2985{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002986 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002987 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002988 window->redraw_task.run = idle_redraw;
2989 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002990 window->redraw_scheduled = 1;
2991 }
2992}
2993
Kristian Høgsberg1671e112012-10-10 11:36:24 -04002994int
2995window_is_fullscreen(struct window *window)
2996{
2997 return window->type == TYPE_FULLSCREEN;
2998}
2999
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003000void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003001window_set_fullscreen(struct window *window, int fullscreen)
3002{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003003 if (!window->display->shell)
3004 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003005
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003006 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003007 return;
3008
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003009 if (fullscreen) {
3010 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003011 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003012 wl_shell_surface_set_fullscreen(window->shell_surface,
3013 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3014 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003015 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003016 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003017 wl_shell_surface_set_toplevel(window->shell_surface);
3018 window_schedule_resize(window,
3019 window->saved_allocation.width,
3020 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003021 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003022}
3023
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003024int
3025window_is_maximized(struct window *window)
3026{
3027 return window->type == TYPE_MAXIMIZED;
3028}
3029
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003030void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003031window_set_maximized(struct window *window, int maximized)
3032{
3033 if (!window->display->shell)
3034 return;
3035
3036 if ((window->type == TYPE_MAXIMIZED) == maximized)
3037 return;
3038
3039 if (window->type == TYPE_TOPLEVEL) {
3040 window->saved_allocation = window->allocation;
3041 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3042 window->type = TYPE_MAXIMIZED;
3043 } else {
3044 wl_shell_surface_set_toplevel(window->shell_surface);
3045 window->type = TYPE_TOPLEVEL;
3046 window_schedule_resize(window,
3047 window->saved_allocation.width,
3048 window->saved_allocation.height);
3049 }
3050}
3051
3052void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003053window_set_user_data(struct window *window, void *data)
3054{
3055 window->user_data = data;
3056}
3057
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003058void *
3059window_get_user_data(struct window *window)
3060{
3061 return window->user_data;
3062}
3063
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003064void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003065window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003066 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003067{
3068 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003069}
3070
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003071void
3072window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003073 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003074{
3075 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003076}
3077
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003078void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003079window_set_data_handler(struct window *window, window_data_handler_t handler)
3080{
3081 window->data_handler = handler;
3082}
3083
3084void
3085window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3086{
3087 window->drop_handler = handler;
3088}
3089
3090void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003091window_set_close_handler(struct window *window,
3092 window_close_handler_t handler)
3093{
3094 window->close_handler = handler;
3095}
3096
3097void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003098window_set_fullscreen_handler(struct window *window,
3099 window_fullscreen_handler_t handler)
3100{
3101 window->fullscreen_handler = handler;
3102}
3103
3104void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003105window_set_title(struct window *window, const char *title)
3106{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003107 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003108 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003109 if (window->shell_surface)
3110 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003111}
3112
3113const char *
3114window_get_title(struct window *window)
3115{
3116 return window->title;
3117}
3118
3119void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003120window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3121{
3122 struct text_cursor_position *text_cursor_position =
3123 window->display->text_cursor_position;
3124
Scott Moreau9295ce02012-06-01 12:46:10 -06003125 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003126 return;
3127
3128 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003129 window->surface,
3130 wl_fixed_from_int(x),
3131 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003132}
3133
3134void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003135window_damage(struct window *window, int32_t x, int32_t y,
3136 int32_t width, int32_t height)
3137{
3138 wl_surface_damage(window->surface, x, y, width, height);
3139}
3140
Casey Dahlin9074db52012-04-19 22:50:09 -04003141static void
3142surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003143 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003144{
Rob Bradford7507b572012-05-15 17:55:34 +01003145 struct window *window = data;
3146 struct output *output;
3147 struct output *output_found = NULL;
3148 struct window_output *window_output;
3149
3150 wl_list_for_each(output, &window->display->output_list, link) {
3151 if (output->output == wl_output) {
3152 output_found = output;
3153 break;
3154 }
3155 }
3156
3157 if (!output_found)
3158 return;
3159
3160 window_output = malloc (sizeof *window_output);
3161 window_output->output = output_found;
3162
3163 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003164}
3165
3166static void
3167surface_leave(void *data,
3168 struct wl_surface *wl_surface, struct wl_output *output)
3169{
Rob Bradford7507b572012-05-15 17:55:34 +01003170 struct window *window = data;
3171 struct window_output *window_output;
3172 struct window_output *window_output_found = NULL;
3173
3174 wl_list_for_each(window_output, &window->window_output_list, link) {
3175 if (window_output->output->output == output) {
3176 window_output_found = window_output;
3177 break;
3178 }
3179 }
3180
3181 if (window_output_found) {
3182 wl_list_remove(&window_output_found->link);
3183 free(window_output_found);
3184 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003185}
3186
3187static const struct wl_surface_listener surface_listener = {
3188 surface_enter,
3189 surface_leave
3190};
3191
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003192static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003193window_create_internal(struct display *display,
3194 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003195{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003196 struct window *window;
3197
3198 window = malloc(sizeof *window);
3199 if (window == NULL)
3200 return NULL;
3201
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003202 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003203 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003204 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003205 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003206 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003207 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003208 window->shell_surface =
3209 wl_shell_get_shell_surface(display->shell,
3210 window->surface);
3211 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003212 window->allocation.x = 0;
3213 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003214 window->allocation.width = 0;
3215 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003216 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003217 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003218 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003219 window->input_region = NULL;
3220 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003221
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003222 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003223#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003224 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003225#else
3226 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3227#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003228 else
3229 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003230
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003231 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003232 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003233 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003234
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003235 if (window->shell_surface) {
3236 wl_shell_surface_set_user_data(window->shell_surface, window);
3237 wl_shell_surface_add_listener(window->shell_surface,
3238 &shell_surface_listener, window);
3239 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003240
Rob Bradford7507b572012-05-15 17:55:34 +01003241 wl_list_init (&window->window_output_list);
3242
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003243 return window;
3244}
3245
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003246struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003247window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003248{
3249 struct window *window;
3250
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003251 window = window_create_internal(display, NULL, TYPE_NONE);
3252 if (!window)
3253 return NULL;
3254
3255 return window;
3256}
3257
3258struct window *
3259window_create_custom(struct display *display)
3260{
3261 struct window *window;
3262
3263 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003264 if (!window)
3265 return NULL;
3266
3267 return window;
3268}
3269
3270struct window *
3271window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003272 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003273{
3274 struct window *window;
3275
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003276 window = window_create_internal(parent->display,
3277 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003278 if (!window)
3279 return NULL;
3280
3281 window->x = x;
3282 window->y = y;
3283
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003284 if (display->shell)
3285 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003286 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003287 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003288
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003289 return window;
3290}
3291
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003292static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003293menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003294{
3295 int next;
3296
3297 next = (sy - 8) / 20;
3298 if (menu->current != next) {
3299 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003300 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003301 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003302}
3303
3304static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003305menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003306 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003307 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003308{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003309 struct menu *menu = data;
3310
3311 if (widget == menu->widget)
3312 menu_set_item(data, y);
3313
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003314 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315}
3316
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003317static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003318menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003319 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003320{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003321 struct menu *menu = data;
3322
3323 if (widget == menu->widget)
3324 menu_set_item(data, y);
3325
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003326 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003327}
3328
3329static void
3330menu_leave_handler(struct widget *widget, struct input *input, void *data)
3331{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003332 struct menu *menu = data;
3333
3334 if (widget == menu->widget)
3335 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003336}
3337
3338static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003339menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003340 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003341 uint32_t button, enum wl_pointer_button_state state,
3342 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003343
3344{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003345 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003346
Daniel Stone4dbadb12012-05-30 16:31:51 +01003347 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3348 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003349 /* Either relase after press-drag-release or
3350 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003351 menu->func(menu->window->parent,
3352 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003353 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003354 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003355 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003356}
3357
3358static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003359menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003360{
3361 cairo_t *cr;
3362 const int32_t r = 3, margin = 3;
3363 struct menu *menu = data;
3364 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003365 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003367 cr = cairo_create(window->cairo_surface);
3368 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3369 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3370 cairo_paint(cr);
3371
3372 width = window->allocation.width;
3373 height = window->allocation.height;
3374 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003375
3376 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003377 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3378 cairo_fill(cr);
3379
3380 for (i = 0; i < menu->count; i++) {
3381 if (i == menu->current) {
3382 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3383 cairo_rectangle(cr, margin, i * 20 + margin,
3384 width - 2 * margin, 20);
3385 cairo_fill(cr);
3386 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3387 cairo_move_to(cr, 10, i * 20 + 16);
3388 cairo_show_text(cr, menu->entries[i]);
3389 } else {
3390 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3391 cairo_move_to(cr, 10, i * 20 + 16);
3392 cairo_show_text(cr, menu->entries[i]);
3393 }
3394 }
3395
3396 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003397}
3398
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003399void
3400window_show_menu(struct display *display,
3401 struct input *input, uint32_t time, struct window *parent,
3402 int32_t x, int32_t y,
3403 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003404{
3405 struct window *window;
3406 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003407 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003408
3409 menu = malloc(sizeof *menu);
3410 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003411 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003412
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003413 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003414 if (!window) {
3415 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003416 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003417 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003418
3419 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003420 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003421 menu->entries = entries;
3422 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003423 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003424 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003425 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003426 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003427 window->type = TYPE_MENU;
3428 window->x = x;
3429 window->y = y;
3430
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003431 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003432 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003433 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003434 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003435 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003436
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003437 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003438 widget_set_enter_handler(menu->widget, menu_enter_handler);
3439 widget_set_leave_handler(menu->widget, menu_leave_handler);
3440 widget_set_motion_handler(menu->widget, menu_motion_handler);
3441 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003442
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003443 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003444 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003445}
3446
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003447void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003448window_set_buffer_type(struct window *window, enum window_buffer_type type)
3449{
3450 window->buffer_type = type;
3451}
3452
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003453
3454static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003455display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003456 struct wl_output *wl_output,
3457 int x, int y,
3458 int physical_width,
3459 int physical_height,
3460 int subpixel,
3461 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003462 const char *model,
3463 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003464{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003465 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003466
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003467 output->allocation.x = x;
3468 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003469 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003470}
3471
3472static void
3473display_handle_mode(void *data,
3474 struct wl_output *wl_output,
3475 uint32_t flags,
3476 int width,
3477 int height,
3478 int refresh)
3479{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003480 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003481 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003482
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003483 if (flags & WL_OUTPUT_MODE_CURRENT) {
3484 output->allocation.width = width;
3485 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003486 if (display->output_configure_handler)
3487 (*display->output_configure_handler)(
3488 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003489 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003490}
3491
3492static const struct wl_output_listener output_listener = {
3493 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003494 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003495};
3496
3497static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003498display_add_output(struct display *d, uint32_t id)
3499{
3500 struct output *output;
3501
3502 output = malloc(sizeof *output);
3503 if (output == NULL)
3504 return;
3505
3506 memset(output, 0, sizeof *output);
3507 output->display = d;
3508 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003509 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003510 wl_list_insert(d->output_list.prev, &output->link);
3511
3512 wl_output_add_listener(output->output, &output_listener, output);
3513}
3514
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003515static void
3516output_destroy(struct output *output)
3517{
3518 if (output->destroy_handler)
3519 (*output->destroy_handler)(output, output->user_data);
3520
3521 wl_output_destroy(output->output);
3522 wl_list_remove(&output->link);
3523 free(output);
3524}
3525
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003526void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003527display_set_global_handler(struct display *display,
3528 display_global_handler_t handler)
3529{
3530 struct global *global;
3531
3532 display->global_handler = handler;
3533 if (!handler)
3534 return;
3535
3536 wl_list_for_each(global, &display->global_list, link)
3537 display->global_handler(display,
3538 global->name, global->interface,
3539 global->version, display->user_data);
3540}
3541
3542void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003543display_set_output_configure_handler(struct display *display,
3544 display_output_handler_t handler)
3545{
3546 struct output *output;
3547
3548 display->output_configure_handler = handler;
3549 if (!handler)
3550 return;
3551
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003552 wl_list_for_each(output, &display->output_list, link) {
3553 if (output->allocation.width == 0 &&
3554 output->allocation.height == 0)
3555 continue;
3556
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003557 (*display->output_configure_handler)(output,
3558 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003559 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003560}
3561
3562void
3563output_set_user_data(struct output *output, void *data)
3564{
3565 output->user_data = data;
3566}
3567
3568void *
3569output_get_user_data(struct output *output)
3570{
3571 return output->user_data;
3572}
3573
3574void
3575output_set_destroy_handler(struct output *output,
3576 display_output_handler_t handler)
3577{
3578 output->destroy_handler = handler;
3579 /* FIXME: implement this, once we have way to remove outputs */
3580}
3581
3582void
Scott Moreau4e072362012-09-29 02:03:11 -06003583output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003584{
Scott Moreau4e072362012-09-29 02:03:11 -06003585 struct rectangle allocation = output->allocation;
3586
3587 switch (output->transform) {
3588 case WL_OUTPUT_TRANSFORM_90:
3589 case WL_OUTPUT_TRANSFORM_270:
3590 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3591 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3592 /* Swap width and height */
3593 allocation.width = output->allocation.height;
3594 allocation.height = output->allocation.width;
3595 break;
3596 }
3597
3598 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003599}
3600
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003601struct wl_output *
3602output_get_wl_output(struct output *output)
3603{
3604 return output->output;
3605}
3606
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003607static void
Daniel Stone97f68542012-05-30 16:32:01 +01003608fini_xkb(struct input *input)
3609{
3610 xkb_state_unref(input->xkb.state);
3611 xkb_map_unref(input->xkb.keymap);
3612}
3613
3614static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003615display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003616{
3617 struct input *input;
3618
3619 input = malloc(sizeof *input);
3620 if (input == NULL)
3621 return;
3622
3623 memset(input, 0, sizeof *input);
3624 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003625 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003626 input->pointer_focus = NULL;
3627 input->keyboard_focus = NULL;
3628 wl_list_insert(d->input_list.prev, &input->link);
3629
Daniel Stone37816df2012-05-16 18:45:18 +01003630 wl_seat_add_listener(input->seat, &seat_listener, input);
3631 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003632
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003633 input->data_device =
3634 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003635 input->seat);
3636 wl_data_device_add_listener(input->data_device, &data_device_listener,
3637 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003638
3639 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003640
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003641 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3642 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003643 input->repeat_task.run = keyboard_repeat_func;
3644 display_watch_fd(d, input->repeat_timer_fd,
3645 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003646}
3647
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003648static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003649input_destroy(struct input *input)
3650{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003651 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003652 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003653
3654 if (input->drag_offer)
3655 data_offer_destroy(input->drag_offer);
3656
3657 if (input->selection_offer)
3658 data_offer_destroy(input->selection_offer);
3659
3660 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003661 fini_xkb(input);
3662
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003663 wl_surface_destroy(input->pointer_surface);
3664
Pekka Paalanene1207c72011-12-16 12:02:09 +02003665 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003666 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003667 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003668 free(input);
3669}
3670
3671static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003672init_workspace_manager(struct display *d, uint32_t id)
3673{
3674 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003675 wl_registry_bind(d->registry, id,
3676 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003677 if (d->workspace_manager != NULL)
3678 workspace_manager_add_listener(d->workspace_manager,
3679 &workspace_manager_listener,
3680 d);
3681}
3682
3683static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003684registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3685 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003686{
3687 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003688 struct global *global;
3689
3690 global = malloc(sizeof *global);
3691 global->name = id;
3692 global->interface = strdup(interface);
3693 global->version = version;
3694 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003695
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003696 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003697 d->compositor = wl_registry_bind(registry, id,
3698 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003699 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003700 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003701 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003702 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003703 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003704 d->shell = wl_registry_bind(registry,
3705 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003706 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003707 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003708 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3709 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003710 wl_registry_bind(registry, id,
3711 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003712 } else if (strcmp(interface, "text_cursor_position") == 0) {
3713 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003714 wl_registry_bind(registry, id,
3715 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003716 } else if (strcmp(interface, "workspace_manager") == 0) {
3717 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003718 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003719
3720 if (d->global_handler)
3721 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003722}
3723
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003724void *
3725display_bind(struct display *display, uint32_t name,
3726 const struct wl_interface *interface, uint32_t version)
3727{
3728 return wl_registry_bind(display->registry, name, interface, version);
3729}
3730
3731static const struct wl_registry_listener registry_listener = {
3732 registry_handle_global
3733};
3734
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003735#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003736static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003737init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003738{
3739 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003740 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003741
Rob Clark6396ed32012-03-11 19:48:41 -05003742#ifdef USE_CAIRO_GLESV2
3743# define GL_BIT EGL_OPENGL_ES2_BIT
3744#else
3745# define GL_BIT EGL_OPENGL_BIT
3746#endif
3747
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003748 static const EGLint argb_cfg_attribs[] = {
3749 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003750 EGL_RED_SIZE, 1,
3751 EGL_GREEN_SIZE, 1,
3752 EGL_BLUE_SIZE, 1,
3753 EGL_ALPHA_SIZE, 1,
3754 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003755 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003756 EGL_NONE
3757 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003758
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003759#ifdef USE_CAIRO_GLESV2
3760 static const EGLint context_attribs[] = {
3761 EGL_CONTEXT_CLIENT_VERSION, 2,
3762 EGL_NONE
3763 };
3764 EGLint api = EGL_OPENGL_ES_API;
3765#else
3766 EGLint *context_attribs = NULL;
3767 EGLint api = EGL_OPENGL_API;
3768#endif
3769
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003770 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003771 if (!eglInitialize(d->dpy, &major, &minor)) {
3772 fprintf(stderr, "failed to initialize display\n");
3773 return -1;
3774 }
3775
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003776 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003777 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3778 return -1;
3779 }
3780
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003781 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3782 &d->argb_config, 1, &n) || n != 1) {
3783 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003784 return -1;
3785 }
3786
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003787 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003788 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003789 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003790 fprintf(stderr, "failed to create context\n");
3791 return -1;
3792 }
3793
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003794 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003795 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003796 return -1;
3797 }
3798
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003799#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003800 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3801 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3802 fprintf(stderr, "failed to get cairo egl argb device\n");
3803 return -1;
3804 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003805#endif
3806
3807 return 0;
3808}
3809
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003810static void
3811fini_egl(struct display *display)
3812{
3813#ifdef HAVE_CAIRO_EGL
3814 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003815#endif
3816
3817 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3818 EGL_NO_CONTEXT);
3819
3820 eglTerminate(display->dpy);
3821 eglReleaseThread();
3822}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003823#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003824
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003825static void
3826handle_display_data(struct task *task, uint32_t events)
3827{
3828 struct display *display =
3829 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003830 struct epoll_event ep;
3831 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003832
3833 display->display_fd_events = events;
3834
3835 if (events & EPOLLERR || events & EPOLLHUP) {
3836 display_exit(display);
3837 return;
3838 }
3839
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003840 if (events & EPOLLIN)
3841 wl_display_dispatch(display->display);
3842
3843 if (events & EPOLLOUT) {
3844 ret = wl_display_flush(display->display);
3845 if (ret == 0) {
3846 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3847 ep.data.ptr = &display->display_task;
3848 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3849 display->display_fd, &ep);
3850 } else if (ret == -1 && errno != EAGAIN) {
3851 display_exit(display);
3852 return;
3853 }
3854 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003855}
3856
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003857struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003858display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003859{
3860 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003861
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003862 d = malloc(sizeof *d);
3863 if (d == NULL)
3864 return NULL;
3865
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003866 memset(d, 0, sizeof *d);
3867
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003868 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003869 if (d->display == NULL) {
3870 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003871 return NULL;
3872 }
3873
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003874 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003875 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003876 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003877 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3878 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003879
3880 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003881 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003882 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003883 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003884
Daniel Stone97f68542012-05-30 16:32:01 +01003885 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003886 if (d->xkb_context == NULL) {
3887 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003888 return NULL;
3889 }
3890
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003891 d->workspace = 0;
3892 d->workspace_count = 1;
3893
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003894 d->registry = wl_display_get_registry(d->display);
3895 wl_registry_add_listener(d->registry, &registry_listener, d);
3896 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003897#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003898 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003899 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003900#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003901
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003902 d->image_target_texture_2d =
3903 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3904 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3905 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3906
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003907 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003908
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003909 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003910
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003911 wl_list_init(&d->window_list);
3912
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003913 return d;
3914}
3915
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003916static void
3917display_destroy_outputs(struct display *display)
3918{
3919 struct output *tmp;
3920 struct output *output;
3921
3922 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3923 output_destroy(output);
3924}
3925
Pekka Paalanene1207c72011-12-16 12:02:09 +02003926static void
3927display_destroy_inputs(struct display *display)
3928{
3929 struct input *tmp;
3930 struct input *input;
3931
3932 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3933 input_destroy(input);
3934}
3935
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003936void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003937display_destroy(struct display *display)
3938{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003939 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003940 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3941 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003942
3943 if (!wl_list_empty(&display->deferred_list))
3944 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3945
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003946 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003947 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003948
Daniel Stone97f68542012-05-30 16:32:01 +01003949 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003950
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003951 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003952 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003953
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003954#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003955 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003956#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003957
Pekka Paalanenc2052982011-12-16 11:41:32 +02003958 if (display->shell)
3959 wl_shell_destroy(display->shell);
3960
3961 if (display->shm)
3962 wl_shm_destroy(display->shm);
3963
3964 if (display->data_device_manager)
3965 wl_data_device_manager_destroy(display->data_device_manager);
3966
3967 wl_compositor_destroy(display->compositor);
3968
3969 close(display->epoll_fd);
3970
U. Artie Eoff44874d92012-10-02 21:12:35 -07003971 if (!(display->display_fd_events & EPOLLERR) &&
3972 !(display->display_fd_events & EPOLLHUP))
3973 wl_display_flush(display->display);
3974
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003975 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003976 free(display);
3977}
3978
3979void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003980display_set_user_data(struct display *display, void *data)
3981{
3982 display->user_data = data;
3983}
3984
3985void *
3986display_get_user_data(struct display *display)
3987{
3988 return display->user_data;
3989}
3990
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003991struct wl_display *
3992display_get_display(struct display *display)
3993{
3994 return display->display;
3995}
3996
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003997struct output *
3998display_get_output(struct display *display)
3999{
4000 return container_of(display->output_list.next, struct output, link);
4001}
4002
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004003struct wl_compositor *
4004display_get_compositor(struct display *display)
4005{
4006 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004007}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004008
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004009uint32_t
4010display_get_serial(struct display *display)
4011{
4012 return display->serial;
4013}
4014
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004015EGLDisplay
4016display_get_egl_display(struct display *d)
4017{
4018 return d->dpy;
4019}
4020
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004021struct wl_data_source *
4022display_create_data_source(struct display *display)
4023{
4024 return wl_data_device_manager_create_data_source(display->data_device_manager);
4025}
4026
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004027EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004028display_get_argb_egl_config(struct display *d)
4029{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004030 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004031}
4032
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004033struct wl_shell *
4034display_get_shell(struct display *display)
4035{
4036 return display->shell;
4037}
4038
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004039int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004040display_acquire_window_surface(struct display *display,
4041 struct window *window,
4042 EGLContext ctx)
4043{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004044#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004045 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004046 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004047
4048 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004049 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004050 device = cairo_surface_get_device(window->cairo_surface);
4051 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004052 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004053
Benjamin Franzke0c991632011-09-27 21:57:31 +02004054 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004055 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004056 ctx = display->argb_ctx;
4057 else
4058 assert(0);
4059 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004060
4061 data = cairo_surface_get_user_data(window->cairo_surface,
4062 &surface_data_key);
4063
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004064 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004065 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004066 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4067 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004068
4069 return 0;
4070#else
4071 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004072#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004073}
4074
4075void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004076display_release_window_surface(struct display *display,
4077 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004078{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004079#ifdef HAVE_CAIRO_EGL
4080 cairo_device_t *device;
4081
4082 device = cairo_surface_get_device(window->cairo_surface);
4083 if (!device)
4084 return;
4085
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004086 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004087 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004088 cairo_device_release(device);
4089#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004090}
4091
4092void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004093display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004094{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004095 wl_list_insert(&display->deferred_list, &task->link);
4096}
4097
4098void
4099display_watch_fd(struct display *display,
4100 int fd, uint32_t events, struct task *task)
4101{
4102 struct epoll_event ep;
4103
4104 ep.events = events;
4105 ep.data.ptr = task;
4106 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4107}
4108
4109void
4110display_run(struct display *display)
4111{
4112 struct task *task;
4113 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004114 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004115
Pekka Paalanen826d7952011-12-15 10:14:07 +02004116 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004117 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004118 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004119 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004120 struct task, link);
4121 wl_list_remove(&task->link);
4122 task->run(task, 0);
4123 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004124
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004125 wl_display_dispatch_pending(display->display);
4126
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004127 if (!display->running)
4128 break;
4129
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004130 ret = wl_display_flush(display->display);
4131 if (ret < 0 && errno == EAGAIN) {
4132 ep[0].events =
4133 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4134 ep[0].data.ptr = &display->display_task;
4135
4136 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4137 display->display_fd, &ep[0]);
4138 } else if (ret < 0) {
4139 break;
4140 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004141
4142 count = epoll_wait(display->epoll_fd,
4143 ep, ARRAY_LENGTH(ep), -1);
4144 for (i = 0; i < count; i++) {
4145 task = ep[i].data.ptr;
4146 task->run(task, ep[i].events);
4147 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004148 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004149}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004150
4151void
4152display_exit(struct display *display)
4153{
4154 display->running = 0;
4155}