blob: c0296917bd4d14eaaee37e2b62eff62cb1214e28 [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øgsberg5a4e9ff2012-06-04 16:04:07 -04001848 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001849 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001850 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001851 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001852 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001853 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001854 case THEME_LOCATION_RESIZING_TOP:
1855 case THEME_LOCATION_RESIZING_BOTTOM:
1856 case THEME_LOCATION_RESIZING_LEFT:
1857 case THEME_LOCATION_RESIZING_RIGHT:
1858 case THEME_LOCATION_RESIZING_TOP_LEFT:
1859 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1860 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1861 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001862 if (!window->shell_surface)
1863 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001865
1866 if (!display->dpy) {
1867 /* If we're using shm, allocate a big
1868 pool to create buffers out of while
1869 we resize. We should probably base
1870 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001871 window->pool =
1872 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001873 }
1874
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001875 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001876 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001878 break;
1879 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001880 } else if (button == BTN_RIGHT &&
1881 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001882 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001883 }
1884}
1885
1886struct widget *
1887frame_create(struct window *window, void *data)
1888{
1889 struct frame *frame;
1890
1891 frame = malloc(sizeof *frame);
1892 memset(frame, 0, sizeof *frame);
1893
1894 frame->widget = window_add_widget(window, frame);
1895 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001896
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001897 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1898 widget_set_resize_handler(frame->widget, frame_resize_handler);
1899 widget_set_enter_handler(frame->widget, frame_enter_handler);
1900 widget_set_motion_handler(frame->widget, frame_motion_handler);
1901 widget_set_button_handler(frame->widget, frame_button_handler);
1902
Martin Minarik1998b152012-05-10 02:04:35 +02001903 /* Create empty list for frame buttons */
1904 wl_list_init(&frame->buttons_list);
1905
1906 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1907 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1908
1909 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1910 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1911
1912 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1913 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1914
1915 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1916 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1917
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001918 window->frame = frame;
1919
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001920 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001921}
1922
Kristian Høgsberga1627922012-06-20 17:30:03 -04001923void
1924frame_set_child_size(struct widget *widget, int child_width, int child_height)
1925{
1926 struct display *display = widget->window->display;
1927 struct theme *t = display->theme;
1928 int decoration_width, decoration_height;
1929 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001930 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001931
1932 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001933 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001934 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001935 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001936
1937 width = child_width + decoration_width;
1938 height = child_height + decoration_height;
1939 } else {
1940 width = child_width;
1941 height = child_height;
1942 }
1943
1944 window_schedule_resize(widget->window, width, height);
1945}
1946
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001947static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001948frame_destroy(struct frame *frame)
1949{
Martin Minarik1998b152012-05-10 02:04:35 +02001950 struct frame_button *button, *tmp;
1951
1952 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1953 frame_button_destroy(button);
1954
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001955 /* frame->child must be destroyed by the application */
1956 widget_destroy(frame->widget);
1957 free(frame);
1958}
1959
1960static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001961input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001962 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001963{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001964 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001965 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001966
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001967 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001968 return;
1969
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001970 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001971 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001972 widget = old;
1973 if (input->grab)
1974 widget = input->grab;
1975 if (widget->leave_handler)
1976 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001977 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001978 }
1979
1980 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001981 widget = focus;
1982 if (input->grab)
1983 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001984 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001985 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001986 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001987 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001988
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001989 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001990 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001991}
1992
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001993void
1994input_grab(struct input *input, struct widget *widget, uint32_t button)
1995{
1996 input->grab = widget;
1997 input->grab_button = button;
1998}
1999
2000void
2001input_ungrab(struct input *input)
2002{
2003 struct widget *widget;
2004
2005 input->grab = NULL;
2006 if (input->pointer_focus) {
2007 widget = widget_find_widget(input->pointer_focus->widget,
2008 input->sx, input->sy);
2009 input_set_focus_widget(input, widget, input->sx, input->sy);
2010 }
2011}
2012
2013static void
2014input_remove_pointer_focus(struct input *input)
2015{
2016 struct window *window = input->pointer_focus;
2017
2018 if (!window)
2019 return;
2020
2021 input_set_focus_widget(input, NULL, 0, 0);
2022
2023 input->pointer_focus = NULL;
2024 input->current_cursor = CURSOR_UNSET;
2025}
2026
2027static void
2028pointer_handle_enter(void *data, struct wl_pointer *pointer,
2029 uint32_t serial, struct wl_surface *surface,
2030 wl_fixed_t sx_w, wl_fixed_t sy_w)
2031{
2032 struct input *input = data;
2033 struct window *window;
2034 struct widget *widget;
2035 float sx = wl_fixed_to_double(sx_w);
2036 float sy = wl_fixed_to_double(sy_w);
2037
2038 if (!surface) {
2039 /* enter event for a window we've just destroyed */
2040 return;
2041 }
2042
2043 input->display->serial = serial;
2044 input->pointer_enter_serial = serial;
2045 input->pointer_focus = wl_surface_get_user_data(surface);
2046 window = input->pointer_focus;
2047
2048 if (window->pool) {
2049 shm_pool_destroy(window->pool);
2050 window->pool = NULL;
2051 /* Schedule a redraw to free the pool */
2052 window_schedule_redraw(window);
2053 }
2054
2055 input->sx = sx;
2056 input->sy = sy;
2057
2058 widget = widget_find_widget(window->widget, sx, sy);
2059 input_set_focus_widget(input, widget, sx, sy);
2060}
2061
2062static void
2063pointer_handle_leave(void *data, struct wl_pointer *pointer,
2064 uint32_t serial, struct wl_surface *surface)
2065{
2066 struct input *input = data;
2067
2068 input->display->serial = serial;
2069 input_remove_pointer_focus(input);
2070}
2071
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002072static void
Daniel Stone37816df2012-05-16 18:45:18 +01002073pointer_handle_motion(void *data, struct wl_pointer *pointer,
2074 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002075{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002076 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002077 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002078 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002079 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002080 float sx = wl_fixed_to_double(sx_w);
2081 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002083 input->sx = sx;
2084 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002085
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002086 if (!window)
2087 return;
2088
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002089 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002090 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002091 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002092 }
2093
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 if (input->grab)
2095 widget = input->grab;
2096 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002097 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002098 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002099 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002100 input, time, sx, sy,
2101 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002102
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002103 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002104}
2105
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002106static void
Daniel Stone37816df2012-05-16 18:45:18 +01002107pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002108 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002109{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002110 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002111 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002112 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002113
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002115 if (input->focus_widget && input->grab == NULL &&
2116 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002117 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002118
Neil Roberts6b28aad2012-01-23 19:11:18 +00002119 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002121 (*widget->button_handler)(widget,
2122 input, time,
2123 button, state,
2124 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002125
Daniel Stone4dbadb12012-05-30 16:31:51 +01002126 if (input->grab && input->grab_button == button &&
2127 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002129}
2130
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002131static void
Daniel Stone37816df2012-05-16 18:45:18 +01002132pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002133 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002134{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002135 struct input *input = data;
2136 struct widget *widget;
2137
2138 widget = input->focus_widget;
2139 if (input->grab)
2140 widget = input->grab;
2141 if (widget && widget->axis_handler)
2142 (*widget->axis_handler)(widget,
2143 input, time,
2144 axis, value,
2145 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002146}
2147
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002148static const struct wl_pointer_listener pointer_listener = {
2149 pointer_handle_enter,
2150 pointer_handle_leave,
2151 pointer_handle_motion,
2152 pointer_handle_button,
2153 pointer_handle_axis,
2154};
2155
2156static void
2157input_remove_keyboard_focus(struct input *input)
2158{
2159 struct window *window = input->keyboard_focus;
2160 struct itimerspec its;
2161
2162 its.it_interval.tv_sec = 0;
2163 its.it_interval.tv_nsec = 0;
2164 its.it_value.tv_sec = 0;
2165 its.it_value.tv_nsec = 0;
2166 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2167
2168 if (!window)
2169 return;
2170
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002171 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002172 if (window->keyboard_focus_handler)
2173 (*window->keyboard_focus_handler)(window, NULL,
2174 window->user_data);
2175
2176 input->keyboard_focus = NULL;
2177}
2178
2179static void
2180keyboard_repeat_func(struct task *task, uint32_t events)
2181{
2182 struct input *input =
2183 container_of(task, struct input, repeat_task);
2184 struct window *window = input->keyboard_focus;
2185 uint64_t exp;
2186
2187 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2188 /* If we change the timer between the fd becoming
2189 * readable and getting here, there'll be nothing to
2190 * read and we get EAGAIN. */
2191 return;
2192
2193 if (window && window->key_handler) {
2194 (*window->key_handler)(window, input, input->repeat_time,
2195 input->repeat_key, input->repeat_sym,
2196 WL_KEYBOARD_KEY_STATE_PRESSED,
2197 window->user_data);
2198 }
2199}
2200
2201static void
2202keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2203 uint32_t format, int fd, uint32_t size)
2204{
2205 struct input *input = data;
2206 char *map_str;
2207
2208 if (!data) {
2209 close(fd);
2210 return;
2211 }
2212
2213 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2214 close(fd);
2215 return;
2216 }
2217
2218 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2219 if (map_str == MAP_FAILED) {
2220 close(fd);
2221 return;
2222 }
2223
2224 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2225 map_str,
2226 XKB_KEYMAP_FORMAT_TEXT_V1,
2227 0);
2228 munmap(map_str, size);
2229 close(fd);
2230
2231 if (!input->xkb.keymap) {
2232 fprintf(stderr, "failed to compile keymap\n");
2233 return;
2234 }
2235
2236 input->xkb.state = xkb_state_new(input->xkb.keymap);
2237 if (!input->xkb.state) {
2238 fprintf(stderr, "failed to create XKB state\n");
2239 xkb_map_unref(input->xkb.keymap);
2240 input->xkb.keymap = NULL;
2241 return;
2242 }
2243
2244 input->xkb.control_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2246 input->xkb.alt_mask =
2247 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2248 input->xkb.shift_mask =
2249 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2250}
2251
2252static void
2253keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2254 uint32_t serial, struct wl_surface *surface,
2255 struct wl_array *keys)
2256{
2257 struct input *input = data;
2258 struct window *window;
2259
2260 input->display->serial = serial;
2261 input->keyboard_focus = wl_surface_get_user_data(surface);
2262
2263 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002264 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002265 if (window->keyboard_focus_handler)
2266 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002267 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002268}
2269
2270static void
2271keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2272 uint32_t serial, struct wl_surface *surface)
2273{
2274 struct input *input = data;
2275
2276 input->display->serial = serial;
2277 input_remove_keyboard_focus(input);
2278}
2279
Scott Moreau210d0792012-03-22 10:47:01 -06002280static void
Daniel Stone37816df2012-05-16 18:45:18 +01002281keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002282 uint32_t serial, uint32_t time, uint32_t key,
2283 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002284{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002285 struct input *input = data;
2286 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002287 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002288 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002289 const xkb_keysym_t *syms;
2290 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002291 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002292
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002294 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002295 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002296 return;
2297
Daniel Stone97f68542012-05-30 16:32:01 +01002298 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002299
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002300 sym = XKB_KEY_NoSymbol;
2301 if (num_syms == 1)
2302 sym = syms[0];
2303
2304 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002305 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002306 window_set_maximized(window,
2307 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002308 } else if (sym == XKB_KEY_F11 &&
2309 window->fullscreen_handler &&
2310 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2311 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002312 } else if (sym == XKB_KEY_F4 &&
2313 input->modifiers == MOD_ALT_MASK &&
2314 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2315 if (window->close_handler)
2316 window->close_handler(window->parent,
2317 window->user_data);
2318 else
2319 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002320 } else if (window->key_handler) {
2321 (*window->key_handler)(window, input, time, key,
2322 sym, state, window->user_data);
2323 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002324
2325 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2326 key == input->repeat_key) {
2327 its.it_interval.tv_sec = 0;
2328 its.it_interval.tv_nsec = 0;
2329 its.it_value.tv_sec = 0;
2330 its.it_value.tv_nsec = 0;
2331 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2332 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2333 input->repeat_sym = sym;
2334 input->repeat_key = key;
2335 input->repeat_time = time;
2336 its.it_interval.tv_sec = 0;
2337 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2338 its.it_value.tv_sec = 0;
2339 its.it_value.tv_nsec = 400 * 1000 * 1000;
2340 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2341 }
2342}
2343
2344static void
Daniel Stone351eb612012-05-31 15:27:47 -04002345keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2346 uint32_t serial, uint32_t mods_depressed,
2347 uint32_t mods_latched, uint32_t mods_locked,
2348 uint32_t group)
2349{
2350 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002351 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002352
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002353 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2354 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002355 mask = xkb_state_serialize_mods(input->xkb.state,
2356 XKB_STATE_DEPRESSED |
2357 XKB_STATE_LATCHED);
2358 input->modifiers = 0;
2359 if (mask & input->xkb.control_mask)
2360 input->modifiers |= MOD_CONTROL_MASK;
2361 if (mask & input->xkb.alt_mask)
2362 input->modifiers |= MOD_ALT_MASK;
2363 if (mask & input->xkb.shift_mask)
2364 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002365}
2366
Daniel Stone37816df2012-05-16 18:45:18 +01002367static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002368 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002369 keyboard_handle_enter,
2370 keyboard_handle_leave,
2371 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002372 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002373};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002374
2375static void
Daniel Stone37816df2012-05-16 18:45:18 +01002376seat_handle_capabilities(void *data, struct wl_seat *seat,
2377 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002378{
Daniel Stone37816df2012-05-16 18:45:18 +01002379 struct input *input = data;
2380
2381 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2382 input->pointer = wl_seat_get_pointer(seat);
2383 wl_pointer_set_user_data(input->pointer, input);
2384 wl_pointer_add_listener(input->pointer, &pointer_listener,
2385 input);
2386 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2387 wl_pointer_destroy(input->pointer);
2388 input->pointer = NULL;
2389 }
2390
2391 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2392 input->keyboard = wl_seat_get_keyboard(seat);
2393 wl_keyboard_set_user_data(input->keyboard, input);
2394 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2395 input);
2396 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2397 wl_keyboard_destroy(input->keyboard);
2398 input->keyboard = NULL;
2399 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002400}
2401
Daniel Stone37816df2012-05-16 18:45:18 +01002402static const struct wl_seat_listener seat_listener = {
2403 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002404};
2405
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002406void
2407input_get_position(struct input *input, int32_t *x, int32_t *y)
2408{
2409 *x = input->sx;
2410 *y = input->sy;
2411}
2412
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002413struct display *
2414input_get_display(struct input *input)
2415{
2416 return input->display;
2417}
2418
Daniel Stone37816df2012-05-16 18:45:18 +01002419struct wl_seat *
2420input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002421{
Daniel Stone37816df2012-05-16 18:45:18 +01002422 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002423}
2424
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002425uint32_t
2426input_get_modifiers(struct input *input)
2427{
2428 return input->modifiers;
2429}
2430
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002431struct widget *
2432input_get_focus_widget(struct input *input)
2433{
2434 return input->focus_widget;
2435}
2436
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002437struct data_offer {
2438 struct wl_data_offer *offer;
2439 struct input *input;
2440 struct wl_array types;
2441 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002442
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002443 struct task io_task;
2444 int fd;
2445 data_func_t func;
2446 int32_t x, y;
2447 void *user_data;
2448};
2449
2450static void
2451data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2452{
2453 struct data_offer *offer = data;
2454 char **p;
2455
2456 p = wl_array_add(&offer->types, sizeof *p);
2457 *p = strdup(type);
2458}
2459
2460static const struct wl_data_offer_listener data_offer_listener = {
2461 data_offer_offer,
2462};
2463
2464static void
2465data_offer_destroy(struct data_offer *offer)
2466{
2467 char **p;
2468
2469 offer->refcount--;
2470 if (offer->refcount == 0) {
2471 wl_data_offer_destroy(offer->offer);
2472 for (p = offer->types.data; *p; p++)
2473 free(*p);
2474 wl_array_release(&offer->types);
2475 free(offer);
2476 }
2477}
2478
2479static void
2480data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002481 struct wl_data_device *data_device,
2482 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002483{
2484 struct data_offer *offer;
2485
2486 offer = malloc(sizeof *offer);
2487
2488 wl_array_init(&offer->types);
2489 offer->refcount = 1;
2490 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002491 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002492 wl_data_offer_add_listener(offer->offer,
2493 &data_offer_listener, offer);
2494}
2495
2496static void
2497data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002498 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002499 wl_fixed_t x_w, wl_fixed_t y_w,
2500 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002501{
2502 struct input *input = data;
2503 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002504 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002505 float x = wl_fixed_to_double(x_w);
2506 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002507 char **p;
2508
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002509 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002510 window = wl_surface_get_user_data(surface);
2511 input->pointer_focus = window;
2512
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002513 if (offer) {
2514 input->drag_offer = wl_data_offer_get_user_data(offer);
2515
2516 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2517 *p = NULL;
2518
2519 types_data = input->drag_offer->types.data;
2520 } else {
2521 input->drag_offer = NULL;
2522 types_data = NULL;
2523 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002524
2525 window = input->pointer_focus;
2526 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002527 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002528 window->user_data);
2529}
2530
2531static void
2532data_device_leave(void *data, struct wl_data_device *data_device)
2533{
2534 struct input *input = data;
2535
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002536 if (input->drag_offer) {
2537 data_offer_destroy(input->drag_offer);
2538 input->drag_offer = NULL;
2539 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002540}
2541
2542static void
2543data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002544 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002545{
2546 struct input *input = data;
2547 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002548 float x = wl_fixed_to_double(x_w);
2549 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002550 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002551
2552 input->sx = x;
2553 input->sy = y;
2554
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002555 if (input->drag_offer)
2556 types_data = input->drag_offer->types.data;
2557 else
2558 types_data = NULL;
2559
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002560 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002561 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002562 window->user_data);
2563}
2564
2565static void
2566data_device_drop(void *data, struct wl_data_device *data_device)
2567{
2568 struct input *input = data;
2569 struct window *window = input->pointer_focus;
2570
2571 if (window->drop_handler)
2572 window->drop_handler(window, input,
2573 input->sx, input->sy, window->user_data);
2574}
2575
2576static void
2577data_device_selection(void *data,
2578 struct wl_data_device *wl_data_device,
2579 struct wl_data_offer *offer)
2580{
2581 struct input *input = data;
2582 char **p;
2583
2584 if (input->selection_offer)
2585 data_offer_destroy(input->selection_offer);
2586
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002587 if (offer) {
2588 input->selection_offer = wl_data_offer_get_user_data(offer);
2589 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2590 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002591 } else {
2592 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002593 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002594}
2595
2596static const struct wl_data_device_listener data_device_listener = {
2597 data_device_data_offer,
2598 data_device_enter,
2599 data_device_leave,
2600 data_device_motion,
2601 data_device_drop,
2602 data_device_selection
2603};
2604
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002605static void
2606input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002608 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002609 struct wl_cursor *cursor;
2610 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002611
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002612 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002613 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002614 return;
2615
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002616 if (index >= (int) cursor->image_count) {
2617 fprintf(stderr, "cursor index out of range\n");
2618 return;
2619 }
2620
2621 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002622 buffer = wl_cursor_image_get_buffer(image);
2623 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002624 return;
2625
Kristian Høgsbergae277372012-08-01 09:41:08 -04002626 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002627 input->pointer_surface,
2628 image->hotspot_x, image->hotspot_y);
2629 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2630 wl_surface_damage(input->pointer_surface, 0, 0,
2631 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002632 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002633}
2634
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002635static const struct wl_callback_listener pointer_surface_listener;
2636
2637static void
2638pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2639 uint32_t time)
2640{
2641 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002642 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002643 int i;
2644
2645 if (callback) {
2646 assert(callback == input->cursor_frame_cb);
2647 wl_callback_destroy(callback);
2648 input->cursor_frame_cb = NULL;
2649 }
2650
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002651 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002652 wl_pointer_set_cursor(input->pointer,
2653 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002654 NULL, 0, 0);
2655 return;
2656 }
2657
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002658 if (input->current_cursor == CURSOR_UNSET)
2659 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002660 cursor = input->display->cursors[input->current_cursor];
2661 if (!cursor)
2662 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002663
2664 /* FIXME We don't have the current time on the first call so we set
2665 * the animation start to the time of the first frame callback. */
2666 if (time == 0)
2667 input->cursor_anim_start = 0;
2668 else if (input->cursor_anim_start == 0)
2669 input->cursor_anim_start = time;
2670
2671 if (time == 0 || input->cursor_anim_start == 0)
2672 i = 0;
2673 else
2674 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2675
Pekka Paalanenbc106382012-10-10 12:49:31 +03002676 if (cursor->image_count > 1) {
2677 input->cursor_frame_cb =
2678 wl_surface_frame(input->pointer_surface);
2679 wl_callback_add_listener(input->cursor_frame_cb,
2680 &pointer_surface_listener, input);
2681 }
2682
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002683 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002684}
2685
2686static const struct wl_callback_listener pointer_surface_listener = {
2687 pointer_surface_frame_callback
2688};
2689
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002690void
2691input_set_pointer_image(struct input *input, int pointer)
2692{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002693 int force = 0;
2694
2695 if (input->pointer_enter_serial > input->cursor_serial)
2696 force = 1;
2697
2698 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002699 return;
2700
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002701 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002702 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002703 if (!input->cursor_frame_cb)
2704 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002705 else if (force) {
2706 /* The current frame callback may be stuck if, for instance,
2707 * the set cursor request was processed by the server after
2708 * this client lost the focus. In this case the cursor surface
2709 * might not be mapped and the frame callback wouldn't ever
2710 * complete. Send a set_cursor and attach to try to map the
2711 * cursor surface again so that the callback will finish */
2712 input_set_pointer_image_index(input, 0);
2713 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002714}
2715
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002716struct wl_data_device *
2717input_get_data_device(struct input *input)
2718{
2719 return input->data_device;
2720}
2721
2722void
2723input_set_selection(struct input *input,
2724 struct wl_data_source *source, uint32_t time)
2725{
2726 wl_data_device_set_selection(input->data_device, source, time);
2727}
2728
2729void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002730input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002731{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002732 wl_data_offer_accept(input->drag_offer->offer,
2733 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002734}
2735
2736static void
2737offer_io_func(struct task *task, uint32_t events)
2738{
2739 struct data_offer *offer =
2740 container_of(task, struct data_offer, io_task);
2741 unsigned int len;
2742 char buffer[4096];
2743
2744 len = read(offer->fd, buffer, sizeof buffer);
2745 offer->func(buffer, len,
2746 offer->x, offer->y, offer->user_data);
2747
2748 if (len == 0) {
2749 close(offer->fd);
2750 data_offer_destroy(offer);
2751 }
2752}
2753
2754static void
2755data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2756 data_func_t func, void *user_data)
2757{
2758 int p[2];
2759
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002760 if (pipe2(p, O_CLOEXEC) == -1)
2761 return;
2762
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002763 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2764 close(p[1]);
2765
2766 offer->io_task.run = offer_io_func;
2767 offer->fd = p[0];
2768 offer->func = func;
2769 offer->refcount++;
2770 offer->user_data = user_data;
2771
2772 display_watch_fd(offer->input->display,
2773 offer->fd, EPOLLIN, &offer->io_task);
2774}
2775
2776void
2777input_receive_drag_data(struct input *input, const char *mime_type,
2778 data_func_t func, void *data)
2779{
2780 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2781 input->drag_offer->x = input->sx;
2782 input->drag_offer->y = input->sy;
2783}
2784
2785int
2786input_receive_selection_data(struct input *input, const char *mime_type,
2787 data_func_t func, void *data)
2788{
2789 char **p;
2790
2791 if (input->selection_offer == NULL)
2792 return -1;
2793
2794 for (p = input->selection_offer->types.data; *p; p++)
2795 if (strcmp(mime_type, *p) == 0)
2796 break;
2797
2798 if (*p == NULL)
2799 return -1;
2800
2801 data_offer_receive_data(input->selection_offer,
2802 mime_type, func, data);
2803 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002804}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002805
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002806int
2807input_receive_selection_data_to_fd(struct input *input,
2808 const char *mime_type, int fd)
2809{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002810 if (input->selection_offer)
2811 wl_data_offer_receive(input->selection_offer->offer,
2812 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002813
2814 return 0;
2815}
2816
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002817void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002818window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002819{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002820 if (!window->shell_surface)
2821 return;
2822
Daniel Stone37816df2012-05-16 18:45:18 +01002823 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002824}
2825
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002826static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002827idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002828{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002829 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002830
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002831 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002832 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002833 widget_set_allocation(widget,
2834 window->pending_allocation.x,
2835 window->pending_allocation.y,
2836 window->pending_allocation.width,
2837 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002838
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002839 if (window->input_region) {
2840 wl_region_destroy(window->input_region);
2841 window->input_region = NULL;
2842 }
2843
2844 if (window->opaque_region) {
2845 wl_region_destroy(window->opaque_region);
2846 window->opaque_region = NULL;
2847 }
2848
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002849 if (widget->resize_handler)
2850 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002851 widget->allocation.width,
2852 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002853 widget->user_data);
2854
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002855 if (window->allocation.width != widget->allocation.width ||
2856 window->allocation.height != widget->allocation.height) {
2857 window->allocation = widget->allocation;
2858 window_schedule_redraw(window);
2859 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002860}
2861
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002862void
2863window_schedule_resize(struct window *window, int width, int height)
2864{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002865 window->pending_allocation.x = 0;
2866 window->pending_allocation.y = 0;
2867 window->pending_allocation.width = width;
2868 window->pending_allocation.height = height;
2869
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002870 if (window->min_allocation.width == 0)
2871 window->min_allocation = window->pending_allocation;
2872 if (window->pending_allocation.width < window->min_allocation.width)
2873 window->pending_allocation.width = window->min_allocation.width;
2874 if (window->pending_allocation.height < window->min_allocation.height)
2875 window->pending_allocation.height = window->min_allocation.height;
2876
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002877 window->resize_needed = 1;
2878 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002879}
2880
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002881void
2882widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2883{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002884 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002885}
2886
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002887static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002888handle_ping(void *data, struct wl_shell_surface *shell_surface,
2889 uint32_t serial)
2890{
2891 wl_shell_surface_pong(shell_surface, serial);
2892}
2893
2894static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002895handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002897{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002898 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002899
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002900 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002901 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002902}
2903
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002904static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002905menu_destroy(struct menu *menu)
2906{
2907 widget_destroy(menu->widget);
2908 window_destroy(menu->window);
2909 free(menu);
2910}
2911
2912static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002913handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2914{
2915 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002916 struct menu *menu = window->widget->user_data;
2917
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002918 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002919 * device. Or just use wl_callback. And this really needs to
2920 * be a window vfunc that the menu can set. And we need the
2921 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002922
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002923 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002924 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002925 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926}
2927
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002928static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002929 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002930 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002931 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002932};
2933
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002934void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002935window_get_allocation(struct window *window,
2936 struct rectangle *allocation)
2937{
2938 *allocation = window->allocation;
2939}
2940
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002941static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002942widget_redraw(struct widget *widget)
2943{
2944 struct widget *child;
2945
2946 if (widget->redraw_handler)
2947 widget->redraw_handler(widget, widget->user_data);
2948 wl_list_for_each(child, &widget->child_list, link)
2949 widget_redraw(child);
2950}
2951
2952static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002953frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2954{
2955 struct window *window = data;
2956
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002957 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002958 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002959 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002960 window->redraw_scheduled = 0;
2961 if (window->redraw_needed)
2962 window_schedule_redraw(window);
2963}
2964
2965static const struct wl_callback_listener listener = {
2966 frame_callback
2967};
2968
2969static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002970idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002971{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002972 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002973
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002974 if (window->resize_needed)
2975 idle_resize(window);
2976
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002977 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002978 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002979 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002980 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002981
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002982 window->frame_cb = wl_surface_frame(window->surface);
2983 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002984 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002985}
2986
2987void
2988window_schedule_redraw(struct window *window)
2989{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002990 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002991 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002992 window->redraw_task.run = idle_redraw;
2993 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002994 window->redraw_scheduled = 1;
2995 }
2996}
2997
Kristian Høgsberg1671e112012-10-10 11:36:24 -04002998int
2999window_is_fullscreen(struct window *window)
3000{
3001 return window->type == TYPE_FULLSCREEN;
3002}
3003
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003004void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003005window_set_fullscreen(struct window *window, int fullscreen)
3006{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003007 if (!window->display->shell)
3008 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003009
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003010 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003011 return;
3012
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003013 if (fullscreen) {
3014 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003015 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003016 wl_shell_surface_set_fullscreen(window->shell_surface,
3017 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3018 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003019 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003020 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003021 wl_shell_surface_set_toplevel(window->shell_surface);
3022 window_schedule_resize(window,
3023 window->saved_allocation.width,
3024 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003025 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003026}
3027
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003028int
3029window_is_maximized(struct window *window)
3030{
3031 return window->type == TYPE_MAXIMIZED;
3032}
3033
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003034void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003035window_set_maximized(struct window *window, int maximized)
3036{
3037 if (!window->display->shell)
3038 return;
3039
3040 if ((window->type == TYPE_MAXIMIZED) == maximized)
3041 return;
3042
3043 if (window->type == TYPE_TOPLEVEL) {
3044 window->saved_allocation = window->allocation;
3045 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3046 window->type = TYPE_MAXIMIZED;
3047 } else {
3048 wl_shell_surface_set_toplevel(window->shell_surface);
3049 window->type = TYPE_TOPLEVEL;
3050 window_schedule_resize(window,
3051 window->saved_allocation.width,
3052 window->saved_allocation.height);
3053 }
3054}
3055
3056void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003057window_set_user_data(struct window *window, void *data)
3058{
3059 window->user_data = data;
3060}
3061
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003062void *
3063window_get_user_data(struct window *window)
3064{
3065 return window->user_data;
3066}
3067
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003068void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003069window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003070 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003071{
3072 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003073}
3074
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003075void
3076window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003077 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003078{
3079 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003080}
3081
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003082void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003083window_set_data_handler(struct window *window, window_data_handler_t handler)
3084{
3085 window->data_handler = handler;
3086}
3087
3088void
3089window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3090{
3091 window->drop_handler = handler;
3092}
3093
3094void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003095window_set_close_handler(struct window *window,
3096 window_close_handler_t handler)
3097{
3098 window->close_handler = handler;
3099}
3100
3101void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003102window_set_fullscreen_handler(struct window *window,
3103 window_fullscreen_handler_t handler)
3104{
3105 window->fullscreen_handler = handler;
3106}
3107
3108void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003109window_set_title(struct window *window, const char *title)
3110{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003111 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003112 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003113 if (window->shell_surface)
3114 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003115}
3116
3117const char *
3118window_get_title(struct window *window)
3119{
3120 return window->title;
3121}
3122
3123void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003124window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3125{
3126 struct text_cursor_position *text_cursor_position =
3127 window->display->text_cursor_position;
3128
Scott Moreau9295ce02012-06-01 12:46:10 -06003129 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003130 return;
3131
3132 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003133 window->surface,
3134 wl_fixed_from_int(x),
3135 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003136}
3137
3138void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003139window_damage(struct window *window, int32_t x, int32_t y,
3140 int32_t width, int32_t height)
3141{
3142 wl_surface_damage(window->surface, x, y, width, height);
3143}
3144
Casey Dahlin9074db52012-04-19 22:50:09 -04003145static void
3146surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003147 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003148{
Rob Bradford7507b572012-05-15 17:55:34 +01003149 struct window *window = data;
3150 struct output *output;
3151 struct output *output_found = NULL;
3152 struct window_output *window_output;
3153
3154 wl_list_for_each(output, &window->display->output_list, link) {
3155 if (output->output == wl_output) {
3156 output_found = output;
3157 break;
3158 }
3159 }
3160
3161 if (!output_found)
3162 return;
3163
3164 window_output = malloc (sizeof *window_output);
3165 window_output->output = output_found;
3166
3167 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003168}
3169
3170static void
3171surface_leave(void *data,
3172 struct wl_surface *wl_surface, struct wl_output *output)
3173{
Rob Bradford7507b572012-05-15 17:55:34 +01003174 struct window *window = data;
3175 struct window_output *window_output;
3176 struct window_output *window_output_found = NULL;
3177
3178 wl_list_for_each(window_output, &window->window_output_list, link) {
3179 if (window_output->output->output == output) {
3180 window_output_found = window_output;
3181 break;
3182 }
3183 }
3184
3185 if (window_output_found) {
3186 wl_list_remove(&window_output_found->link);
3187 free(window_output_found);
3188 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003189}
3190
3191static const struct wl_surface_listener surface_listener = {
3192 surface_enter,
3193 surface_leave
3194};
3195
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003196static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003197window_create_internal(struct display *display,
3198 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003199{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003200 struct window *window;
3201
3202 window = malloc(sizeof *window);
3203 if (window == NULL)
3204 return NULL;
3205
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003206 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003207 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003208 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003209 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003210 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003211 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003212 window->shell_surface =
3213 wl_shell_get_shell_surface(display->shell,
3214 window->surface);
3215 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003216 window->allocation.x = 0;
3217 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003218 window->allocation.width = 0;
3219 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003220 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003221 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003222 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003223 window->input_region = NULL;
3224 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003225
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003226 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003227#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003228 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003229#else
3230 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3231#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003232 else
3233 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003234
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003235 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003236 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003237 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003238
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003239 if (window->shell_surface) {
3240 wl_shell_surface_set_user_data(window->shell_surface, window);
3241 wl_shell_surface_add_listener(window->shell_surface,
3242 &shell_surface_listener, window);
3243 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003244
Rob Bradford7507b572012-05-15 17:55:34 +01003245 wl_list_init (&window->window_output_list);
3246
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003247 return window;
3248}
3249
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003250struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003251window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003252{
3253 struct window *window;
3254
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003255 window = window_create_internal(display, NULL, TYPE_NONE);
3256 if (!window)
3257 return NULL;
3258
3259 return window;
3260}
3261
3262struct window *
3263window_create_custom(struct display *display)
3264{
3265 struct window *window;
3266
3267 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003268 if (!window)
3269 return NULL;
3270
3271 return window;
3272}
3273
3274struct window *
3275window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003276 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003277{
3278 struct window *window;
3279
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003280 window = window_create_internal(parent->display,
3281 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003282 if (!window)
3283 return NULL;
3284
3285 window->x = x;
3286 window->y = y;
3287
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003288 if (display->shell)
3289 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003290 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003291 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003292
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003293 return window;
3294}
3295
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003296static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003297menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003298{
3299 int next;
3300
3301 next = (sy - 8) / 20;
3302 if (menu->current != next) {
3303 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003304 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003306}
3307
3308static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003309menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003310 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003311 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003312{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003313 struct menu *menu = data;
3314
3315 if (widget == menu->widget)
3316 menu_set_item(data, y);
3317
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003318 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319}
3320
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003321static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003322menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003323 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003324{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003325 struct menu *menu = data;
3326
3327 if (widget == menu->widget)
3328 menu_set_item(data, y);
3329
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003330 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003331}
3332
3333static void
3334menu_leave_handler(struct widget *widget, struct input *input, void *data)
3335{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003336 struct menu *menu = data;
3337
3338 if (widget == menu->widget)
3339 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003340}
3341
3342static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003343menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003344 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003345 uint32_t button, enum wl_pointer_button_state state,
3346 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003347
3348{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003349 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003350
Daniel Stone4dbadb12012-05-30 16:31:51 +01003351 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3352 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003353 /* Either relase after press-drag-release or
3354 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003355 menu->func(menu->window->parent,
3356 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003357 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003358 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003359 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003360}
3361
3362static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003363menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003364{
3365 cairo_t *cr;
3366 const int32_t r = 3, margin = 3;
3367 struct menu *menu = data;
3368 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003369 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003370
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003371 cr = cairo_create(window->cairo_surface);
3372 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3373 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3374 cairo_paint(cr);
3375
3376 width = window->allocation.width;
3377 height = window->allocation.height;
3378 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003379
3380 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003381 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3382 cairo_fill(cr);
3383
3384 for (i = 0; i < menu->count; i++) {
3385 if (i == menu->current) {
3386 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3387 cairo_rectangle(cr, margin, i * 20 + margin,
3388 width - 2 * margin, 20);
3389 cairo_fill(cr);
3390 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3391 cairo_move_to(cr, 10, i * 20 + 16);
3392 cairo_show_text(cr, menu->entries[i]);
3393 } else {
3394 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3395 cairo_move_to(cr, 10, i * 20 + 16);
3396 cairo_show_text(cr, menu->entries[i]);
3397 }
3398 }
3399
3400 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003401}
3402
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003403void
3404window_show_menu(struct display *display,
3405 struct input *input, uint32_t time, struct window *parent,
3406 int32_t x, int32_t y,
3407 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003408{
3409 struct window *window;
3410 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003411 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003412
3413 menu = malloc(sizeof *menu);
3414 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003415 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003416
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003417 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003418 if (!window) {
3419 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003420 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003421 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003422
3423 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003424 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003425 menu->entries = entries;
3426 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003427 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003428 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003429 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003430 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003431 window->type = TYPE_MENU;
3432 window->x = x;
3433 window->y = y;
3434
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003435 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003436 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003437 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003438 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003439 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003440
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003441 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003442 widget_set_enter_handler(menu->widget, menu_enter_handler);
3443 widget_set_leave_handler(menu->widget, menu_leave_handler);
3444 widget_set_motion_handler(menu->widget, menu_motion_handler);
3445 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003446
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003447 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003448 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003449}
3450
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003451void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003452window_set_buffer_type(struct window *window, enum window_buffer_type type)
3453{
3454 window->buffer_type = type;
3455}
3456
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003457
3458static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003459display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003460 struct wl_output *wl_output,
3461 int x, int y,
3462 int physical_width,
3463 int physical_height,
3464 int subpixel,
3465 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003466 const char *model,
3467 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003468{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003469 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003470
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003471 output->allocation.x = x;
3472 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003473 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003474}
3475
3476static void
3477display_handle_mode(void *data,
3478 struct wl_output *wl_output,
3479 uint32_t flags,
3480 int width,
3481 int height,
3482 int refresh)
3483{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003484 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003485 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003486
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003487 if (flags & WL_OUTPUT_MODE_CURRENT) {
3488 output->allocation.width = width;
3489 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003490 if (display->output_configure_handler)
3491 (*display->output_configure_handler)(
3492 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003493 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003494}
3495
3496static const struct wl_output_listener output_listener = {
3497 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003498 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003499};
3500
3501static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003502display_add_output(struct display *d, uint32_t id)
3503{
3504 struct output *output;
3505
3506 output = malloc(sizeof *output);
3507 if (output == NULL)
3508 return;
3509
3510 memset(output, 0, sizeof *output);
3511 output->display = d;
3512 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003513 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003514 wl_list_insert(d->output_list.prev, &output->link);
3515
3516 wl_output_add_listener(output->output, &output_listener, output);
3517}
3518
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003519static void
3520output_destroy(struct output *output)
3521{
3522 if (output->destroy_handler)
3523 (*output->destroy_handler)(output, output->user_data);
3524
3525 wl_output_destroy(output->output);
3526 wl_list_remove(&output->link);
3527 free(output);
3528}
3529
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003530void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003531display_set_global_handler(struct display *display,
3532 display_global_handler_t handler)
3533{
3534 struct global *global;
3535
3536 display->global_handler = handler;
3537 if (!handler)
3538 return;
3539
3540 wl_list_for_each(global, &display->global_list, link)
3541 display->global_handler(display,
3542 global->name, global->interface,
3543 global->version, display->user_data);
3544}
3545
3546void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003547display_set_output_configure_handler(struct display *display,
3548 display_output_handler_t handler)
3549{
3550 struct output *output;
3551
3552 display->output_configure_handler = handler;
3553 if (!handler)
3554 return;
3555
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003556 wl_list_for_each(output, &display->output_list, link) {
3557 if (output->allocation.width == 0 &&
3558 output->allocation.height == 0)
3559 continue;
3560
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003561 (*display->output_configure_handler)(output,
3562 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003563 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003564}
3565
3566void
3567output_set_user_data(struct output *output, void *data)
3568{
3569 output->user_data = data;
3570}
3571
3572void *
3573output_get_user_data(struct output *output)
3574{
3575 return output->user_data;
3576}
3577
3578void
3579output_set_destroy_handler(struct output *output,
3580 display_output_handler_t handler)
3581{
3582 output->destroy_handler = handler;
3583 /* FIXME: implement this, once we have way to remove outputs */
3584}
3585
3586void
Scott Moreau4e072362012-09-29 02:03:11 -06003587output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003588{
Scott Moreau4e072362012-09-29 02:03:11 -06003589 struct rectangle allocation = output->allocation;
3590
3591 switch (output->transform) {
3592 case WL_OUTPUT_TRANSFORM_90:
3593 case WL_OUTPUT_TRANSFORM_270:
3594 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3595 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3596 /* Swap width and height */
3597 allocation.width = output->allocation.height;
3598 allocation.height = output->allocation.width;
3599 break;
3600 }
3601
3602 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003603}
3604
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003605struct wl_output *
3606output_get_wl_output(struct output *output)
3607{
3608 return output->output;
3609}
3610
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003611static void
Daniel Stone97f68542012-05-30 16:32:01 +01003612fini_xkb(struct input *input)
3613{
3614 xkb_state_unref(input->xkb.state);
3615 xkb_map_unref(input->xkb.keymap);
3616}
3617
3618static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003619display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003620{
3621 struct input *input;
3622
3623 input = malloc(sizeof *input);
3624 if (input == NULL)
3625 return;
3626
3627 memset(input, 0, sizeof *input);
3628 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003629 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003630 input->pointer_focus = NULL;
3631 input->keyboard_focus = NULL;
3632 wl_list_insert(d->input_list.prev, &input->link);
3633
Daniel Stone37816df2012-05-16 18:45:18 +01003634 wl_seat_add_listener(input->seat, &seat_listener, input);
3635 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003636
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637 input->data_device =
3638 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003639 input->seat);
3640 wl_data_device_add_listener(input->data_device, &data_device_listener,
3641 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003642
3643 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003644
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003645 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3646 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003647 input->repeat_task.run = keyboard_repeat_func;
3648 display_watch_fd(d, input->repeat_timer_fd,
3649 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003650}
3651
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003652static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003653input_destroy(struct input *input)
3654{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003655 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003656 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003657
3658 if (input->drag_offer)
3659 data_offer_destroy(input->drag_offer);
3660
3661 if (input->selection_offer)
3662 data_offer_destroy(input->selection_offer);
3663
3664 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003665 fini_xkb(input);
3666
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003667 wl_surface_destroy(input->pointer_surface);
3668
Pekka Paalanene1207c72011-12-16 12:02:09 +02003669 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003670 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003671 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003672 free(input);
3673}
3674
3675static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003676init_workspace_manager(struct display *d, uint32_t id)
3677{
3678 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003679 wl_registry_bind(d->registry, id,
3680 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003681 if (d->workspace_manager != NULL)
3682 workspace_manager_add_listener(d->workspace_manager,
3683 &workspace_manager_listener,
3684 d);
3685}
3686
3687static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003688registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3689 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003690{
3691 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003692 struct global *global;
3693
3694 global = malloc(sizeof *global);
3695 global->name = id;
3696 global->interface = strdup(interface);
3697 global->version = version;
3698 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003699
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003700 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003701 d->compositor = wl_registry_bind(registry, id,
3702 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003703 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003704 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003705 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003706 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003707 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003708 d->shell = wl_registry_bind(registry,
3709 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003710 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003711 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003712 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3713 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003714 wl_registry_bind(registry, id,
3715 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003716 } else if (strcmp(interface, "text_cursor_position") == 0) {
3717 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003718 wl_registry_bind(registry, id,
3719 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003720 } else if (strcmp(interface, "workspace_manager") == 0) {
3721 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003722 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003723
3724 if (d->global_handler)
3725 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003726}
3727
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003728void *
3729display_bind(struct display *display, uint32_t name,
3730 const struct wl_interface *interface, uint32_t version)
3731{
3732 return wl_registry_bind(display->registry, name, interface, version);
3733}
3734
3735static const struct wl_registry_listener registry_listener = {
3736 registry_handle_global
3737};
3738
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003739#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003740static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003741init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003742{
3743 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003744 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003745
Rob Clark6396ed32012-03-11 19:48:41 -05003746#ifdef USE_CAIRO_GLESV2
3747# define GL_BIT EGL_OPENGL_ES2_BIT
3748#else
3749# define GL_BIT EGL_OPENGL_BIT
3750#endif
3751
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003752 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04003753 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003754 EGL_RED_SIZE, 1,
3755 EGL_GREEN_SIZE, 1,
3756 EGL_BLUE_SIZE, 1,
3757 EGL_ALPHA_SIZE, 1,
3758 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003759 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003760 EGL_NONE
3761 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003762
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003763#ifdef USE_CAIRO_GLESV2
3764 static const EGLint context_attribs[] = {
3765 EGL_CONTEXT_CLIENT_VERSION, 2,
3766 EGL_NONE
3767 };
3768 EGLint api = EGL_OPENGL_ES_API;
3769#else
3770 EGLint *context_attribs = NULL;
3771 EGLint api = EGL_OPENGL_API;
3772#endif
3773
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003774 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003775 if (!eglInitialize(d->dpy, &major, &minor)) {
3776 fprintf(stderr, "failed to initialize display\n");
3777 return -1;
3778 }
3779
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003780 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003781 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3782 return -1;
3783 }
3784
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003785 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3786 &d->argb_config, 1, &n) || n != 1) {
3787 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003788 return -1;
3789 }
3790
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003791 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003792 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003793 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003794 fprintf(stderr, "failed to create context\n");
3795 return -1;
3796 }
3797
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003798 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003799 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003800 return -1;
3801 }
3802
Benjamin Franzke0c991632011-09-27 21:57:31 +02003803 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3804 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3805 fprintf(stderr, "failed to get cairo egl argb device\n");
3806 return -1;
3807 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003808
3809 return 0;
3810}
3811
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003812static void
3813fini_egl(struct display *display)
3814{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003815 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003816
3817 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3818 EGL_NO_CONTEXT);
3819
3820 eglTerminate(display->dpy);
3821 eglReleaseThread();
3822}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003823#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003824
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003825static void
3826handle_display_data(struct task *task, uint32_t events)
3827{
3828 struct display *display =
3829 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003830 struct epoll_event ep;
3831 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003832
3833 display->display_fd_events = events;
3834
3835 if (events & EPOLLERR || events & EPOLLHUP) {
3836 display_exit(display);
3837 return;
3838 }
3839
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04003840 if (events & EPOLLIN) {
3841 ret = wl_display_dispatch(display->display);
3842 if (ret == -1) {
3843 display_exit(display);
3844 return;
3845 }
3846 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003847
3848 if (events & EPOLLOUT) {
3849 ret = wl_display_flush(display->display);
3850 if (ret == 0) {
3851 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3852 ep.data.ptr = &display->display_task;
3853 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3854 display->display_fd, &ep);
3855 } else if (ret == -1 && errno != EAGAIN) {
3856 display_exit(display);
3857 return;
3858 }
3859 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003860}
3861
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003862struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003863display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003864{
3865 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003866
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003867 d = malloc(sizeof *d);
3868 if (d == NULL)
3869 return NULL;
3870
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003871 memset(d, 0, sizeof *d);
3872
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003873 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003874 if (d->display == NULL) {
3875 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003876 return NULL;
3877 }
3878
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003879 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003880 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003881 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003882 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3883 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003884
3885 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003886 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003887 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003888 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003889
Daniel Stone97f68542012-05-30 16:32:01 +01003890 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003891 if (d->xkb_context == NULL) {
3892 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003893 return NULL;
3894 }
3895
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003896 d->workspace = 0;
3897 d->workspace_count = 1;
3898
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003899 d->registry = wl_display_get_registry(d->display);
3900 wl_registry_add_listener(d->registry, &registry_listener, d);
3901 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003902#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003903 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003904 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003905#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003906
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003907 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003908
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003909 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003910
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003911 wl_list_init(&d->window_list);
3912
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003913 return d;
3914}
3915
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003916static void
3917display_destroy_outputs(struct display *display)
3918{
3919 struct output *tmp;
3920 struct output *output;
3921
3922 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3923 output_destroy(output);
3924}
3925
Pekka Paalanene1207c72011-12-16 12:02:09 +02003926static void
3927display_destroy_inputs(struct display *display)
3928{
3929 struct input *tmp;
3930 struct input *input;
3931
3932 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3933 input_destroy(input);
3934}
3935
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003936void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003937display_destroy(struct display *display)
3938{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003939 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003940 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3941 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003942
3943 if (!wl_list_empty(&display->deferred_list))
3944 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3945
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003946 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003947 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003948
Daniel Stone97f68542012-05-30 16:32:01 +01003949 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003950
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003951 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003952 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003953
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003954#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003955 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003956#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003957
Pekka Paalanenc2052982011-12-16 11:41:32 +02003958 if (display->shell)
3959 wl_shell_destroy(display->shell);
3960
3961 if (display->shm)
3962 wl_shm_destroy(display->shm);
3963
3964 if (display->data_device_manager)
3965 wl_data_device_manager_destroy(display->data_device_manager);
3966
3967 wl_compositor_destroy(display->compositor);
3968
3969 close(display->epoll_fd);
3970
U. Artie Eoff44874d92012-10-02 21:12:35 -07003971 if (!(display->display_fd_events & EPOLLERR) &&
3972 !(display->display_fd_events & EPOLLHUP))
3973 wl_display_flush(display->display);
3974
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003975 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003976 free(display);
3977}
3978
3979void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003980display_set_user_data(struct display *display, void *data)
3981{
3982 display->user_data = data;
3983}
3984
3985void *
3986display_get_user_data(struct display *display)
3987{
3988 return display->user_data;
3989}
3990
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003991struct wl_display *
3992display_get_display(struct display *display)
3993{
3994 return display->display;
3995}
3996
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003997struct output *
3998display_get_output(struct display *display)
3999{
4000 return container_of(display->output_list.next, struct output, link);
4001}
4002
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004003struct wl_compositor *
4004display_get_compositor(struct display *display)
4005{
4006 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004007}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004008
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004009uint32_t
4010display_get_serial(struct display *display)
4011{
4012 return display->serial;
4013}
4014
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004015EGLDisplay
4016display_get_egl_display(struct display *d)
4017{
4018 return d->dpy;
4019}
4020
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004021struct wl_data_source *
4022display_create_data_source(struct display *display)
4023{
4024 return wl_data_device_manager_create_data_source(display->data_device_manager);
4025}
4026
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004027EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004028display_get_argb_egl_config(struct display *d)
4029{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004030 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004031}
4032
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004033struct wl_shell *
4034display_get_shell(struct display *display)
4035{
4036 return display->shell;
4037}
4038
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004039int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004040display_acquire_window_surface(struct display *display,
4041 struct window *window,
4042 EGLContext ctx)
4043{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004044#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004045 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004046 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004047
4048 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004049 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004050 device = cairo_surface_get_device(window->cairo_surface);
4051 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004052 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004053
Benjamin Franzke0c991632011-09-27 21:57:31 +02004054 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004055 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004056 ctx = display->argb_ctx;
4057 else
4058 assert(0);
4059 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004060
4061 data = cairo_surface_get_user_data(window->cairo_surface,
4062 &surface_data_key);
4063
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004064 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004065 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004066 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4067 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004068
4069 return 0;
4070#else
4071 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004072#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004073}
4074
4075void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004076display_release_window_surface(struct display *display,
4077 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004078{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004079#ifdef HAVE_CAIRO_EGL
4080 cairo_device_t *device;
4081
4082 device = cairo_surface_get_device(window->cairo_surface);
4083 if (!device)
4084 return;
4085
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004086 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004087 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004088 cairo_device_release(device);
4089#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004090}
4091
4092void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004093display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004094{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004095 wl_list_insert(&display->deferred_list, &task->link);
4096}
4097
4098void
4099display_watch_fd(struct display *display,
4100 int fd, uint32_t events, struct task *task)
4101{
4102 struct epoll_event ep;
4103
4104 ep.events = events;
4105 ep.data.ptr = task;
4106 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4107}
4108
4109void
4110display_run(struct display *display)
4111{
4112 struct task *task;
4113 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004114 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004115
Pekka Paalanen826d7952011-12-15 10:14:07 +02004116 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004117 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004118 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004119 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004120 struct task, link);
4121 wl_list_remove(&task->link);
4122 task->run(task, 0);
4123 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004124
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004125 wl_display_dispatch_pending(display->display);
4126
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004127 if (!display->running)
4128 break;
4129
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004130 ret = wl_display_flush(display->display);
4131 if (ret < 0 && errno == EAGAIN) {
4132 ep[0].events =
4133 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4134 ep[0].data.ptr = &display->display_task;
4135
4136 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4137 display->display_fd, &ep[0]);
4138 } else if (ret < 0) {
4139 break;
4140 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004141
4142 count = epoll_wait(display->epoll_fd,
4143 ep, ARRAY_LENGTH(ep), -1);
4144 for (i = 0; i < count; i++) {
4145 task = ep[i].data.ptr;
4146 task->run(task, ep[i].events);
4147 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004148 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004149}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004150
4151void
4152display_exit(struct display *display)
4153{
4154 display->running = 0;
4155}