blob: 7b5e3510418ebd8b67a079da243e05d046de434e [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
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040069#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060070#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020071#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030072#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050073
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050075
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070076struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030077
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040078struct global {
79 uint32_t name;
80 char *interface;
81 uint32_t version;
82 struct wl_list link;
83};
84
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050085struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040087 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050088 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040089 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040090 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040091 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060092 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020093 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128};
129
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400131 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400132 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400133 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500134 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500136 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137 TYPE_CUSTOM
138};
Rob Bradford7507b572012-05-15 17:55:34 +0100139
140struct window_output {
141 struct output *output;
142 struct wl_list link;
143};
144
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500145struct window {
146 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500147 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100148 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500149 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500151 struct wl_region *input_region;
152 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500153 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500154 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400155 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500156 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500157 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400158 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400159 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400160 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400161 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400162 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400163 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400164 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400165 int focus_count;
166
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400167 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500168
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400169 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500170
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700171 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400172
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500173 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500174 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400175 window_data_handler_t data_handler;
176 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500177 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400178 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300180 struct wl_callback *frame_cb;
181
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200182 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500183 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500185 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400186 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500187};
188
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500189struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500190 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300191 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500192 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400193 struct wl_list link;
194 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195 widget_resize_handler_t resize_handler;
196 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500197 widget_enter_handler_t enter_handler;
198 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500199 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500200 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200201 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400202 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500203 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300204 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400205};
206
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207struct input {
208 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100209 struct wl_seat *seat;
210 struct wl_pointer *pointer;
211 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212 struct window *pointer_focus;
213 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300214 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300215 uint32_t cursor_anim_start;
216 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300217 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400218 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400219 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400220 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400221 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400222 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400223
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500224 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500225 struct widget *grab;
226 uint32_t grab_button;
227
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400228 struct wl_data_device *data_device;
229 struct data_offer *drag_offer;
230 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100231
232 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100233 struct xkb_keymap *keymap;
234 struct xkb_state *state;
235 xkb_mod_mask_t control_mask;
236 xkb_mod_mask_t alt_mask;
237 xkb_mod_mask_t shift_mask;
238 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400239
240 struct task repeat_task;
241 int repeat_timer_fd;
242 uint32_t repeat_sym;
243 uint32_t repeat_key;
244 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400245};
246
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500247struct output {
248 struct display *display;
249 struct wl_output *output;
250 struct rectangle allocation;
251 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600252 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200253
254 display_output_handler_t destroy_handler;
255 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500256};
257
Martin Minarik1998b152012-05-10 02:04:35 +0200258enum frame_button_action {
259 FRAME_BUTTON_NULL = 0,
260 FRAME_BUTTON_ICON = 1,
261 FRAME_BUTTON_CLOSE = 2,
262 FRAME_BUTTON_MINIMIZE = 3,
263 FRAME_BUTTON_MAXIMIZE = 4,
264};
265
266enum frame_button_pointer {
267 FRAME_BUTTON_DEFAULT = 0,
268 FRAME_BUTTON_OVER = 1,
269 FRAME_BUTTON_ACTIVE = 2,
270};
271
272enum frame_button_align {
273 FRAME_BUTTON_RIGHT = 0,
274 FRAME_BUTTON_LEFT = 1,
275};
276
277enum frame_button_decoration {
278 FRAME_BUTTON_NONE = 0,
279 FRAME_BUTTON_FANCY = 1,
280};
281
282struct frame_button {
283 struct widget *widget;
284 struct frame *frame;
285 cairo_surface_t *icon;
286 enum frame_button_action type;
287 enum frame_button_pointer state;
288 struct wl_list link; /* buttons_list */
289 enum frame_button_align align;
290 enum frame_button_decoration decoration;
291};
292
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500293struct frame {
294 struct widget *widget;
295 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200296 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500297};
298
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500299struct menu {
300 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500301 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500302 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500303 const char **entries;
304 uint32_t time;
305 int current;
306 int count;
307 menu_func_t func;
308};
309
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300310struct tooltip {
311 struct widget *parent;
312 struct window *window;
313 struct widget *widget;
314 char *entry;
315 struct task tooltip_task;
316 int tooltip_fd;
317 float x, y;
318};
319
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700320struct shm_pool {
321 struct wl_shm_pool *pool;
322 size_t size;
323 size_t used;
324 void *data;
325};
326
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400327enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300328 CURSOR_DEFAULT = 100,
329 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400330};
331
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500332enum window_location {
333 WINDOW_INTERIOR = 0,
334 WINDOW_RESIZING_TOP = 1,
335 WINDOW_RESIZING_BOTTOM = 2,
336 WINDOW_RESIZING_LEFT = 4,
337 WINDOW_RESIZING_TOP_LEFT = 5,
338 WINDOW_RESIZING_BOTTOM_LEFT = 6,
339 WINDOW_RESIZING_RIGHT = 8,
340 WINDOW_RESIZING_TOP_RIGHT = 9,
341 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
342 WINDOW_RESIZING_MASK = 15,
343 WINDOW_EXTERIOR = 16,
344 WINDOW_TITLEBAR = 17,
345 WINDOW_CLIENT_AREA = 18,
346};
347
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400348static const cairo_user_data_key_t surface_data_key;
349struct surface_data {
350 struct wl_buffer *buffer;
351};
352
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500353#define MULT(_d,c,a,t) \
354 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
355
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500356#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100358struct egl_window_surface_data {
359 struct display *display;
360 struct wl_surface *surface;
361 struct wl_egl_window *window;
362 EGLSurface surf;
363};
364
365static void
366egl_window_surface_data_destroy(void *p)
367{
368 struct egl_window_surface_data *data = p;
369 struct display *d = data->display;
370
371 eglDestroySurface(d->dpy, data->surf);
372 wl_egl_window_destroy(data->window);
373 data->surface = NULL;
374
375 free(p);
376}
377
378static cairo_surface_t *
379display_create_egl_window_surface(struct display *display,
380 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100382 struct rectangle *rectangle)
383{
384 cairo_surface_t *cairo_surface;
385 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200387 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100388
389 data = malloc(sizeof *data);
390 if (data == NULL)
391 return NULL;
392
393 data->display = display;
394 data->surface = surface;
395
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500396 config = display->argb_config;
397 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400398
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400399 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100400 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400401 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100402
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400403 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500404 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100405
Benjamin Franzke0c991632011-09-27 21:57:31 +0200406 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100407 data->surf,
408 rectangle->width,
409 rectangle->height);
410
411 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
412 data, egl_window_surface_data_destroy);
413
414 return cairo_surface;
415}
416
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417#endif
418
419struct wl_buffer *
420display_get_buffer_for_surface(struct display *display,
421 cairo_surface_t *surface)
422{
423 struct surface_data *data;
424
425 data = cairo_surface_get_user_data (surface, &surface_data_key);
426
427 return data->buffer;
428}
429
430struct shm_surface_data {
431 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700432 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433};
434
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500435static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700436shm_pool_destroy(struct shm_pool *pool);
437
438static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439shm_surface_data_destroy(void *p)
440{
441 struct shm_surface_data *data = p;
442
443 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700444 if (data->pool)
445 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300446
447 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400448}
449
Kristian Høgsberg16626282012-04-03 11:21:27 -0400450static struct wl_shm_pool *
451make_shm_pool(struct display *display, int size, void **data)
452{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400453 struct wl_shm_pool *pool;
454 int fd;
455
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300456 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400457 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300458 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
459 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400460 return NULL;
461 }
462
463 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400464 if (*data == MAP_FAILED) {
465 fprintf(stderr, "mmap failed: %m\n");
466 close(fd);
467 return NULL;
468 }
469
470 pool = wl_shm_create_pool(display->shm, fd, size);
471
472 close(fd);
473
474 return pool;
475}
476
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700477static struct shm_pool *
478shm_pool_create(struct display *display, size_t size)
479{
480 struct shm_pool *pool = malloc(sizeof *pool);
481
482 if (!pool)
483 return NULL;
484
485 pool->pool = make_shm_pool(display, size, &pool->data);
486 if (!pool->pool) {
487 free(pool);
488 return NULL;
489 }
490
491 pool->size = size;
492 pool->used = 0;
493
494 return pool;
495}
496
497static void *
498shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
499{
500 if (pool->used + size > pool->size)
501 return NULL;
502
503 *offset = pool->used;
504 pool->used += size;
505
506 return (char *) pool->data + *offset;
507}
508
509/* destroy the pool. this does not unmap the memory though */
510static void
511shm_pool_destroy(struct shm_pool *pool)
512{
513 munmap(pool->data, pool->size);
514 wl_shm_pool_destroy(pool->pool);
515 free(pool);
516}
517
518/* Start allocating from the beginning of the pool again */
519static void
520shm_pool_reset(struct shm_pool *pool)
521{
522 pool->used = 0;
523}
524
525static int
526data_length_for_shm_surface(struct rectangle *rect)
527{
528 int stride;
529
530 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
531 rect->width);
532 return stride * rect->height;
533}
534
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500535static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700536display_create_shm_surface_from_pool(struct display *display,
537 struct rectangle *rectangle,
538 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539{
540 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400541 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700543 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400544 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400545
546 data = malloc(sizeof *data);
547 if (data == NULL)
548 return NULL;
549
550 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
551 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700552 length = stride * rectangle->height;
553 data->pool = NULL;
554 map = shm_pool_allocate(pool, length, &offset);
555
556 if (!map) {
557 free(data);
558 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400559 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400561 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 CAIRO_FORMAT_ARGB32,
563 rectangle->width,
564 rectangle->height,
565 stride);
566
567 cairo_surface_set_user_data (surface, &surface_data_key,
568 data, shm_surface_data_destroy);
569
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400570 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500571 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400572 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500573 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400574
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700575 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400576 rectangle->width,
577 rectangle->height,
578 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700580 return surface;
581}
582
583static cairo_surface_t *
584display_create_shm_surface(struct display *display,
585 struct rectangle *rectangle, uint32_t flags,
586 struct window *window)
587{
588 struct shm_surface_data *data;
589 struct shm_pool *pool;
590 cairo_surface_t *surface;
591
592 if (window && window->pool) {
593 shm_pool_reset(window->pool);
594 surface = display_create_shm_surface_from_pool(display,
595 rectangle,
596 flags,
597 window->pool);
598 if (surface)
599 return surface;
600 }
601
602 pool = shm_pool_create(display,
603 data_length_for_shm_surface(rectangle));
604 if (!pool)
605 return NULL;
606
607 surface =
608 display_create_shm_surface_from_pool(display, rectangle,
609 flags, pool);
610
611 if (!surface) {
612 shm_pool_destroy(pool);
613 return NULL;
614 }
615
616 /* make sure we destroy the pool when the surface is destroyed */
617 data = cairo_surface_get_user_data(surface, &surface_data_key);
618 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619
620 return surface;
621}
622
nobled7b87cb02011-02-01 18:51:47 +0000623static int
624check_size(struct rectangle *rect)
625{
626 if (rect->width && rect->height)
627 return 0;
628
629 fprintf(stderr, "tried to create surface of "
630 "width: %d, height: %d\n", rect->width, rect->height);
631 return -1;
632}
633
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634cairo_surface_t *
635display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400637 struct rectangle *rectangle,
638 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639{
nobled7b87cb02011-02-01 18:51:47 +0000640 if (check_size(rectangle) < 0)
641 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500642#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300643 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400644 return display_create_egl_window_surface(display,
645 surface,
646 flags,
647 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400649 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650}
651
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200652/*
653 * The following correspondences between file names and cursors was copied
654 * from: https://bugs.kde.org/attachment.cgi?id=67313
655 */
656
657static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300658 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200659 "sw-resize"
660};
661
662static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200664 "se-resize"
665};
666
667static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200669 "s-resize"
670};
671
672static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300673 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200674 "closedhand",
675 "208530c400c041818281048008011002"
676};
677
678static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300679 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200680 "default",
681 "top_left_arrow",
682 "left-arrow"
683};
684
685static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300686 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200687 "w-resize"
688};
689
690static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300691 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200692 "e-resize"
693};
694
695static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300696 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200697 "nw-resize"
698};
699
700static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300701 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200702 "ne-resize"
703};
704
705static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300706 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200707 "n-resize"
708};
709
710static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300711 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200712 "ibeam",
713 "text"
714};
715
716static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300717 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200718 "pointer",
719 "pointing_hand",
720 "e29285e634086352946a0e7090d73106"
721};
722
723static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400724 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200725 "wait",
726 "0426c94ea35c87780ff01dc239897213"
727};
728
729struct cursor_alternatives {
730 const char **names;
731 size_t count;
732};
733
734static const struct cursor_alternatives cursors[] = {
735 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
736 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
737 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
738 {grabbings, ARRAY_LENGTH(grabbings)},
739 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
740 {left_sides, ARRAY_LENGTH(left_sides)},
741 {right_sides, ARRAY_LENGTH(right_sides)},
742 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
743 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
744 {top_sides, ARRAY_LENGTH(top_sides)},
745 {xterms, ARRAY_LENGTH(xterms)},
746 {hand1s, ARRAY_LENGTH(hand1s)},
747 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300748};
749
750static void
751create_cursors(struct display *display)
752{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100753 char *config_file;
754 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200755 unsigned int i, j;
756 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100757 struct config_key shell_keys[] = {
758 { "cursor-theme", CONFIG_KEY_STRING, &theme },
759 };
760 struct config_section cs[] = {
761 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
762 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300763
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100764 config_file = config_file_path("weston.ini");
765 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
766 free(config_file);
767
768 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300769 display->cursors =
770 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
771
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300772 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200773 cursor = NULL;
774 for (j = 0; !cursor && j < cursors[i].count; ++j)
775 cursor = wl_cursor_theme_get_cursor(
776 display->cursor_theme, cursors[i].names[j]);
777
778 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300779 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200780 cursors[i].names[0]);
781
782 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300783 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300784}
785
786static void
787destroy_cursors(struct display *display)
788{
789 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800790 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300791}
792
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300793struct wl_cursor_image *
794display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400795{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200796 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400797
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300798 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400799}
800
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400801static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200802window_get_resize_dx_dy(struct window *window, int *x, int *y)
803{
804 if (window->resize_edges & WINDOW_RESIZING_LEFT)
805 *x = window->server_allocation.width - window->allocation.width;
806 else
807 *x = 0;
808
809 if (window->resize_edges & WINDOW_RESIZING_TOP)
810 *y = window->server_allocation.height -
811 window->allocation.height;
812 else
813 *y = 0;
814
815 window->resize_edges = 0;
816}
817
818static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500819window_attach_surface(struct window *window)
820{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400821 struct display *display = window->display;
822 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500826 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400828 if (window->type == TYPE_NONE) {
829 window->type = TYPE_TOPLEVEL;
830 if (display->shell)
831 wl_shell_surface_set_toplevel(window->shell_surface);
832 }
833
Pekka Paalanen512dde82012-10-10 12:49:27 +0300834 if (window->opaque_region) {
835 wl_surface_set_opaque_region(window->surface,
836 window->opaque_region);
837 wl_region_destroy(window->opaque_region);
838 window->opaque_region = NULL;
839 }
840
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300841 if (window->input_region) {
842 wl_surface_set_input_region(window->surface,
843 window->input_region);
844 wl_region_destroy(window->input_region);
845 window->input_region = NULL;
846 }
847
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000849#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
851 data = cairo_surface_get_user_data(window->cairo_surface,
852 &surface_data_key);
853
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 cairo_gl_surface_swapbuffers(window->cairo_surface);
855 wl_egl_window_get_attached_size(data->window,
856 &window->server_allocation.width,
857 &window->server_allocation.height);
858 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000859#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 buffer =
862 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400863 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200865 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400867 wl_surface_damage(window->surface, 0, 0,
868 window->allocation.width,
869 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300870 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100871 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400872 cairo_surface_destroy(window->cairo_surface);
873 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000875 default:
876 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500878}
879
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400880int
881window_has_focus(struct window *window)
882{
883 return window->focus_count > 0;
884}
885
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500886void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400887window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500888{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400889 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100890 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500891}
892
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893void
894window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500896 cairo_surface_reference(surface);
897
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898 if (window->cairo_surface != NULL)
899 cairo_surface_destroy(window->cairo_surface);
900
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500901 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400902}
903
Benjamin Franzke22d54812011-07-16 19:50:32 +0000904#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905static void
906window_resize_cairo_window_surface(struct window *window)
907{
908 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200909 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910
911 data = cairo_surface_get_user_data(window->cairo_surface,
912 &surface_data_key);
913
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200914 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 wl_egl_window_resize(data->window,
916 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200917 window->allocation.height,
918 x,y);
919
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920 cairo_gl_surface_set_size(window->cairo_surface,
921 window->allocation.width,
922 window->allocation.height);
923}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000924#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100925
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400926struct display *
927window_get_display(struct window *window)
928{
929 return window->display;
930}
931
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400932void
933window_create_surface(struct window *window)
934{
935 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400936 uint32_t flags = 0;
937
938 if (!window->transparent)
939 flags = SURFACE_OPAQUE;
940
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500942#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100943 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
944 if (window->cairo_surface) {
945 window_resize_cairo_window_surface(window);
946 return;
947 }
948 surface = display_create_surface(window->display,
949 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400950 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100951 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952#endif
953 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400954 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400955 &window->allocation,
956 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800958 default:
959 surface = NULL;
960 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400962
963 window_set_surface(window, surface);
964 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400965}
966
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200967static void frame_destroy(struct frame *frame);
968
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400969void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500970window_destroy(struct window *window)
971{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200972 struct display *display = window->display;
973 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100974 struct window_output *window_output;
975 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200976
977 if (window->redraw_scheduled)
978 wl_list_remove(&window->redraw_task.link);
979
980 wl_list_for_each(input, &display->input_list, link) {
981 if (input->pointer_focus == window)
982 input->pointer_focus = NULL;
983 if (input->keyboard_focus == window)
984 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500985 if (input->focus_widget &&
986 input->focus_widget->window == window)
987 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200988 }
989
Rob Bradford7507b572012-05-15 17:55:34 +0100990 wl_list_for_each_safe(window_output, window_output_tmp,
991 &window->window_output_list, link) {
992 free (window_output);
993 }
994
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500995 if (window->input_region)
996 wl_region_destroy(window->input_region);
997 if (window->opaque_region)
998 wl_region_destroy(window->opaque_region);
999
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001000 if (window->frame)
1001 frame_destroy(window->frame);
1002
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001003 if (window->shell_surface)
1004 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001005 wl_surface_destroy(window->surface);
1006 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001007
1008 if (window->cairo_surface != NULL)
1009 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001010
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001011 if (window->frame_cb)
1012 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001013 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001014 free(window);
1015}
1016
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001017static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001018widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001020 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001022 wl_list_for_each(child, &widget->child_list, link) {
1023 target = widget_find_widget(child, x, y);
1024 if (target)
1025 return target;
1026 }
1027
1028 if (widget->allocation.x <= x &&
1029 x < widget->allocation.x + widget->allocation.width &&
1030 widget->allocation.y <= y &&
1031 y < widget->allocation.y + widget->allocation.height) {
1032 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001033 }
1034
1035 return NULL;
1036}
1037
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001038static struct widget *
1039widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001043 widget = malloc(sizeof *widget);
1044 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001045 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001046 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001047 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001048 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001049 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001050 widget->tooltip = NULL;
1051 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001052
1053 return widget;
1054}
1055
1056struct widget *
1057window_add_widget(struct window *window, void *data)
1058{
1059 window->widget = widget_create(window, data);
1060 wl_list_init(&window->widget->link);
1061
1062 return window->widget;
1063}
1064
1065struct widget *
1066widget_add_widget(struct widget *parent, void *data)
1067{
1068 struct widget *widget;
1069
1070 widget = widget_create(parent->window, data);
1071 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074}
1075
1076void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001077widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001078{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001079 struct display *display = widget->window->display;
1080 struct input *input;
1081
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001082 if (widget->tooltip) {
1083 free(widget->tooltip);
1084 widget->tooltip = NULL;
1085 }
1086
Pekka Paalanene156fb62012-01-19 13:51:38 +02001087 wl_list_for_each(input, &display->input_list, link) {
1088 if (input->focus_widget == widget)
1089 input->focus_widget = NULL;
1090 }
1091
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001092 wl_list_remove(&widget->link);
1093 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001094}
1095
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001096void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001097widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100}
1101
1102void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001103widget_set_size(struct widget *widget, int32_t width, int32_t height)
1104{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001105 widget->allocation.width = width;
1106 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001107}
1108
1109void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001110widget_set_allocation(struct widget *widget,
1111 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001113 widget->allocation.x = x;
1114 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001115 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001116}
1117
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001118void
1119widget_set_transparent(struct widget *widget, int transparent)
1120{
1121 widget->opaque = !transparent;
1122}
1123
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001124void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001125widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001126{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001127 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001128}
1129
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001130void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001131widget_set_resize_handler(struct widget *widget,
1132 widget_resize_handler_t handler)
1133{
1134 widget->resize_handler = handler;
1135}
1136
1137void
1138widget_set_redraw_handler(struct widget *widget,
1139 widget_redraw_handler_t handler)
1140{
1141 widget->redraw_handler = handler;
1142}
1143
1144void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001145widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001146{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001147 widget->enter_handler = handler;
1148}
1149
1150void
1151widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1152{
1153 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001154}
1155
1156void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001157widget_set_motion_handler(struct widget *widget,
1158 widget_motion_handler_t handler)
1159{
1160 widget->motion_handler = handler;
1161}
1162
1163void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001164widget_set_button_handler(struct widget *widget,
1165 widget_button_handler_t handler)
1166{
1167 widget->button_handler = handler;
1168}
1169
1170void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001171widget_set_axis_handler(struct widget *widget,
1172 widget_axis_handler_t handler)
1173{
1174 widget->axis_handler = handler;
1175}
1176
1177void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001178widget_schedule_redraw(struct widget *widget)
1179{
1180 window_schedule_redraw(widget->window);
1181}
1182
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001183cairo_surface_t *
1184window_get_surface(struct window *window)
1185{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001186 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001187}
1188
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001189struct wl_surface *
1190window_get_wl_surface(struct window *window)
1191{
1192 return window->surface;
1193}
1194
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001195struct wl_shell_surface *
1196window_get_wl_shell_surface(struct window *window)
1197{
1198 return window->shell_surface;
1199}
1200
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001201static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001202tooltip_redraw_handler(struct widget *widget, void *data)
1203{
1204 cairo_t *cr;
1205 const int32_t r = 3;
1206 struct tooltip *tooltip = data;
1207 int32_t width, height;
1208 struct window *window = widget->window;
1209
1210 cr = cairo_create(window->cairo_surface);
1211 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1212 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1213 cairo_paint(cr);
1214
1215 width = window->allocation.width;
1216 height = window->allocation.height;
1217 rounded_rect(cr, 0, 0, width, height, r);
1218
1219 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1220 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1221 cairo_fill(cr);
1222
1223 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1224 cairo_move_to(cr, 10, 16);
1225 cairo_show_text(cr, tooltip->entry);
1226 cairo_destroy(cr);
1227}
1228
1229static cairo_text_extents_t
1230get_text_extents(struct tooltip *tooltip)
1231{
1232 struct window *window;
1233 cairo_t *cr;
1234 cairo_text_extents_t extents;
1235
1236 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1237 * created yet */
1238 window = tooltip->widget->window->parent;
1239 cr = cairo_create(window->cairo_surface);
1240 cairo_text_extents(cr, tooltip->entry, &extents);
1241 cairo_destroy(cr);
1242
1243 return extents;
1244}
1245
1246static int
1247window_create_tooltip(struct tooltip *tooltip)
1248{
1249 struct widget *parent = tooltip->parent;
1250 struct display *display = parent->window->display;
1251 struct window *window;
1252 const int offset_y = 27;
1253 const int margin = 3;
1254 cairo_text_extents_t extents;
1255
1256 if (tooltip->widget)
1257 return 0;
1258
1259 window = window_create_transient(display, parent->window, tooltip->x,
1260 tooltip->y + offset_y,
1261 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1262 if (!window)
1263 return -1;
1264
1265 tooltip->window = window;
1266 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1267
1268 extents = get_text_extents(tooltip);
1269 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1270 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1271
1272 return 0;
1273}
1274
1275void
1276widget_destroy_tooltip(struct widget *parent)
1277{
1278 struct tooltip *tooltip = parent->tooltip;
1279
1280 parent->tooltip_count = 0;
1281 if (!tooltip)
1282 return;
1283
1284 if (tooltip->widget) {
1285 widget_destroy(tooltip->widget);
1286 window_destroy(tooltip->window);
1287 tooltip->widget = NULL;
1288 tooltip->window = NULL;
1289 }
1290
1291 close(tooltip->tooltip_fd);
1292 free(tooltip->entry);
1293 free(tooltip);
1294 parent->tooltip = NULL;
1295}
1296
1297static void
1298tooltip_func(struct task *task, uint32_t events)
1299{
1300 struct tooltip *tooltip =
1301 container_of(task, struct tooltip, tooltip_task);
1302 uint64_t exp;
1303
Martin Olsson8df662a2012-07-08 03:03:47 +02001304 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1305 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001306 window_create_tooltip(tooltip);
1307}
1308
1309#define TOOLTIP_TIMEOUT 500
1310static int
1311tooltip_timer_reset(struct tooltip *tooltip)
1312{
1313 struct itimerspec its;
1314
1315 its.it_interval.tv_sec = 0;
1316 its.it_interval.tv_nsec = 0;
1317 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1318 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1319 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1320 fprintf(stderr, "could not set timerfd\n: %m");
1321 return -1;
1322 }
1323
1324 return 0;
1325}
1326
1327int
1328widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1329{
1330 struct tooltip *tooltip = parent->tooltip;
1331
1332 parent->tooltip_count++;
1333 if (tooltip) {
1334 tooltip->x = x;
1335 tooltip->y = y;
1336 tooltip_timer_reset(tooltip);
1337 return 0;
1338 }
1339
1340 /* the handler might be triggered too fast via input device motion, so
1341 * we need this check here to make sure tooltip is fully initialized */
1342 if (parent->tooltip_count > 1)
1343 return 0;
1344
1345 tooltip = malloc(sizeof *tooltip);
1346 if (!tooltip)
1347 return -1;
1348
1349 parent->tooltip = tooltip;
1350 tooltip->parent = parent;
1351 tooltip->widget = NULL;
1352 tooltip->window = NULL;
1353 tooltip->x = x;
1354 tooltip->y = y;
1355 tooltip->entry = strdup(entry);
1356 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1357 if (tooltip->tooltip_fd < 0) {
1358 fprintf(stderr, "could not create timerfd\n: %m");
1359 return -1;
1360 }
1361
1362 tooltip->tooltip_task.run = tooltip_func;
1363 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1364 EPOLLIN, &tooltip->tooltip_task);
1365 tooltip_timer_reset(tooltip);
1366
1367 return 0;
1368}
1369
1370static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001371workspace_manager_state(void *data,
1372 struct workspace_manager *workspace_manager,
1373 uint32_t current,
1374 uint32_t count)
1375{
1376 struct display *display = data;
1377
1378 display->workspace = current;
1379 display->workspace_count = count;
1380}
1381
1382static const struct workspace_manager_listener workspace_manager_listener = {
1383 workspace_manager_state
1384};
1385
1386static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387frame_resize_handler(struct widget *widget,
1388 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001389{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001390 struct frame *frame = data;
1391 struct widget *child = frame->child;
1392 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001393 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001394 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001395 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001396 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001397 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001398 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001399
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001400 switch (widget->window->type) {
1401 case TYPE_FULLSCREEN:
1402 decoration_width = 0;
1403 decoration_height = 0;
1404
1405 allocation.x = 0;
1406 allocation.y = 0;
1407 allocation.width = width;
1408 allocation.height = height;
1409 opaque_margin = 0;
1410
1411 wl_list_for_each(button, &frame->buttons_list, link)
1412 button->widget->opaque = 1;
1413 break;
1414 case TYPE_MAXIMIZED:
1415 decoration_width = t->width * 2;
1416 decoration_height = t->width + t->titlebar_height;
1417
1418 allocation.x = t->width;
1419 allocation.y = t->titlebar_height;
1420 allocation.width = width - decoration_width;
1421 allocation.height = height - decoration_height;
1422
1423 opaque_margin = 0;
1424
1425 wl_list_for_each(button, &frame->buttons_list, link)
1426 button->widget->opaque = 0;
1427 break;
1428 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001429 decoration_width = (t->width + t->margin) * 2;
1430 decoration_height = t->width +
1431 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001432
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001433 allocation.x = t->width + t->margin;
1434 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001435 allocation.width = width - decoration_width;
1436 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001437
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001438 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001439
1440 wl_list_for_each(button, &frame->buttons_list, link)
1441 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001442 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001443 }
1444
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001445 widget_set_allocation(child, allocation.x, allocation.y,
1446 allocation.width, allocation.height);
1447
1448 if (child->resize_handler)
1449 child->resize_handler(child,
1450 allocation.width,
1451 allocation.height,
1452 child->user_data);
1453
Scott Moreauf7e498c2012-05-14 11:39:29 -06001454 width = child->allocation.width + decoration_width;
1455 height = child->allocation.height + decoration_height;
1456
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001457 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1458
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001459 widget->window->input_region =
1460 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001461 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001462 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001463 shadow_margin, shadow_margin,
1464 width - 2 * shadow_margin,
1465 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001466 } else {
1467 wl_region_add(widget->window->input_region,
1468 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001469 }
1470
Scott Moreauf7e498c2012-05-14 11:39:29 -06001471 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001472
1473 if (child->opaque) {
1474 widget->window->opaque_region =
1475 wl_compositor_create_region(display->compositor);
1476 wl_region_add(widget->window->opaque_region,
1477 opaque_margin, opaque_margin,
1478 widget->allocation.width - 2 * opaque_margin,
1479 widget->allocation.height - 2 * opaque_margin);
1480 }
Martin Minarik1998b152012-05-10 02:04:35 +02001481
1482 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001483 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1484 x_l = t->width + shadow_margin;
1485 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001486 wl_list_for_each(button, &frame->buttons_list, link) {
1487 const int button_padding = 4;
1488 w = cairo_image_surface_get_width(button->icon);
1489 h = cairo_image_surface_get_height(button->icon);
1490
1491 if (button->decoration == FRAME_BUTTON_FANCY)
1492 w += 10;
1493
1494 if (button->align == FRAME_BUTTON_LEFT) {
1495 widget_set_allocation(button->widget,
1496 x_l, y , w + 1, h + 1);
1497 x_l += w;
1498 x_l += button_padding;
1499 } else {
1500 x_r -= w;
1501 widget_set_allocation(button->widget,
1502 x_r, y , w + 1, h + 1);
1503 x_r -= button_padding;
1504 }
1505 }
1506}
1507
1508static int
1509frame_button_enter_handler(struct widget *widget,
1510 struct input *input, float x, float y, void *data)
1511{
1512 struct frame_button *frame_button = data;
1513
1514 widget_schedule_redraw(frame_button->widget);
1515 frame_button->state = FRAME_BUTTON_OVER;
1516
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001517 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001518}
1519
1520static void
1521frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1522{
1523 struct frame_button *frame_button = data;
1524
1525 widget_schedule_redraw(frame_button->widget);
1526 frame_button->state = FRAME_BUTTON_DEFAULT;
1527}
1528
1529static void
1530frame_button_button_handler(struct widget *widget,
1531 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001532 uint32_t button,
1533 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001534{
1535 struct frame_button *frame_button = data;
1536 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001537 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001538
1539 if (button != BTN_LEFT)
1540 return;
1541
1542 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001543 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001544 frame_button->state = FRAME_BUTTON_ACTIVE;
1545 widget_schedule_redraw(frame_button->widget);
1546
1547 if (frame_button->type == FRAME_BUTTON_ICON)
1548 window_show_frame_menu(window, input, time);
1549 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001550 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001551 frame_button->state = FRAME_BUTTON_DEFAULT;
1552 widget_schedule_redraw(frame_button->widget);
1553 break;
1554 }
1555
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001556 if (!was_pressed)
1557 return;
1558
Martin Minarik1998b152012-05-10 02:04:35 +02001559 switch (frame_button->type) {
1560 case FRAME_BUTTON_CLOSE:
1561 if (window->close_handler)
1562 window->close_handler(window->parent,
1563 window->user_data);
1564 else
1565 display_exit(window->display);
1566 break;
1567 case FRAME_BUTTON_MINIMIZE:
1568 fprintf(stderr,"Minimize stub\n");
1569 break;
1570 case FRAME_BUTTON_MAXIMIZE:
1571 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1572 break;
1573 default:
1574 /* Unknown operation */
1575 break;
1576 }
1577}
1578
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001579static int
1580frame_button_motion_handler(struct widget *widget,
1581 struct input *input, uint32_t time,
1582 float x, float y, void *data)
1583{
1584 struct frame_button *frame_button = data;
1585 enum frame_button_pointer previous_button_state = frame_button->state;
1586
1587 /* only track state for a pressed button */
1588 if (input->grab != widget)
1589 return CURSOR_LEFT_PTR;
1590
1591 if (x > widget->allocation.x &&
1592 x < (widget->allocation.x + widget->allocation.width) &&
1593 y > widget->allocation.y &&
1594 y < (widget->allocation.y + widget->allocation.height)) {
1595 frame_button->state = FRAME_BUTTON_ACTIVE;
1596 } else {
1597 frame_button->state = FRAME_BUTTON_DEFAULT;
1598 }
1599
1600 if (frame_button->state != previous_button_state)
1601 widget_schedule_redraw(frame_button->widget);
1602
1603 return CURSOR_LEFT_PTR;
1604}
1605
Martin Minarik1998b152012-05-10 02:04:35 +02001606static void
1607frame_button_redraw_handler(struct widget *widget, void *data)
1608{
1609 struct frame_button *frame_button = data;
1610 cairo_t *cr;
1611 int width, height, x, y;
1612 struct window *window = widget->window;
1613
1614 x = widget->allocation.x;
1615 y = widget->allocation.y;
1616 width = widget->allocation.width;
1617 height = widget->allocation.height;
1618
1619 if (!width)
1620 return;
1621 if (!height)
1622 return;
1623 if (widget->opaque)
1624 return;
1625
1626 cr = cairo_create(window->cairo_surface);
1627
1628 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1629 cairo_set_line_width(cr, 1);
1630
1631 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1632 cairo_rectangle (cr, x, y, 25, 16);
1633
1634 cairo_stroke_preserve(cr);
1635
1636 switch (frame_button->state) {
1637 case FRAME_BUTTON_DEFAULT:
1638 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1639 break;
1640 case FRAME_BUTTON_OVER:
1641 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1642 break;
1643 case FRAME_BUTTON_ACTIVE:
1644 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1645 break;
1646 }
1647
1648 cairo_fill (cr);
1649
1650 x += 4;
1651 }
1652
1653 cairo_set_source_surface(cr, frame_button->icon, x, y);
1654 cairo_paint(cr);
1655
1656 cairo_destroy(cr);
1657}
1658
1659static struct widget *
1660frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1661 enum frame_button_align align, enum frame_button_decoration style)
1662{
1663 struct frame_button *frame_button;
1664 const char *icon = data;
1665
1666 frame_button = malloc (sizeof *frame_button);
1667 memset(frame_button, 0, sizeof *frame_button);
1668
1669 frame_button->icon = cairo_image_surface_create_from_png(icon);
1670 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1671 frame_button->frame = frame;
1672 frame_button->type = type;
1673 frame_button->align = align;
1674 frame_button->decoration = style;
1675
1676 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1677
1678 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1679 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1680 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1681 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001682 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001683 return frame_button->widget;
1684}
1685
1686static void
1687frame_button_destroy(struct frame_button *frame_button)
1688{
1689 widget_destroy(frame_button->widget);
1690 wl_list_remove(&frame_button->link);
1691 cairo_surface_destroy(frame_button->icon);
1692 free(frame_button);
1693
1694 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695}
1696
1697static void
1698frame_redraw_handler(struct widget *widget, void *data)
1699{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001700 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001702 struct theme *t = window->display->theme;
1703 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001704
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001705 if (window->type == TYPE_FULLSCREEN)
1706 return;
1707
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001708 cr = cairo_create(window->cairo_surface);
1709
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001710 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001711 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001712 if (window->type == TYPE_MAXIMIZED)
1713 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001714 theme_render_frame(t, cr, widget->allocation.width,
1715 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716
1717 cairo_destroy(cr);
1718}
1719
1720static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001721frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001722{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001723 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001724 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001725 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001726
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001727 location = theme_get_location(t, input->sx, input->sy,
1728 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001729 frame->widget->allocation.height,
1730 window->type == TYPE_MAXIMIZED ?
1731 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001732
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001733 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001734 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001735 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001736 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001738 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001739 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001740 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001741 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001742 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001743 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001744 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001745 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001746 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001747 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001748 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001749 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001750 case THEME_LOCATION_EXTERIOR:
1751 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001752 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001753 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001754 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001755}
1756
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001757static void
1758frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001759{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001760 struct display *display;
1761
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001762 switch (index) {
1763 case 0: /* close */
1764 if (window->close_handler)
1765 window->close_handler(window->parent,
1766 window->user_data);
1767 else
1768 display_exit(window->display);
1769 break;
1770 case 1: /* fullscreen */
1771 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001772 if (window->fullscreen_handler)
1773 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001774 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001775 case 2: /* move to workspace above */
1776 display = window->display;
1777 if (display->workspace > 0)
1778 workspace_manager_move_surface(display->workspace_manager,
1779 window->surface,
1780 display->workspace - 1);
1781 break;
1782 case 3: /* move to workspace below */
1783 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001784 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001785 workspace_manager_move_surface(display->workspace_manager,
1786 window->surface,
1787 display->workspace + 1);
1788 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001789 }
1790}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001791
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001792void
1793window_show_frame_menu(struct window *window,
1794 struct input *input, uint32_t time)
1795{
1796 int32_t x, y;
1797
1798 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001799 "Close", "Fullscreen",
1800 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001801 };
1802
1803 input_get_position(input, &x, &y);
1804 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001805 x - 10, y - 10, frame_menu_func, entries,
1806 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001807}
1808
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809static int
1810frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001811 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001812{
1813 return frame_get_pointer_image_for_location(data, input);
1814}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001815
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001816static int
1817frame_motion_handler(struct widget *widget,
1818 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001819 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001820{
1821 return frame_get_pointer_image_for_location(data, input);
1822}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001823
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825frame_button_handler(struct widget *widget,
1826 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001827 uint32_t button, enum wl_pointer_button_state state,
1828 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001829
1830{
1831 struct frame *frame = data;
1832 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001833 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001834 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001835
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001836 location = theme_get_location(display->theme, input->sx, input->sy,
1837 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001838 frame->widget->allocation.height,
1839 window->type == TYPE_MAXIMIZED ?
1840 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001841
Daniel Stone4dbadb12012-05-30 16:31:51 +01001842 if (window->display->shell && button == BTN_LEFT &&
1843 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001844 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001845 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001846 if (!window->shell_surface)
1847 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001849 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001850 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001851 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001852 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001853 case THEME_LOCATION_RESIZING_TOP:
1854 case THEME_LOCATION_RESIZING_BOTTOM:
1855 case THEME_LOCATION_RESIZING_LEFT:
1856 case THEME_LOCATION_RESIZING_RIGHT:
1857 case THEME_LOCATION_RESIZING_TOP_LEFT:
1858 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1859 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1860 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001861 if (!window->shell_surface)
1862 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001863 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001864
1865 if (!display->dpy) {
1866 /* If we're using shm, allocate a big
1867 pool to create buffers out of while
1868 we resize. We should probably base
1869 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001870 window->pool =
1871 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001872 }
1873
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001874 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001875 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001876 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001877 break;
1878 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001879 } else if (button == BTN_RIGHT &&
1880 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001881 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001882 }
1883}
1884
1885struct widget *
1886frame_create(struct window *window, void *data)
1887{
1888 struct frame *frame;
1889
1890 frame = malloc(sizeof *frame);
1891 memset(frame, 0, sizeof *frame);
1892
1893 frame->widget = window_add_widget(window, frame);
1894 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001895
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001896 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1897 widget_set_resize_handler(frame->widget, frame_resize_handler);
1898 widget_set_enter_handler(frame->widget, frame_enter_handler);
1899 widget_set_motion_handler(frame->widget, frame_motion_handler);
1900 widget_set_button_handler(frame->widget, frame_button_handler);
1901
Martin Minarik1998b152012-05-10 02:04:35 +02001902 /* Create empty list for frame buttons */
1903 wl_list_init(&frame->buttons_list);
1904
1905 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1906 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1907
1908 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1909 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1910
1911 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1912 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1913
1914 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1915 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1916
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001917 window->frame = frame;
1918
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001919 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001920}
1921
Kristian Høgsberga1627922012-06-20 17:30:03 -04001922void
1923frame_set_child_size(struct widget *widget, int child_width, int child_height)
1924{
1925 struct display *display = widget->window->display;
1926 struct theme *t = display->theme;
1927 int decoration_width, decoration_height;
1928 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001929 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001930
1931 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001932 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001933 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001934 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001935
1936 width = child_width + decoration_width;
1937 height = child_height + decoration_height;
1938 } else {
1939 width = child_width;
1940 height = child_height;
1941 }
1942
1943 window_schedule_resize(widget->window, width, height);
1944}
1945
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001946static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001947frame_destroy(struct frame *frame)
1948{
Martin Minarik1998b152012-05-10 02:04:35 +02001949 struct frame_button *button, *tmp;
1950
1951 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1952 frame_button_destroy(button);
1953
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001954 /* frame->child must be destroyed by the application */
1955 widget_destroy(frame->widget);
1956 free(frame);
1957}
1958
1959static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001960input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001961 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001962{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001963 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001964 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001965
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001966 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001967 return;
1968
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001969 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001970 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001971 widget = old;
1972 if (input->grab)
1973 widget = input->grab;
1974 if (widget->leave_handler)
1975 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001976 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001977 }
1978
1979 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001980 widget = focus;
1981 if (input->grab)
1982 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001983 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001984 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001985 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001986 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001987
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001988 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001989 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001990}
1991
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001992void
1993input_grab(struct input *input, struct widget *widget, uint32_t button)
1994{
1995 input->grab = widget;
1996 input->grab_button = button;
1997}
1998
1999void
2000input_ungrab(struct input *input)
2001{
2002 struct widget *widget;
2003
2004 input->grab = NULL;
2005 if (input->pointer_focus) {
2006 widget = widget_find_widget(input->pointer_focus->widget,
2007 input->sx, input->sy);
2008 input_set_focus_widget(input, widget, input->sx, input->sy);
2009 }
2010}
2011
2012static void
2013input_remove_pointer_focus(struct input *input)
2014{
2015 struct window *window = input->pointer_focus;
2016
2017 if (!window)
2018 return;
2019
2020 input_set_focus_widget(input, NULL, 0, 0);
2021
2022 input->pointer_focus = NULL;
2023 input->current_cursor = CURSOR_UNSET;
2024}
2025
2026static void
2027pointer_handle_enter(void *data, struct wl_pointer *pointer,
2028 uint32_t serial, struct wl_surface *surface,
2029 wl_fixed_t sx_w, wl_fixed_t sy_w)
2030{
2031 struct input *input = data;
2032 struct window *window;
2033 struct widget *widget;
2034 float sx = wl_fixed_to_double(sx_w);
2035 float sy = wl_fixed_to_double(sy_w);
2036
2037 if (!surface) {
2038 /* enter event for a window we've just destroyed */
2039 return;
2040 }
2041
2042 input->display->serial = serial;
2043 input->pointer_enter_serial = serial;
2044 input->pointer_focus = wl_surface_get_user_data(surface);
2045 window = input->pointer_focus;
2046
2047 if (window->pool) {
2048 shm_pool_destroy(window->pool);
2049 window->pool = NULL;
2050 /* Schedule a redraw to free the pool */
2051 window_schedule_redraw(window);
2052 }
2053
2054 input->sx = sx;
2055 input->sy = sy;
2056
2057 widget = widget_find_widget(window->widget, sx, sy);
2058 input_set_focus_widget(input, widget, sx, sy);
2059}
2060
2061static void
2062pointer_handle_leave(void *data, struct wl_pointer *pointer,
2063 uint32_t serial, struct wl_surface *surface)
2064{
2065 struct input *input = data;
2066
2067 input->display->serial = serial;
2068 input_remove_pointer_focus(input);
2069}
2070
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002071static void
Daniel Stone37816df2012-05-16 18:45:18 +01002072pointer_handle_motion(void *data, struct wl_pointer *pointer,
2073 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002074{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002075 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002076 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002077 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002078 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002079 float sx = wl_fixed_to_double(sx_w);
2080 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002081
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082 input->sx = sx;
2083 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002084
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002085 if (!window)
2086 return;
2087
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002088 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002089 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002090 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002091 }
2092
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002093 if (input->grab)
2094 widget = input->grab;
2095 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002096 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002097 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002098 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002099 input, time, sx, sy,
2100 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002101
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002102 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002103}
2104
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002105static void
Daniel Stone37816df2012-05-16 18:45:18 +01002106pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002107 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002108{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002109 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002110 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002111 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002112
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002113 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002114 if (input->focus_widget && input->grab == NULL &&
2115 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002116 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002117
Neil Roberts6b28aad2012-01-23 19:11:18 +00002118 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002120 (*widget->button_handler)(widget,
2121 input, time,
2122 button, state,
2123 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002124
Daniel Stone4dbadb12012-05-30 16:31:51 +01002125 if (input->grab && input->grab_button == button &&
2126 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002127 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002128}
2129
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002130static void
Daniel Stone37816df2012-05-16 18:45:18 +01002131pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002132 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002133{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002134 struct input *input = data;
2135 struct widget *widget;
2136
2137 widget = input->focus_widget;
2138 if (input->grab)
2139 widget = input->grab;
2140 if (widget && widget->axis_handler)
2141 (*widget->axis_handler)(widget,
2142 input, time,
2143 axis, value,
2144 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002145}
2146
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002147static const struct wl_pointer_listener pointer_listener = {
2148 pointer_handle_enter,
2149 pointer_handle_leave,
2150 pointer_handle_motion,
2151 pointer_handle_button,
2152 pointer_handle_axis,
2153};
2154
2155static void
2156input_remove_keyboard_focus(struct input *input)
2157{
2158 struct window *window = input->keyboard_focus;
2159 struct itimerspec its;
2160
2161 its.it_interval.tv_sec = 0;
2162 its.it_interval.tv_nsec = 0;
2163 its.it_value.tv_sec = 0;
2164 its.it_value.tv_nsec = 0;
2165 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2166
2167 if (!window)
2168 return;
2169
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002170 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002171 if (window->keyboard_focus_handler)
2172 (*window->keyboard_focus_handler)(window, NULL,
2173 window->user_data);
2174
2175 input->keyboard_focus = NULL;
2176}
2177
2178static void
2179keyboard_repeat_func(struct task *task, uint32_t events)
2180{
2181 struct input *input =
2182 container_of(task, struct input, repeat_task);
2183 struct window *window = input->keyboard_focus;
2184 uint64_t exp;
2185
2186 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2187 /* If we change the timer between the fd becoming
2188 * readable and getting here, there'll be nothing to
2189 * read and we get EAGAIN. */
2190 return;
2191
2192 if (window && window->key_handler) {
2193 (*window->key_handler)(window, input, input->repeat_time,
2194 input->repeat_key, input->repeat_sym,
2195 WL_KEYBOARD_KEY_STATE_PRESSED,
2196 window->user_data);
2197 }
2198}
2199
2200static void
2201keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2202 uint32_t format, int fd, uint32_t size)
2203{
2204 struct input *input = data;
2205 char *map_str;
2206
2207 if (!data) {
2208 close(fd);
2209 return;
2210 }
2211
2212 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2213 close(fd);
2214 return;
2215 }
2216
2217 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2218 if (map_str == MAP_FAILED) {
2219 close(fd);
2220 return;
2221 }
2222
2223 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2224 map_str,
2225 XKB_KEYMAP_FORMAT_TEXT_V1,
2226 0);
2227 munmap(map_str, size);
2228 close(fd);
2229
2230 if (!input->xkb.keymap) {
2231 fprintf(stderr, "failed to compile keymap\n");
2232 return;
2233 }
2234
2235 input->xkb.state = xkb_state_new(input->xkb.keymap);
2236 if (!input->xkb.state) {
2237 fprintf(stderr, "failed to create XKB state\n");
2238 xkb_map_unref(input->xkb.keymap);
2239 input->xkb.keymap = NULL;
2240 return;
2241 }
2242
2243 input->xkb.control_mask =
2244 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2245 input->xkb.alt_mask =
2246 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2247 input->xkb.shift_mask =
2248 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2249}
2250
2251static void
2252keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2253 uint32_t serial, struct wl_surface *surface,
2254 struct wl_array *keys)
2255{
2256 struct input *input = data;
2257 struct window *window;
2258
2259 input->display->serial = serial;
2260 input->keyboard_focus = wl_surface_get_user_data(surface);
2261
2262 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002263 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002264 if (window->keyboard_focus_handler)
2265 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002266 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002267}
2268
2269static void
2270keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2271 uint32_t serial, struct wl_surface *surface)
2272{
2273 struct input *input = data;
2274
2275 input->display->serial = serial;
2276 input_remove_keyboard_focus(input);
2277}
2278
Scott Moreau210d0792012-03-22 10:47:01 -06002279static void
Daniel Stone37816df2012-05-16 18:45:18 +01002280keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002281 uint32_t serial, uint32_t time, uint32_t key,
2282 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002283{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002284 struct input *input = data;
2285 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002286 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002287 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002288 const xkb_keysym_t *syms;
2289 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002290 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002291
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002292 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002293 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002294 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002295 return;
2296
Daniel Stone97f68542012-05-30 16:32:01 +01002297 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002298
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002299 sym = XKB_KEY_NoSymbol;
2300 if (num_syms == 1)
2301 sym = syms[0];
2302
2303 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002304 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002305 window_set_maximized(window,
2306 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002307 } else if (sym == XKB_KEY_F11 &&
2308 window->fullscreen_handler &&
2309 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2310 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002311 } else if (sym == XKB_KEY_F4 &&
2312 input->modifiers == MOD_ALT_MASK &&
2313 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2314 if (window->close_handler)
2315 window->close_handler(window->parent,
2316 window->user_data);
2317 else
2318 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002319 } else if (window->key_handler) {
2320 (*window->key_handler)(window, input, time, key,
2321 sym, state, window->user_data);
2322 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002323
2324 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2325 key == input->repeat_key) {
2326 its.it_interval.tv_sec = 0;
2327 its.it_interval.tv_nsec = 0;
2328 its.it_value.tv_sec = 0;
2329 its.it_value.tv_nsec = 0;
2330 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2331 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2332 input->repeat_sym = sym;
2333 input->repeat_key = key;
2334 input->repeat_time = time;
2335 its.it_interval.tv_sec = 0;
2336 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2337 its.it_value.tv_sec = 0;
2338 its.it_value.tv_nsec = 400 * 1000 * 1000;
2339 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2340 }
2341}
2342
2343static void
Daniel Stone351eb612012-05-31 15:27:47 -04002344keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2345 uint32_t serial, uint32_t mods_depressed,
2346 uint32_t mods_latched, uint32_t mods_locked,
2347 uint32_t group)
2348{
2349 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002350 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002351
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002352 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2353 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002354 mask = xkb_state_serialize_mods(input->xkb.state,
2355 XKB_STATE_DEPRESSED |
2356 XKB_STATE_LATCHED);
2357 input->modifiers = 0;
2358 if (mask & input->xkb.control_mask)
2359 input->modifiers |= MOD_CONTROL_MASK;
2360 if (mask & input->xkb.alt_mask)
2361 input->modifiers |= MOD_ALT_MASK;
2362 if (mask & input->xkb.shift_mask)
2363 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002364}
2365
Daniel Stone37816df2012-05-16 18:45:18 +01002366static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002367 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002368 keyboard_handle_enter,
2369 keyboard_handle_leave,
2370 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002371 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002372};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002373
2374static void
Daniel Stone37816df2012-05-16 18:45:18 +01002375seat_handle_capabilities(void *data, struct wl_seat *seat,
2376 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002377{
Daniel Stone37816df2012-05-16 18:45:18 +01002378 struct input *input = data;
2379
2380 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2381 input->pointer = wl_seat_get_pointer(seat);
2382 wl_pointer_set_user_data(input->pointer, input);
2383 wl_pointer_add_listener(input->pointer, &pointer_listener,
2384 input);
2385 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2386 wl_pointer_destroy(input->pointer);
2387 input->pointer = NULL;
2388 }
2389
2390 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2391 input->keyboard = wl_seat_get_keyboard(seat);
2392 wl_keyboard_set_user_data(input->keyboard, input);
2393 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2394 input);
2395 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2396 wl_keyboard_destroy(input->keyboard);
2397 input->keyboard = NULL;
2398 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002399}
2400
Daniel Stone37816df2012-05-16 18:45:18 +01002401static const struct wl_seat_listener seat_listener = {
2402 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002403};
2404
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002405void
2406input_get_position(struct input *input, int32_t *x, int32_t *y)
2407{
2408 *x = input->sx;
2409 *y = input->sy;
2410}
2411
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002412struct display *
2413input_get_display(struct input *input)
2414{
2415 return input->display;
2416}
2417
Daniel Stone37816df2012-05-16 18:45:18 +01002418struct wl_seat *
2419input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002420{
Daniel Stone37816df2012-05-16 18:45:18 +01002421 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002422}
2423
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002424uint32_t
2425input_get_modifiers(struct input *input)
2426{
2427 return input->modifiers;
2428}
2429
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002430struct widget *
2431input_get_focus_widget(struct input *input)
2432{
2433 return input->focus_widget;
2434}
2435
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002436struct data_offer {
2437 struct wl_data_offer *offer;
2438 struct input *input;
2439 struct wl_array types;
2440 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002441
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002442 struct task io_task;
2443 int fd;
2444 data_func_t func;
2445 int32_t x, y;
2446 void *user_data;
2447};
2448
2449static void
2450data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2451{
2452 struct data_offer *offer = data;
2453 char **p;
2454
2455 p = wl_array_add(&offer->types, sizeof *p);
2456 *p = strdup(type);
2457}
2458
2459static const struct wl_data_offer_listener data_offer_listener = {
2460 data_offer_offer,
2461};
2462
2463static void
2464data_offer_destroy(struct data_offer *offer)
2465{
2466 char **p;
2467
2468 offer->refcount--;
2469 if (offer->refcount == 0) {
2470 wl_data_offer_destroy(offer->offer);
2471 for (p = offer->types.data; *p; p++)
2472 free(*p);
2473 wl_array_release(&offer->types);
2474 free(offer);
2475 }
2476}
2477
2478static void
2479data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002480 struct wl_data_device *data_device,
2481 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002482{
2483 struct data_offer *offer;
2484
2485 offer = malloc(sizeof *offer);
2486
2487 wl_array_init(&offer->types);
2488 offer->refcount = 1;
2489 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002490 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002491 wl_data_offer_add_listener(offer->offer,
2492 &data_offer_listener, offer);
2493}
2494
2495static void
2496data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002497 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002498 wl_fixed_t x_w, wl_fixed_t y_w,
2499 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002500{
2501 struct input *input = data;
2502 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002503 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002504 float x = wl_fixed_to_double(x_w);
2505 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002506 char **p;
2507
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002508 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002509 window = wl_surface_get_user_data(surface);
2510 input->pointer_focus = window;
2511
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002512 if (offer) {
2513 input->drag_offer = wl_data_offer_get_user_data(offer);
2514
2515 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2516 *p = NULL;
2517
2518 types_data = input->drag_offer->types.data;
2519 } else {
2520 input->drag_offer = NULL;
2521 types_data = NULL;
2522 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002523
2524 window = input->pointer_focus;
2525 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002526 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002527 window->user_data);
2528}
2529
2530static void
2531data_device_leave(void *data, struct wl_data_device *data_device)
2532{
2533 struct input *input = data;
2534
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002535 if (input->drag_offer) {
2536 data_offer_destroy(input->drag_offer);
2537 input->drag_offer = NULL;
2538 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002539}
2540
2541static void
2542data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002543 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002544{
2545 struct input *input = data;
2546 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002547 float x = wl_fixed_to_double(x_w);
2548 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002549 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002550
2551 input->sx = x;
2552 input->sy = y;
2553
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002554 if (input->drag_offer)
2555 types_data = input->drag_offer->types.data;
2556 else
2557 types_data = NULL;
2558
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002559 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002560 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002561 window->user_data);
2562}
2563
2564static void
2565data_device_drop(void *data, struct wl_data_device *data_device)
2566{
2567 struct input *input = data;
2568 struct window *window = input->pointer_focus;
2569
2570 if (window->drop_handler)
2571 window->drop_handler(window, input,
2572 input->sx, input->sy, window->user_data);
2573}
2574
2575static void
2576data_device_selection(void *data,
2577 struct wl_data_device *wl_data_device,
2578 struct wl_data_offer *offer)
2579{
2580 struct input *input = data;
2581 char **p;
2582
2583 if (input->selection_offer)
2584 data_offer_destroy(input->selection_offer);
2585
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002586 if (offer) {
2587 input->selection_offer = wl_data_offer_get_user_data(offer);
2588 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2589 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002590 } else {
2591 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002592 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002593}
2594
2595static const struct wl_data_device_listener data_device_listener = {
2596 data_device_data_offer,
2597 data_device_enter,
2598 data_device_leave,
2599 data_device_motion,
2600 data_device_drop,
2601 data_device_selection
2602};
2603
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002604static void
2605input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002606{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002608 struct wl_cursor *cursor;
2609 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002610
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002611 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002612 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002613 return;
2614
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002615 if (index >= (int) cursor->image_count) {
2616 fprintf(stderr, "cursor index out of range\n");
2617 return;
2618 }
2619
2620 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002621 buffer = wl_cursor_image_get_buffer(image);
2622 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002623 return;
2624
Kristian Høgsbergae277372012-08-01 09:41:08 -04002625 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002626 input->pointer_surface,
2627 image->hotspot_x, image->hotspot_y);
2628 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2629 wl_surface_damage(input->pointer_surface, 0, 0,
2630 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002631 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002632}
2633
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002634static const struct wl_callback_listener pointer_surface_listener;
2635
2636static void
2637pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2638 uint32_t time)
2639{
2640 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002641 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002642 int i;
2643
2644 if (callback) {
2645 assert(callback == input->cursor_frame_cb);
2646 wl_callback_destroy(callback);
2647 input->cursor_frame_cb = NULL;
2648 }
2649
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002650 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002651 wl_pointer_set_cursor(input->pointer,
2652 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002653 NULL, 0, 0);
2654 return;
2655 }
2656
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002657 if (input->current_cursor == CURSOR_UNSET)
2658 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002659 cursor = input->display->cursors[input->current_cursor];
2660 if (!cursor)
2661 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002662
2663 /* FIXME We don't have the current time on the first call so we set
2664 * the animation start to the time of the first frame callback. */
2665 if (time == 0)
2666 input->cursor_anim_start = 0;
2667 else if (input->cursor_anim_start == 0)
2668 input->cursor_anim_start = time;
2669
2670 if (time == 0 || input->cursor_anim_start == 0)
2671 i = 0;
2672 else
2673 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2674
Pekka Paalanenbc106382012-10-10 12:49:31 +03002675 if (cursor->image_count > 1) {
2676 input->cursor_frame_cb =
2677 wl_surface_frame(input->pointer_surface);
2678 wl_callback_add_listener(input->cursor_frame_cb,
2679 &pointer_surface_listener, input);
2680 }
2681
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002682 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002683}
2684
2685static const struct wl_callback_listener pointer_surface_listener = {
2686 pointer_surface_frame_callback
2687};
2688
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002689void
2690input_set_pointer_image(struct input *input, int pointer)
2691{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002692 int force = 0;
2693
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04002694 if (!input->pointer)
2695 return;
2696
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002697 if (input->pointer_enter_serial > input->cursor_serial)
2698 force = 1;
2699
2700 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002701 return;
2702
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002703 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002704 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002705 if (!input->cursor_frame_cb)
2706 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002707 else if (force) {
2708 /* The current frame callback may be stuck if, for instance,
2709 * the set cursor request was processed by the server after
2710 * this client lost the focus. In this case the cursor surface
2711 * might not be mapped and the frame callback wouldn't ever
2712 * complete. Send a set_cursor and attach to try to map the
2713 * cursor surface again so that the callback will finish */
2714 input_set_pointer_image_index(input, 0);
2715 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002716}
2717
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002718struct wl_data_device *
2719input_get_data_device(struct input *input)
2720{
2721 return input->data_device;
2722}
2723
2724void
2725input_set_selection(struct input *input,
2726 struct wl_data_source *source, uint32_t time)
2727{
2728 wl_data_device_set_selection(input->data_device, source, time);
2729}
2730
2731void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002732input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002733{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002734 wl_data_offer_accept(input->drag_offer->offer,
2735 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002736}
2737
2738static void
2739offer_io_func(struct task *task, uint32_t events)
2740{
2741 struct data_offer *offer =
2742 container_of(task, struct data_offer, io_task);
2743 unsigned int len;
2744 char buffer[4096];
2745
2746 len = read(offer->fd, buffer, sizeof buffer);
2747 offer->func(buffer, len,
2748 offer->x, offer->y, offer->user_data);
2749
2750 if (len == 0) {
2751 close(offer->fd);
2752 data_offer_destroy(offer);
2753 }
2754}
2755
2756static void
2757data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2758 data_func_t func, void *user_data)
2759{
2760 int p[2];
2761
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002762 if (pipe2(p, O_CLOEXEC) == -1)
2763 return;
2764
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002765 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2766 close(p[1]);
2767
2768 offer->io_task.run = offer_io_func;
2769 offer->fd = p[0];
2770 offer->func = func;
2771 offer->refcount++;
2772 offer->user_data = user_data;
2773
2774 display_watch_fd(offer->input->display,
2775 offer->fd, EPOLLIN, &offer->io_task);
2776}
2777
2778void
2779input_receive_drag_data(struct input *input, const char *mime_type,
2780 data_func_t func, void *data)
2781{
2782 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2783 input->drag_offer->x = input->sx;
2784 input->drag_offer->y = input->sy;
2785}
2786
2787int
2788input_receive_selection_data(struct input *input, const char *mime_type,
2789 data_func_t func, void *data)
2790{
2791 char **p;
2792
2793 if (input->selection_offer == NULL)
2794 return -1;
2795
2796 for (p = input->selection_offer->types.data; *p; p++)
2797 if (strcmp(mime_type, *p) == 0)
2798 break;
2799
2800 if (*p == NULL)
2801 return -1;
2802
2803 data_offer_receive_data(input->selection_offer,
2804 mime_type, func, data);
2805 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002806}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002807
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002808int
2809input_receive_selection_data_to_fd(struct input *input,
2810 const char *mime_type, int fd)
2811{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002812 if (input->selection_offer)
2813 wl_data_offer_receive(input->selection_offer->offer,
2814 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002815
2816 return 0;
2817}
2818
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002819void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002820window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002821{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002822 if (!window->shell_surface)
2823 return;
2824
Daniel Stone37816df2012-05-16 18:45:18 +01002825 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002826}
2827
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002828static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002829idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002830{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002831 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002832
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002833 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002834 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002835 widget_set_allocation(widget,
2836 window->pending_allocation.x,
2837 window->pending_allocation.y,
2838 window->pending_allocation.width,
2839 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002840
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002841 if (window->input_region) {
2842 wl_region_destroy(window->input_region);
2843 window->input_region = NULL;
2844 }
2845
2846 if (window->opaque_region) {
2847 wl_region_destroy(window->opaque_region);
2848 window->opaque_region = NULL;
2849 }
2850
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002851 if (widget->resize_handler)
2852 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002853 widget->allocation.width,
2854 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002855 widget->user_data);
2856
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002857 if (window->allocation.width != widget->allocation.width ||
2858 window->allocation.height != widget->allocation.height) {
2859 window->allocation = widget->allocation;
2860 window_schedule_redraw(window);
2861 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002862}
2863
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002864void
2865window_schedule_resize(struct window *window, int width, int height)
2866{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002867 window->pending_allocation.x = 0;
2868 window->pending_allocation.y = 0;
2869 window->pending_allocation.width = width;
2870 window->pending_allocation.height = height;
2871
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002872 if (window->min_allocation.width == 0)
2873 window->min_allocation = window->pending_allocation;
2874 if (window->pending_allocation.width < window->min_allocation.width)
2875 window->pending_allocation.width = window->min_allocation.width;
2876 if (window->pending_allocation.height < window->min_allocation.height)
2877 window->pending_allocation.height = window->min_allocation.height;
2878
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002879 window->resize_needed = 1;
2880 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002881}
2882
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002883void
2884widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2885{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002886 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002887}
2888
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002889static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002890handle_ping(void *data, struct wl_shell_surface *shell_surface,
2891 uint32_t serial)
2892{
2893 wl_shell_surface_pong(shell_surface, serial);
2894}
2895
2896static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002897handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002898 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002899{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002900 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002901
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002902 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002903 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002904}
2905
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002906static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002907menu_destroy(struct menu *menu)
2908{
2909 widget_destroy(menu->widget);
2910 window_destroy(menu->window);
2911 free(menu);
2912}
2913
2914static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002915handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2916{
2917 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002918 struct menu *menu = window->widget->user_data;
2919
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002920 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002921 * device. Or just use wl_callback. And this really needs to
2922 * be a window vfunc that the menu can set. And we need the
2923 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002924
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002925 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002926 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002927 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002928}
2929
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002930static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002931 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002932 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002933 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002934};
2935
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002936void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002937window_get_allocation(struct window *window,
2938 struct rectangle *allocation)
2939{
2940 *allocation = window->allocation;
2941}
2942
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002943static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002944widget_redraw(struct widget *widget)
2945{
2946 struct widget *child;
2947
2948 if (widget->redraw_handler)
2949 widget->redraw_handler(widget, widget->user_data);
2950 wl_list_for_each(child, &widget->child_list, link)
2951 widget_redraw(child);
2952}
2953
2954static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002955frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2956{
2957 struct window *window = data;
2958
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002959 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002960 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002961 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002962 window->redraw_scheduled = 0;
2963 if (window->redraw_needed)
2964 window_schedule_redraw(window);
2965}
2966
2967static const struct wl_callback_listener listener = {
2968 frame_callback
2969};
2970
2971static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002972idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002973{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002974 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002975
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002976 if (window->resize_needed)
2977 idle_resize(window);
2978
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002979 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002980 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002981 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002982 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002983
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002984 window->frame_cb = wl_surface_frame(window->surface);
2985 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002986 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002987}
2988
2989void
2990window_schedule_redraw(struct window *window)
2991{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002992 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002993 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002994 window->redraw_task.run = idle_redraw;
2995 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002996 window->redraw_scheduled = 1;
2997 }
2998}
2999
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003000int
3001window_is_fullscreen(struct window *window)
3002{
3003 return window->type == TYPE_FULLSCREEN;
3004}
3005
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003006void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003007window_set_fullscreen(struct window *window, int fullscreen)
3008{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003009 if (!window->display->shell)
3010 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003011
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003012 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003013 return;
3014
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003015 if (fullscreen) {
3016 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003017 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003018 wl_shell_surface_set_fullscreen(window->shell_surface,
3019 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3020 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003021 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003022 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003023 wl_shell_surface_set_toplevel(window->shell_surface);
3024 window_schedule_resize(window,
3025 window->saved_allocation.width,
3026 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003027 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003028}
3029
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003030int
3031window_is_maximized(struct window *window)
3032{
3033 return window->type == TYPE_MAXIMIZED;
3034}
3035
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003036void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003037window_set_maximized(struct window *window, int maximized)
3038{
3039 if (!window->display->shell)
3040 return;
3041
3042 if ((window->type == TYPE_MAXIMIZED) == maximized)
3043 return;
3044
3045 if (window->type == TYPE_TOPLEVEL) {
3046 window->saved_allocation = window->allocation;
3047 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3048 window->type = TYPE_MAXIMIZED;
3049 } else {
3050 wl_shell_surface_set_toplevel(window->shell_surface);
3051 window->type = TYPE_TOPLEVEL;
3052 window_schedule_resize(window,
3053 window->saved_allocation.width,
3054 window->saved_allocation.height);
3055 }
3056}
3057
3058void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003059window_set_user_data(struct window *window, void *data)
3060{
3061 window->user_data = data;
3062}
3063
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003064void *
3065window_get_user_data(struct window *window)
3066{
3067 return window->user_data;
3068}
3069
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003070void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003071window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003072 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003073{
3074 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003075}
3076
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003077void
3078window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003079 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003080{
3081 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003082}
3083
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003084void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003085window_set_data_handler(struct window *window, window_data_handler_t handler)
3086{
3087 window->data_handler = handler;
3088}
3089
3090void
3091window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3092{
3093 window->drop_handler = handler;
3094}
3095
3096void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003097window_set_close_handler(struct window *window,
3098 window_close_handler_t handler)
3099{
3100 window->close_handler = handler;
3101}
3102
3103void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003104window_set_fullscreen_handler(struct window *window,
3105 window_fullscreen_handler_t handler)
3106{
3107 window->fullscreen_handler = handler;
3108}
3109
3110void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003111window_set_title(struct window *window, const char *title)
3112{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003113 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003114 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003115 if (window->shell_surface)
3116 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003117}
3118
3119const char *
3120window_get_title(struct window *window)
3121{
3122 return window->title;
3123}
3124
3125void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003126window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3127{
3128 struct text_cursor_position *text_cursor_position =
3129 window->display->text_cursor_position;
3130
Scott Moreau9295ce02012-06-01 12:46:10 -06003131 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003132 return;
3133
3134 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003135 window->surface,
3136 wl_fixed_from_int(x),
3137 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003138}
3139
3140void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003141window_damage(struct window *window, int32_t x, int32_t y,
3142 int32_t width, int32_t height)
3143{
3144 wl_surface_damage(window->surface, x, y, width, height);
3145}
3146
Casey Dahlin9074db52012-04-19 22:50:09 -04003147static void
3148surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003149 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003150{
Rob Bradford7507b572012-05-15 17:55:34 +01003151 struct window *window = data;
3152 struct output *output;
3153 struct output *output_found = NULL;
3154 struct window_output *window_output;
3155
3156 wl_list_for_each(output, &window->display->output_list, link) {
3157 if (output->output == wl_output) {
3158 output_found = output;
3159 break;
3160 }
3161 }
3162
3163 if (!output_found)
3164 return;
3165
3166 window_output = malloc (sizeof *window_output);
3167 window_output->output = output_found;
3168
3169 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003170}
3171
3172static void
3173surface_leave(void *data,
3174 struct wl_surface *wl_surface, struct wl_output *output)
3175{
Rob Bradford7507b572012-05-15 17:55:34 +01003176 struct window *window = data;
3177 struct window_output *window_output;
3178 struct window_output *window_output_found = NULL;
3179
3180 wl_list_for_each(window_output, &window->window_output_list, link) {
3181 if (window_output->output->output == output) {
3182 window_output_found = window_output;
3183 break;
3184 }
3185 }
3186
3187 if (window_output_found) {
3188 wl_list_remove(&window_output_found->link);
3189 free(window_output_found);
3190 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003191}
3192
3193static const struct wl_surface_listener surface_listener = {
3194 surface_enter,
3195 surface_leave
3196};
3197
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003198static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003199window_create_internal(struct display *display,
3200 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003201{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003202 struct window *window;
3203
3204 window = malloc(sizeof *window);
3205 if (window == NULL)
3206 return NULL;
3207
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003208 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003209 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003210 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003211 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003212 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003213 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003214 window->shell_surface =
3215 wl_shell_get_shell_surface(display->shell,
3216 window->surface);
3217 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003218 window->allocation.x = 0;
3219 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003220 window->allocation.width = 0;
3221 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003222 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003223 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003224 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003225 window->input_region = NULL;
3226 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003227
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003228 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003229#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003230 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003231#else
3232 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3233#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003234 else
3235 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003236
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003237 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003238 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003239 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003240
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003241 if (window->shell_surface) {
3242 wl_shell_surface_set_user_data(window->shell_surface, window);
3243 wl_shell_surface_add_listener(window->shell_surface,
3244 &shell_surface_listener, window);
3245 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003246
Rob Bradford7507b572012-05-15 17:55:34 +01003247 wl_list_init (&window->window_output_list);
3248
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003249 return window;
3250}
3251
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003252struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003253window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003254{
3255 struct window *window;
3256
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003257 window = window_create_internal(display, NULL, TYPE_NONE);
3258 if (!window)
3259 return NULL;
3260
3261 return window;
3262}
3263
3264struct window *
3265window_create_custom(struct display *display)
3266{
3267 struct window *window;
3268
3269 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003270 if (!window)
3271 return NULL;
3272
3273 return window;
3274}
3275
3276struct window *
3277window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003278 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003279{
3280 struct window *window;
3281
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003282 window = window_create_internal(parent->display,
3283 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003284 if (!window)
3285 return NULL;
3286
3287 window->x = x;
3288 window->y = y;
3289
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003290 if (display->shell)
3291 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003292 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003293 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003294
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003295 return window;
3296}
3297
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003298static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003299menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003300{
3301 int next;
3302
3303 next = (sy - 8) / 20;
3304 if (menu->current != next) {
3305 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003306 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003307 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003308}
3309
3310static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003311menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003312 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003313 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003314{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003315 struct menu *menu = data;
3316
3317 if (widget == menu->widget)
3318 menu_set_item(data, y);
3319
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003320 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003321}
3322
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003323static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003324menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003325 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003326{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003327 struct menu *menu = data;
3328
3329 if (widget == menu->widget)
3330 menu_set_item(data, y);
3331
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003332 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003333}
3334
3335static void
3336menu_leave_handler(struct widget *widget, struct input *input, void *data)
3337{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003338 struct menu *menu = data;
3339
3340 if (widget == menu->widget)
3341 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003342}
3343
3344static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003345menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003346 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003347 uint32_t button, enum wl_pointer_button_state state,
3348 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003349
3350{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003351 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003352
Daniel Stone4dbadb12012-05-30 16:31:51 +01003353 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3354 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003355 /* Either relase after press-drag-release or
3356 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003357 menu->func(menu->window->parent,
3358 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003359 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003360 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003361 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003362}
3363
3364static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003365menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366{
3367 cairo_t *cr;
3368 const int32_t r = 3, margin = 3;
3369 struct menu *menu = data;
3370 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003371 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003372
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003373 cr = cairo_create(window->cairo_surface);
3374 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3375 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3376 cairo_paint(cr);
3377
3378 width = window->allocation.width;
3379 height = window->allocation.height;
3380 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003381
3382 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003383 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3384 cairo_fill(cr);
3385
3386 for (i = 0; i < menu->count; i++) {
3387 if (i == menu->current) {
3388 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3389 cairo_rectangle(cr, margin, i * 20 + margin,
3390 width - 2 * margin, 20);
3391 cairo_fill(cr);
3392 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3393 cairo_move_to(cr, 10, i * 20 + 16);
3394 cairo_show_text(cr, menu->entries[i]);
3395 } else {
3396 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3397 cairo_move_to(cr, 10, i * 20 + 16);
3398 cairo_show_text(cr, menu->entries[i]);
3399 }
3400 }
3401
3402 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003403}
3404
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003405void
3406window_show_menu(struct display *display,
3407 struct input *input, uint32_t time, struct window *parent,
3408 int32_t x, int32_t y,
3409 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003410{
3411 struct window *window;
3412 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003413 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003414
3415 menu = malloc(sizeof *menu);
3416 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003417 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003418
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003419 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003420 if (!window) {
3421 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003422 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003423 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003424
3425 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003426 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003427 menu->entries = entries;
3428 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003429 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003430 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003431 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003432 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003433 window->type = TYPE_MENU;
3434 window->x = x;
3435 window->y = y;
3436
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003437 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003438 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003439 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003440 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003441 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003442
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003443 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003444 widget_set_enter_handler(menu->widget, menu_enter_handler);
3445 widget_set_leave_handler(menu->widget, menu_leave_handler);
3446 widget_set_motion_handler(menu->widget, menu_motion_handler);
3447 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003448
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003449 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003450 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003451}
3452
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003453void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003454window_set_buffer_type(struct window *window, enum window_buffer_type type)
3455{
3456 window->buffer_type = type;
3457}
3458
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003459
3460static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003461display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003462 struct wl_output *wl_output,
3463 int x, int y,
3464 int physical_width,
3465 int physical_height,
3466 int subpixel,
3467 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003468 const char *model,
3469 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003470{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003471 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003472
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003473 output->allocation.x = x;
3474 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003475 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003476}
3477
3478static void
3479display_handle_mode(void *data,
3480 struct wl_output *wl_output,
3481 uint32_t flags,
3482 int width,
3483 int height,
3484 int refresh)
3485{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003486 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003487 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003488
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003489 if (flags & WL_OUTPUT_MODE_CURRENT) {
3490 output->allocation.width = width;
3491 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003492 if (display->output_configure_handler)
3493 (*display->output_configure_handler)(
3494 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003495 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003496}
3497
3498static const struct wl_output_listener output_listener = {
3499 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003500 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003501};
3502
3503static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003504display_add_output(struct display *d, uint32_t id)
3505{
3506 struct output *output;
3507
3508 output = malloc(sizeof *output);
3509 if (output == NULL)
3510 return;
3511
3512 memset(output, 0, sizeof *output);
3513 output->display = d;
3514 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003515 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003516 wl_list_insert(d->output_list.prev, &output->link);
3517
3518 wl_output_add_listener(output->output, &output_listener, output);
3519}
3520
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003521static void
3522output_destroy(struct output *output)
3523{
3524 if (output->destroy_handler)
3525 (*output->destroy_handler)(output, output->user_data);
3526
3527 wl_output_destroy(output->output);
3528 wl_list_remove(&output->link);
3529 free(output);
3530}
3531
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003532void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003533display_set_global_handler(struct display *display,
3534 display_global_handler_t handler)
3535{
3536 struct global *global;
3537
3538 display->global_handler = handler;
3539 if (!handler)
3540 return;
3541
3542 wl_list_for_each(global, &display->global_list, link)
3543 display->global_handler(display,
3544 global->name, global->interface,
3545 global->version, display->user_data);
3546}
3547
3548void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003549display_set_output_configure_handler(struct display *display,
3550 display_output_handler_t handler)
3551{
3552 struct output *output;
3553
3554 display->output_configure_handler = handler;
3555 if (!handler)
3556 return;
3557
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003558 wl_list_for_each(output, &display->output_list, link) {
3559 if (output->allocation.width == 0 &&
3560 output->allocation.height == 0)
3561 continue;
3562
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003563 (*display->output_configure_handler)(output,
3564 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003565 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003566}
3567
3568void
3569output_set_user_data(struct output *output, void *data)
3570{
3571 output->user_data = data;
3572}
3573
3574void *
3575output_get_user_data(struct output *output)
3576{
3577 return output->user_data;
3578}
3579
3580void
3581output_set_destroy_handler(struct output *output,
3582 display_output_handler_t handler)
3583{
3584 output->destroy_handler = handler;
3585 /* FIXME: implement this, once we have way to remove outputs */
3586}
3587
3588void
Scott Moreau4e072362012-09-29 02:03:11 -06003589output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003590{
Scott Moreau4e072362012-09-29 02:03:11 -06003591 struct rectangle allocation = output->allocation;
3592
3593 switch (output->transform) {
3594 case WL_OUTPUT_TRANSFORM_90:
3595 case WL_OUTPUT_TRANSFORM_270:
3596 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3597 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3598 /* Swap width and height */
3599 allocation.width = output->allocation.height;
3600 allocation.height = output->allocation.width;
3601 break;
3602 }
3603
3604 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003605}
3606
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003607struct wl_output *
3608output_get_wl_output(struct output *output)
3609{
3610 return output->output;
3611}
3612
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003613static void
Daniel Stone97f68542012-05-30 16:32:01 +01003614fini_xkb(struct input *input)
3615{
3616 xkb_state_unref(input->xkb.state);
3617 xkb_map_unref(input->xkb.keymap);
3618}
3619
3620static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003621display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003622{
3623 struct input *input;
3624
3625 input = malloc(sizeof *input);
3626 if (input == NULL)
3627 return;
3628
3629 memset(input, 0, sizeof *input);
3630 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003631 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003632 input->pointer_focus = NULL;
3633 input->keyboard_focus = NULL;
3634 wl_list_insert(d->input_list.prev, &input->link);
3635
Daniel Stone37816df2012-05-16 18:45:18 +01003636 wl_seat_add_listener(input->seat, &seat_listener, input);
3637 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003638
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003639 input->data_device =
3640 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003641 input->seat);
3642 wl_data_device_add_listener(input->data_device, &data_device_listener,
3643 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003644
3645 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003646
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003647 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3648 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003649 input->repeat_task.run = keyboard_repeat_func;
3650 display_watch_fd(d, input->repeat_timer_fd,
3651 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003652}
3653
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003654static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003655input_destroy(struct input *input)
3656{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003657 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003658 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003659
3660 if (input->drag_offer)
3661 data_offer_destroy(input->drag_offer);
3662
3663 if (input->selection_offer)
3664 data_offer_destroy(input->selection_offer);
3665
3666 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003667 fini_xkb(input);
3668
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003669 wl_surface_destroy(input->pointer_surface);
3670
Pekka Paalanene1207c72011-12-16 12:02:09 +02003671 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003672 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003673 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003674 free(input);
3675}
3676
3677static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003678init_workspace_manager(struct display *d, uint32_t id)
3679{
3680 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003681 wl_registry_bind(d->registry, id,
3682 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003683 if (d->workspace_manager != NULL)
3684 workspace_manager_add_listener(d->workspace_manager,
3685 &workspace_manager_listener,
3686 d);
3687}
3688
3689static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003690registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3691 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003692{
3693 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003694 struct global *global;
3695
3696 global = malloc(sizeof *global);
3697 global->name = id;
3698 global->interface = strdup(interface);
3699 global->version = version;
3700 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003701
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003702 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003703 d->compositor = wl_registry_bind(registry, id,
3704 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003705 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003706 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003707 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003708 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003709 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003710 d->shell = wl_registry_bind(registry,
3711 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003712 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003713 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003714 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3715 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003716 wl_registry_bind(registry, id,
3717 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003718 } else if (strcmp(interface, "text_cursor_position") == 0) {
3719 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003720 wl_registry_bind(registry, id,
3721 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003722 } else if (strcmp(interface, "workspace_manager") == 0) {
3723 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003724 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003725
3726 if (d->global_handler)
3727 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003728}
3729
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003730void *
3731display_bind(struct display *display, uint32_t name,
3732 const struct wl_interface *interface, uint32_t version)
3733{
3734 return wl_registry_bind(display->registry, name, interface, version);
3735}
3736
3737static const struct wl_registry_listener registry_listener = {
3738 registry_handle_global
3739};
3740
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003741#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003742static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003743init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003744{
3745 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003746 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003747
Rob Clark6396ed32012-03-11 19:48:41 -05003748#ifdef USE_CAIRO_GLESV2
3749# define GL_BIT EGL_OPENGL_ES2_BIT
3750#else
3751# define GL_BIT EGL_OPENGL_BIT
3752#endif
3753
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003754 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003755 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003756 EGL_RED_SIZE, 1,
3757 EGL_GREEN_SIZE, 1,
3758 EGL_BLUE_SIZE, 1,
3759 EGL_ALPHA_SIZE, 1,
3760 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003761 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003762 EGL_NONE
3763 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003764
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003765#ifdef USE_CAIRO_GLESV2
3766 static const EGLint context_attribs[] = {
3767 EGL_CONTEXT_CLIENT_VERSION, 2,
3768 EGL_NONE
3769 };
3770 EGLint api = EGL_OPENGL_ES_API;
3771#else
3772 EGLint *context_attribs = NULL;
3773 EGLint api = EGL_OPENGL_API;
3774#endif
3775
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003776 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003777 if (!eglInitialize(d->dpy, &major, &minor)) {
3778 fprintf(stderr, "failed to initialize display\n");
3779 return -1;
3780 }
3781
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003782 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003783 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3784 return -1;
3785 }
3786
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003787 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3788 &d->argb_config, 1, &n) || n != 1) {
3789 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003790 return -1;
3791 }
3792
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003793 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003794 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003795 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003796 fprintf(stderr, "failed to create context\n");
3797 return -1;
3798 }
3799
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003800 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003801 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003802 return -1;
3803 }
3804
Benjamin Franzke0c991632011-09-27 21:57:31 +02003805 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3806 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3807 fprintf(stderr, "failed to get cairo egl argb device\n");
3808 return -1;
3809 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003810
3811 return 0;
3812}
3813
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003814static void
3815fini_egl(struct display *display)
3816{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003817 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003818
3819 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3820 EGL_NO_CONTEXT);
3821
3822 eglTerminate(display->dpy);
3823 eglReleaseThread();
3824}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003825#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003826
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003827static void
3828handle_display_data(struct task *task, uint32_t events)
3829{
3830 struct display *display =
3831 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003832 struct epoll_event ep;
3833 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003834
3835 display->display_fd_events = events;
3836
3837 if (events & EPOLLERR || events & EPOLLHUP) {
3838 display_exit(display);
3839 return;
3840 }
3841
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003842 if (events & EPOLLIN) {
3843 ret = wl_display_dispatch(display->display);
3844 if (ret == -1) {
3845 display_exit(display);
3846 return;
3847 }
3848 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003849
3850 if (events & EPOLLOUT) {
3851 ret = wl_display_flush(display->display);
3852 if (ret == 0) {
3853 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3854 ep.data.ptr = &display->display_task;
3855 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3856 display->display_fd, &ep);
3857 } else if (ret == -1 && errno != EAGAIN) {
3858 display_exit(display);
3859 return;
3860 }
3861 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003862}
3863
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003864struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003865display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003866{
3867 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003868
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003869 d = malloc(sizeof *d);
3870 if (d == NULL)
3871 return NULL;
3872
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003873 memset(d, 0, sizeof *d);
3874
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003875 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003876 if (d->display == NULL) {
3877 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003878 return NULL;
3879 }
3880
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003881 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003882 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003883 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003884 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3885 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003886
3887 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003888 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003889 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003890 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003891
Daniel Stone97f68542012-05-30 16:32:01 +01003892 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003893 if (d->xkb_context == NULL) {
3894 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003895 return NULL;
3896 }
3897
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003898 d->workspace = 0;
3899 d->workspace_count = 1;
3900
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003901 d->registry = wl_display_get_registry(d->display);
3902 wl_registry_add_listener(d->registry, &registry_listener, d);
3903 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003904#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003905 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003906 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003907#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003908
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003909 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003910
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003911 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003912
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003913 wl_list_init(&d->window_list);
3914
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003915 return d;
3916}
3917
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003918static void
3919display_destroy_outputs(struct display *display)
3920{
3921 struct output *tmp;
3922 struct output *output;
3923
3924 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3925 output_destroy(output);
3926}
3927
Pekka Paalanene1207c72011-12-16 12:02:09 +02003928static void
3929display_destroy_inputs(struct display *display)
3930{
3931 struct input *tmp;
3932 struct input *input;
3933
3934 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3935 input_destroy(input);
3936}
3937
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003938void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003939display_destroy(struct display *display)
3940{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003941 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003942 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3943 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003944
3945 if (!wl_list_empty(&display->deferred_list))
3946 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3947
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003948 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003949 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003950
Daniel Stone97f68542012-05-30 16:32:01 +01003951 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003952
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003953 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003954 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003955
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003956#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003957 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003958#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003959
Pekka Paalanenc2052982011-12-16 11:41:32 +02003960 if (display->shell)
3961 wl_shell_destroy(display->shell);
3962
3963 if (display->shm)
3964 wl_shm_destroy(display->shm);
3965
3966 if (display->data_device_manager)
3967 wl_data_device_manager_destroy(display->data_device_manager);
3968
3969 wl_compositor_destroy(display->compositor);
3970
3971 close(display->epoll_fd);
3972
U. Artie Eoff44874d92012-10-02 21:12:35 -07003973 if (!(display->display_fd_events & EPOLLERR) &&
3974 !(display->display_fd_events & EPOLLHUP))
3975 wl_display_flush(display->display);
3976
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003977 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003978 free(display);
3979}
3980
3981void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003982display_set_user_data(struct display *display, void *data)
3983{
3984 display->user_data = data;
3985}
3986
3987void *
3988display_get_user_data(struct display *display)
3989{
3990 return display->user_data;
3991}
3992
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003993struct wl_display *
3994display_get_display(struct display *display)
3995{
3996 return display->display;
3997}
3998
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003999struct output *
4000display_get_output(struct display *display)
4001{
4002 return container_of(display->output_list.next, struct output, link);
4003}
4004
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004005struct wl_compositor *
4006display_get_compositor(struct display *display)
4007{
4008 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004009}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004010
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004011uint32_t
4012display_get_serial(struct display *display)
4013{
4014 return display->serial;
4015}
4016
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004017EGLDisplay
4018display_get_egl_display(struct display *d)
4019{
4020 return d->dpy;
4021}
4022
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004023struct wl_data_source *
4024display_create_data_source(struct display *display)
4025{
4026 return wl_data_device_manager_create_data_source(display->data_device_manager);
4027}
4028
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004029EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004030display_get_argb_egl_config(struct display *d)
4031{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004032 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004033}
4034
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004035struct wl_shell *
4036display_get_shell(struct display *display)
4037{
4038 return display->shell;
4039}
4040
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004041int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004042display_acquire_window_surface(struct display *display,
4043 struct window *window,
4044 EGLContext ctx)
4045{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004046#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004047 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004048 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004049
4050 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004051 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004052 device = cairo_surface_get_device(window->cairo_surface);
4053 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004054 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004055
Benjamin Franzke0c991632011-09-27 21:57:31 +02004056 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004057 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004058 ctx = display->argb_ctx;
4059 else
4060 assert(0);
4061 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004062
4063 data = cairo_surface_get_user_data(window->cairo_surface,
4064 &surface_data_key);
4065
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004066 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004067 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004068 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4069 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004070
4071 return 0;
4072#else
4073 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004074#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004075}
4076
4077void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004078display_release_window_surface(struct display *display,
4079 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004080{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004081#ifdef HAVE_CAIRO_EGL
4082 cairo_device_t *device;
4083
4084 device = cairo_surface_get_device(window->cairo_surface);
4085 if (!device)
4086 return;
4087
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004088 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004089 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004090 cairo_device_release(device);
4091#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004092}
4093
4094void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004095display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004096{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004097 wl_list_insert(&display->deferred_list, &task->link);
4098}
4099
4100void
4101display_watch_fd(struct display *display,
4102 int fd, uint32_t events, struct task *task)
4103{
4104 struct epoll_event ep;
4105
4106 ep.events = events;
4107 ep.data.ptr = task;
4108 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4109}
4110
4111void
4112display_run(struct display *display)
4113{
4114 struct task *task;
4115 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004116 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004117
Pekka Paalanen826d7952011-12-15 10:14:07 +02004118 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004119 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004120 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004121 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004122 struct task, link);
4123 wl_list_remove(&task->link);
4124 task->run(task, 0);
4125 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004126
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004127 wl_display_dispatch_pending(display->display);
4128
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004129 if (!display->running)
4130 break;
4131
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004132 ret = wl_display_flush(display->display);
4133 if (ret < 0 && errno == EAGAIN) {
4134 ep[0].events =
4135 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4136 ep[0].data.ptr = &display->display_task;
4137
4138 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4139 display->display_fd, &ep[0]);
4140 } else if (ret < 0) {
4141 break;
4142 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004143
4144 count = epoll_wait(display->epoll_fd,
4145 ep, ARRAY_LENGTH(ep), -1);
4146 for (i = 0; i < count; i++) {
4147 task = ep[i].data.ptr;
4148 task->run(task, ep[i].events);
4149 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004150 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004151}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004152
4153void
4154display_exit(struct display *display)
4155{
4156 display->running = 0;
4157}