blob: 30fc67c4f51c096206358fd888078dba25bf2ecc [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberg8def2642011-01-14 17:41:33 -050055#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040057#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050058
Daniel Stone9d4f0302012-02-15 16:33:21 +000059#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030060#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020063#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040064#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060065#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020066#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030067#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050070
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070071struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030072
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040073struct global {
74 uint32_t name;
75 char *interface;
76 uint32_t version;
77 struct wl_list link;
78};
79
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050080struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050081 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040082 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050083 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040084 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040085 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040086 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060087 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020088 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040089 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050090 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020091 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020092 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040093 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040094
95 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -070096 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040097 struct task display_task;
98
99 int epoll_fd;
100 struct wl_list deferred_list;
101
Pekka Paalanen826d7952011-12-15 10:14:07 +0200102 int running;
103
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400104 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400105 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400106 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500107 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400108
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400109 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400110
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300111 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300112 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400113
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200114 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400115 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200116
117 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100118
119 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200120
121 uint32_t workspace;
122 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500123};
124
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400126 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400127 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400128 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500129 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500131 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400132 TYPE_CUSTOM
133};
Rob Bradford7507b572012-05-15 17:55:34 +0100134
135struct window_output {
136 struct output *output;
137 struct wl_list link;
138};
139
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500140struct window {
141 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500142 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100143 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500144 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200145 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500146 struct wl_region *input_region;
147 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500148 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500149 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400150 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500151 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500152 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400153 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400154 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400155 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400156 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400157 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400158 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400159 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400160 int focus_count;
161
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400162 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500163
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400164 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500165
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700166 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400167
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500168 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500169 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400170 window_data_handler_t data_handler;
171 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500172 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400173 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400174
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300175 struct wl_callback *frame_cb;
176
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200177 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500178 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500179
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500180 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400181 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500182};
183
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500184struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500185 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300186 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500187 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400188 struct wl_list link;
189 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500190 widget_resize_handler_t resize_handler;
191 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500192 widget_enter_handler_t enter_handler;
193 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500194 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500195 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200196 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400197 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500198 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300199 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400200};
201
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400202struct input {
203 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100204 struct wl_seat *seat;
205 struct wl_pointer *pointer;
206 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207 struct window *pointer_focus;
208 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300209 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300210 uint32_t cursor_anim_start;
211 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300212 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400213 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400214 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400215 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400216 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400217 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400218
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500219 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500220 struct widget *grab;
221 uint32_t grab_button;
222
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400223 struct wl_data_device *data_device;
224 struct data_offer *drag_offer;
225 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100226
227 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100228 struct xkb_keymap *keymap;
229 struct xkb_state *state;
230 xkb_mod_mask_t control_mask;
231 xkb_mod_mask_t alt_mask;
232 xkb_mod_mask_t shift_mask;
233 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400234
235 struct task repeat_task;
236 int repeat_timer_fd;
237 uint32_t repeat_sym;
238 uint32_t repeat_key;
239 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400240};
241
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500242struct output {
243 struct display *display;
244 struct wl_output *output;
245 struct rectangle allocation;
246 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600247 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200248
249 display_output_handler_t destroy_handler;
250 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500251};
252
Martin Minarik1998b152012-05-10 02:04:35 +0200253enum frame_button_action {
254 FRAME_BUTTON_NULL = 0,
255 FRAME_BUTTON_ICON = 1,
256 FRAME_BUTTON_CLOSE = 2,
257 FRAME_BUTTON_MINIMIZE = 3,
258 FRAME_BUTTON_MAXIMIZE = 4,
259};
260
261enum frame_button_pointer {
262 FRAME_BUTTON_DEFAULT = 0,
263 FRAME_BUTTON_OVER = 1,
264 FRAME_BUTTON_ACTIVE = 2,
265};
266
267enum frame_button_align {
268 FRAME_BUTTON_RIGHT = 0,
269 FRAME_BUTTON_LEFT = 1,
270};
271
272enum frame_button_decoration {
273 FRAME_BUTTON_NONE = 0,
274 FRAME_BUTTON_FANCY = 1,
275};
276
277struct frame_button {
278 struct widget *widget;
279 struct frame *frame;
280 cairo_surface_t *icon;
281 enum frame_button_action type;
282 enum frame_button_pointer state;
283 struct wl_list link; /* buttons_list */
284 enum frame_button_align align;
285 enum frame_button_decoration decoration;
286};
287
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500288struct frame {
289 struct widget *widget;
290 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200291 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500292};
293
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500294struct menu {
295 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500296 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500297 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500298 const char **entries;
299 uint32_t time;
300 int current;
301 int count;
302 menu_func_t func;
303};
304
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300305struct tooltip {
306 struct widget *parent;
307 struct window *window;
308 struct widget *widget;
309 char *entry;
310 struct task tooltip_task;
311 int tooltip_fd;
312 float x, y;
313};
314
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700315struct shm_pool {
316 struct wl_shm_pool *pool;
317 size_t size;
318 size_t used;
319 void *data;
320};
321
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400322enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300323 CURSOR_DEFAULT = 100,
324 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400325};
326
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500327enum window_location {
328 WINDOW_INTERIOR = 0,
329 WINDOW_RESIZING_TOP = 1,
330 WINDOW_RESIZING_BOTTOM = 2,
331 WINDOW_RESIZING_LEFT = 4,
332 WINDOW_RESIZING_TOP_LEFT = 5,
333 WINDOW_RESIZING_BOTTOM_LEFT = 6,
334 WINDOW_RESIZING_RIGHT = 8,
335 WINDOW_RESIZING_TOP_RIGHT = 9,
336 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
337 WINDOW_RESIZING_MASK = 15,
338 WINDOW_EXTERIOR = 16,
339 WINDOW_TITLEBAR = 17,
340 WINDOW_CLIENT_AREA = 18,
341};
342
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400343static const cairo_user_data_key_t surface_data_key;
344struct surface_data {
345 struct wl_buffer *buffer;
346};
347
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500348#define MULT(_d,c,a,t) \
349 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
350
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500351#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100353struct egl_window_surface_data {
354 struct display *display;
355 struct wl_surface *surface;
356 struct wl_egl_window *window;
357 EGLSurface surf;
358};
359
360static void
361egl_window_surface_data_destroy(void *p)
362{
363 struct egl_window_surface_data *data = p;
364 struct display *d = data->display;
365
366 eglDestroySurface(d->dpy, data->surf);
367 wl_egl_window_destroy(data->window);
368 data->surface = NULL;
369
370 free(p);
371}
372
373static cairo_surface_t *
374display_create_egl_window_surface(struct display *display,
375 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377 struct rectangle *rectangle)
378{
379 cairo_surface_t *cairo_surface;
380 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
384 data = malloc(sizeof *data);
385 if (data == NULL)
386 return NULL;
387
388 data->display = display;
389 data->surface = surface;
390
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500391 config = display->argb_config;
392 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400393
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400394 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100395 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400396 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100397
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400398 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500399 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100400
Benjamin Franzke0c991632011-09-27 21:57:31 +0200401 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100402 data->surf,
403 rectangle->width,
404 rectangle->height);
405
406 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
407 data, egl_window_surface_data_destroy);
408
409 return cairo_surface;
410}
411
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412#endif
413
414struct wl_buffer *
415display_get_buffer_for_surface(struct display *display,
416 cairo_surface_t *surface)
417{
418 struct surface_data *data;
419
420 data = cairo_surface_get_user_data (surface, &surface_data_key);
421
422 return data->buffer;
423}
424
425struct shm_surface_data {
426 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700427 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428};
429
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500430static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431shm_pool_destroy(struct shm_pool *pool);
432
433static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434shm_surface_data_destroy(void *p)
435{
436 struct shm_surface_data *data = p;
437
438 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700439 if (data->pool)
440 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300441
442 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443}
444
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445static struct wl_shm_pool *
446make_shm_pool(struct display *display, int size, void **data)
447{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400448 struct wl_shm_pool *pool;
449 int fd;
450
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300451 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300453 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
454 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400455 return NULL;
456 }
457
458 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400459 if (*data == MAP_FAILED) {
460 fprintf(stderr, "mmap failed: %m\n");
461 close(fd);
462 return NULL;
463 }
464
465 pool = wl_shm_create_pool(display->shm, fd, size);
466
467 close(fd);
468
469 return pool;
470}
471
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700472static struct shm_pool *
473shm_pool_create(struct display *display, size_t size)
474{
475 struct shm_pool *pool = malloc(sizeof *pool);
476
477 if (!pool)
478 return NULL;
479
480 pool->pool = make_shm_pool(display, size, &pool->data);
481 if (!pool->pool) {
482 free(pool);
483 return NULL;
484 }
485
486 pool->size = size;
487 pool->used = 0;
488
489 return pool;
490}
491
492static void *
493shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
494{
495 if (pool->used + size > pool->size)
496 return NULL;
497
498 *offset = pool->used;
499 pool->used += size;
500
501 return (char *) pool->data + *offset;
502}
503
504/* destroy the pool. this does not unmap the memory though */
505static void
506shm_pool_destroy(struct shm_pool *pool)
507{
508 munmap(pool->data, pool->size);
509 wl_shm_pool_destroy(pool->pool);
510 free(pool);
511}
512
513/* Start allocating from the beginning of the pool again */
514static void
515shm_pool_reset(struct shm_pool *pool)
516{
517 pool->used = 0;
518}
519
520static int
521data_length_for_shm_surface(struct rectangle *rect)
522{
523 int stride;
524
525 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
526 rect->width);
527 return stride * rect->height;
528}
529
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500530static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531display_create_shm_surface_from_pool(struct display *display,
532 struct rectangle *rectangle,
533 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534{
535 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400536 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400537 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700538 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540
541 data = malloc(sizeof *data);
542 if (data == NULL)
543 return NULL;
544
545 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
546 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700547 length = stride * rectangle->height;
548 data->pool = NULL;
549 map = shm_pool_allocate(pool, length, &offset);
550
551 if (!map) {
552 free(data);
553 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400554 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400556 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557 CAIRO_FORMAT_ARGB32,
558 rectangle->width,
559 rectangle->height,
560 stride);
561
562 cairo_surface_set_user_data (surface, &surface_data_key,
563 data, shm_surface_data_destroy);
564
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400565 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500566 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400567 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500568 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700570 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400571 rectangle->width,
572 rectangle->height,
573 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400574
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700575 return surface;
576}
577
578static cairo_surface_t *
579display_create_shm_surface(struct display *display,
580 struct rectangle *rectangle, uint32_t flags,
581 struct window *window)
582{
583 struct shm_surface_data *data;
584 struct shm_pool *pool;
585 cairo_surface_t *surface;
586
587 if (window && window->pool) {
588 shm_pool_reset(window->pool);
589 surface = display_create_shm_surface_from_pool(display,
590 rectangle,
591 flags,
592 window->pool);
593 if (surface)
594 return surface;
595 }
596
597 pool = shm_pool_create(display,
598 data_length_for_shm_surface(rectangle));
599 if (!pool)
600 return NULL;
601
602 surface =
603 display_create_shm_surface_from_pool(display, rectangle,
604 flags, pool);
605
606 if (!surface) {
607 shm_pool_destroy(pool);
608 return NULL;
609 }
610
611 /* make sure we destroy the pool when the surface is destroyed */
612 data = cairo_surface_get_user_data(surface, &surface_data_key);
613 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614
615 return surface;
616}
617
nobled7b87cb02011-02-01 18:51:47 +0000618static int
619check_size(struct rectangle *rect)
620{
621 if (rect->width && rect->height)
622 return 0;
623
624 fprintf(stderr, "tried to create surface of "
625 "width: %d, height: %d\n", rect->width, rect->height);
626 return -1;
627}
628
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629cairo_surface_t *
630display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400632 struct rectangle *rectangle,
633 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634{
nobled7b87cb02011-02-01 18:51:47 +0000635 if (check_size(rectangle) < 0)
636 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500637#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300638 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400639 return display_create_egl_window_surface(display,
640 surface,
641 flags,
642 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400644 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645}
646
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200647/*
648 * The following correspondences between file names and cursors was copied
649 * from: https://bugs.kde.org/attachment.cgi?id=67313
650 */
651
652static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200654 "sw-resize"
655};
656
657static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300658 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200659 "se-resize"
660};
661
662static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300663 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200664 "s-resize"
665};
666
667static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200669 "closedhand",
670 "208530c400c041818281048008011002"
671};
672
673static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200675 "default",
676 "top_left_arrow",
677 "left-arrow"
678};
679
680static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300681 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200682 "w-resize"
683};
684
685static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300686 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200687 "e-resize"
688};
689
690static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300691 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200692 "nw-resize"
693};
694
695static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300696 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200697 "ne-resize"
698};
699
700static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300701 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200702 "n-resize"
703};
704
705static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300706 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200707 "ibeam",
708 "text"
709};
710
711static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300712 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200713 "pointer",
714 "pointing_hand",
715 "e29285e634086352946a0e7090d73106"
716};
717
718static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400719 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200720 "wait",
721 "0426c94ea35c87780ff01dc239897213"
722};
723
724struct cursor_alternatives {
725 const char **names;
726 size_t count;
727};
728
729static const struct cursor_alternatives cursors[] = {
730 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
731 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
732 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
733 {grabbings, ARRAY_LENGTH(grabbings)},
734 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
735 {left_sides, ARRAY_LENGTH(left_sides)},
736 {right_sides, ARRAY_LENGTH(right_sides)},
737 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
738 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
739 {top_sides, ARRAY_LENGTH(top_sides)},
740 {xterms, ARRAY_LENGTH(xterms)},
741 {hand1s, ARRAY_LENGTH(hand1s)},
742 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300743};
744
745static void
746create_cursors(struct display *display)
747{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100748 char *config_file;
749 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200750 unsigned int i, j;
751 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100752 struct config_key shell_keys[] = {
753 { "cursor-theme", CONFIG_KEY_STRING, &theme },
754 };
755 struct config_section cs[] = {
756 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
757 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300758
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100759 config_file = config_file_path("weston.ini");
760 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
761 free(config_file);
762
763 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300764 display->cursors =
765 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
766
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300767 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200768 cursor = NULL;
769 for (j = 0; !cursor && j < cursors[i].count; ++j)
770 cursor = wl_cursor_theme_get_cursor(
771 display->cursor_theme, cursors[i].names[j]);
772
773 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300774 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200775 cursors[i].names[0]);
776
777 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300778 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300779}
780
781static void
782destroy_cursors(struct display *display)
783{
784 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800785 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300786}
787
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300788struct wl_cursor_image *
789display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400790{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200791 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400792
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300793 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400794}
795
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400796static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200797window_get_resize_dx_dy(struct window *window, int *x, int *y)
798{
799 if (window->resize_edges & WINDOW_RESIZING_LEFT)
800 *x = window->server_allocation.width - window->allocation.width;
801 else
802 *x = 0;
803
804 if (window->resize_edges & WINDOW_RESIZING_TOP)
805 *y = window->server_allocation.height -
806 window->allocation.height;
807 else
808 *y = 0;
809
810 window->resize_edges = 0;
811}
812
813static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500814window_attach_surface(struct window *window)
815{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400816 struct display *display = window->display;
817 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500821 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400823 if (window->type == TYPE_NONE) {
824 window->type = TYPE_TOPLEVEL;
825 if (display->shell)
826 wl_shell_surface_set_toplevel(window->shell_surface);
827 }
828
Pekka Paalanen512dde82012-10-10 12:49:27 +0300829 if (window->opaque_region) {
830 wl_surface_set_opaque_region(window->surface,
831 window->opaque_region);
832 wl_region_destroy(window->opaque_region);
833 window->opaque_region = NULL;
834 }
835
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300836 if (window->input_region) {
837 wl_surface_set_input_region(window->surface,
838 window->input_region);
839 wl_region_destroy(window->input_region);
840 window->input_region = NULL;
841 }
842
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000844#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
846 data = cairo_surface_get_user_data(window->cairo_surface,
847 &surface_data_key);
848
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 cairo_gl_surface_swapbuffers(window->cairo_surface);
850 wl_egl_window_get_attached_size(data->window,
851 &window->server_allocation.width,
852 &window->server_allocation.height);
853 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000854#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 buffer =
857 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400858 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200860 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400862 wl_surface_damage(window->surface, 0, 0,
863 window->allocation.width,
864 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300865 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400867 cairo_surface_destroy(window->cairo_surface);
868 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100869 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000870 default:
871 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500873}
874
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400875int
876window_has_focus(struct window *window)
877{
878 return window->focus_count > 0;
879}
880
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500881void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400882window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500883{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400884 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100885 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500886}
887
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888void
889window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500891 cairo_surface_reference(surface);
892
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893 if (window->cairo_surface != NULL)
894 cairo_surface_destroy(window->cairo_surface);
895
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500896 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897}
898
Benjamin Franzke22d54812011-07-16 19:50:32 +0000899#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900static void
901window_resize_cairo_window_surface(struct window *window)
902{
903 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905
906 data = cairo_surface_get_user_data(window->cairo_surface,
907 &surface_data_key);
908
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200909 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 wl_egl_window_resize(data->window,
911 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200912 window->allocation.height,
913 x,y);
914
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915 cairo_gl_surface_set_size(window->cairo_surface,
916 window->allocation.width,
917 window->allocation.height);
918}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000919#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400921struct display *
922window_get_display(struct window *window)
923{
924 return window->display;
925}
926
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400927void
928window_create_surface(struct window *window)
929{
930 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 uint32_t flags = 0;
932
933 if (!window->transparent)
934 flags = SURFACE_OPAQUE;
935
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500937#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
939 if (window->cairo_surface) {
940 window_resize_cairo_window_surface(window);
941 return;
942 }
943 surface = display_create_surface(window->display,
944 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100946 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947#endif
948 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400950 &window->allocation,
951 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800953 default:
954 surface = NULL;
955 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400957
958 window_set_surface(window, surface);
959 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960}
961
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200962static void frame_destroy(struct frame *frame);
963
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400964void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500965window_destroy(struct window *window)
966{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200967 struct display *display = window->display;
968 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100969 struct window_output *window_output;
970 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971
972 if (window->redraw_scheduled)
973 wl_list_remove(&window->redraw_task.link);
974
975 wl_list_for_each(input, &display->input_list, link) {
976 if (input->pointer_focus == window)
977 input->pointer_focus = NULL;
978 if (input->keyboard_focus == window)
979 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500980 if (input->focus_widget &&
981 input->focus_widget->window == window)
982 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200983 }
984
Rob Bradford7507b572012-05-15 17:55:34 +0100985 wl_list_for_each_safe(window_output, window_output_tmp,
986 &window->window_output_list, link) {
987 free (window_output);
988 }
989
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500990 if (window->input_region)
991 wl_region_destroy(window->input_region);
992 if (window->opaque_region)
993 wl_region_destroy(window->opaque_region);
994
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200995 if (window->frame)
996 frame_destroy(window->frame);
997
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200998 if (window->shell_surface)
999 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000 wl_surface_destroy(window->surface);
1001 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001002
1003 if (window->cairo_surface != NULL)
1004 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001005
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001006 if (window->frame_cb)
1007 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001008 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001009 free(window);
1010}
1011
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001013widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001016
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017 wl_list_for_each(child, &widget->child_list, link) {
1018 target = widget_find_widget(child, x, y);
1019 if (target)
1020 return target;
1021 }
1022
1023 if (widget->allocation.x <= x &&
1024 x < widget->allocation.x + widget->allocation.width &&
1025 widget->allocation.y <= y &&
1026 y < widget->allocation.y + widget->allocation.height) {
1027 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028 }
1029
1030 return NULL;
1031}
1032
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001033static struct widget *
1034widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001035{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001036 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001037
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038 widget = malloc(sizeof *widget);
1039 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001040 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001042 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001043 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001044 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001045 widget->tooltip = NULL;
1046 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001047
1048 return widget;
1049}
1050
1051struct widget *
1052window_add_widget(struct window *window, void *data)
1053{
1054 window->widget = widget_create(window, data);
1055 wl_list_init(&window->widget->link);
1056
1057 return window->widget;
1058}
1059
1060struct widget *
1061widget_add_widget(struct widget *parent, void *data)
1062{
1063 struct widget *widget;
1064
1065 widget = widget_create(parent->window, data);
1066 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069}
1070
1071void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001072widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001074 struct display *display = widget->window->display;
1075 struct input *input;
1076
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001077 if (widget->tooltip) {
1078 free(widget->tooltip);
1079 widget->tooltip = NULL;
1080 }
1081
Pekka Paalanene156fb62012-01-19 13:51:38 +02001082 wl_list_for_each(input, &display->input_list, link) {
1083 if (input->focus_widget == widget)
1084 input->focus_widget = NULL;
1085 }
1086
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001087 wl_list_remove(&widget->link);
1088 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001094 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095}
1096
1097void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001098widget_set_size(struct widget *widget, int32_t width, int32_t height)
1099{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001100 widget->allocation.width = width;
1101 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001102}
1103
1104void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001105widget_set_allocation(struct widget *widget,
1106 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001107{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001108 widget->allocation.x = x;
1109 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001110 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111}
1112
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001113void
1114widget_set_transparent(struct widget *widget, int transparent)
1115{
1116 widget->opaque = !transparent;
1117}
1118
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001119void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001120widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001121{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001122 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123}
1124
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001125void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001126widget_set_resize_handler(struct widget *widget,
1127 widget_resize_handler_t handler)
1128{
1129 widget->resize_handler = handler;
1130}
1131
1132void
1133widget_set_redraw_handler(struct widget *widget,
1134 widget_redraw_handler_t handler)
1135{
1136 widget->redraw_handler = handler;
1137}
1138
1139void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001140widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001141{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001142 widget->enter_handler = handler;
1143}
1144
1145void
1146widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1147{
1148 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001149}
1150
1151void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001152widget_set_motion_handler(struct widget *widget,
1153 widget_motion_handler_t handler)
1154{
1155 widget->motion_handler = handler;
1156}
1157
1158void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001159widget_set_button_handler(struct widget *widget,
1160 widget_button_handler_t handler)
1161{
1162 widget->button_handler = handler;
1163}
1164
1165void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001166widget_set_axis_handler(struct widget *widget,
1167 widget_axis_handler_t handler)
1168{
1169 widget->axis_handler = handler;
1170}
1171
1172void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001173widget_schedule_redraw(struct widget *widget)
1174{
1175 window_schedule_redraw(widget->window);
1176}
1177
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001178cairo_surface_t *
1179window_get_surface(struct window *window)
1180{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001181 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001182}
1183
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001184struct wl_surface *
1185window_get_wl_surface(struct window *window)
1186{
1187 return window->surface;
1188}
1189
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001190struct wl_shell_surface *
1191window_get_wl_shell_surface(struct window *window)
1192{
1193 return window->shell_surface;
1194}
1195
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001196static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001197tooltip_redraw_handler(struct widget *widget, void *data)
1198{
1199 cairo_t *cr;
1200 const int32_t r = 3;
1201 struct tooltip *tooltip = data;
1202 int32_t width, height;
1203 struct window *window = widget->window;
1204
1205 cr = cairo_create(window->cairo_surface);
1206 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1207 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1208 cairo_paint(cr);
1209
1210 width = window->allocation.width;
1211 height = window->allocation.height;
1212 rounded_rect(cr, 0, 0, width, height, r);
1213
1214 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1215 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1216 cairo_fill(cr);
1217
1218 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1219 cairo_move_to(cr, 10, 16);
1220 cairo_show_text(cr, tooltip->entry);
1221 cairo_destroy(cr);
1222}
1223
1224static cairo_text_extents_t
1225get_text_extents(struct tooltip *tooltip)
1226{
1227 struct window *window;
1228 cairo_t *cr;
1229 cairo_text_extents_t extents;
1230
1231 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1232 * created yet */
1233 window = tooltip->widget->window->parent;
1234 cr = cairo_create(window->cairo_surface);
1235 cairo_text_extents(cr, tooltip->entry, &extents);
1236 cairo_destroy(cr);
1237
1238 return extents;
1239}
1240
1241static int
1242window_create_tooltip(struct tooltip *tooltip)
1243{
1244 struct widget *parent = tooltip->parent;
1245 struct display *display = parent->window->display;
1246 struct window *window;
1247 const int offset_y = 27;
1248 const int margin = 3;
1249 cairo_text_extents_t extents;
1250
1251 if (tooltip->widget)
1252 return 0;
1253
1254 window = window_create_transient(display, parent->window, tooltip->x,
1255 tooltip->y + offset_y,
1256 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1257 if (!window)
1258 return -1;
1259
1260 tooltip->window = window;
1261 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1262
1263 extents = get_text_extents(tooltip);
1264 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1265 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1266
1267 return 0;
1268}
1269
1270void
1271widget_destroy_tooltip(struct widget *parent)
1272{
1273 struct tooltip *tooltip = parent->tooltip;
1274
1275 parent->tooltip_count = 0;
1276 if (!tooltip)
1277 return;
1278
1279 if (tooltip->widget) {
1280 widget_destroy(tooltip->widget);
1281 window_destroy(tooltip->window);
1282 tooltip->widget = NULL;
1283 tooltip->window = NULL;
1284 }
1285
1286 close(tooltip->tooltip_fd);
1287 free(tooltip->entry);
1288 free(tooltip);
1289 parent->tooltip = NULL;
1290}
1291
1292static void
1293tooltip_func(struct task *task, uint32_t events)
1294{
1295 struct tooltip *tooltip =
1296 container_of(task, struct tooltip, tooltip_task);
1297 uint64_t exp;
1298
Martin Olsson8df662a2012-07-08 03:03:47 +02001299 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1300 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001301 window_create_tooltip(tooltip);
1302}
1303
1304#define TOOLTIP_TIMEOUT 500
1305static int
1306tooltip_timer_reset(struct tooltip *tooltip)
1307{
1308 struct itimerspec its;
1309
1310 its.it_interval.tv_sec = 0;
1311 its.it_interval.tv_nsec = 0;
1312 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1313 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1314 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1315 fprintf(stderr, "could not set timerfd\n: %m");
1316 return -1;
1317 }
1318
1319 return 0;
1320}
1321
1322int
1323widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1324{
1325 struct tooltip *tooltip = parent->tooltip;
1326
1327 parent->tooltip_count++;
1328 if (tooltip) {
1329 tooltip->x = x;
1330 tooltip->y = y;
1331 tooltip_timer_reset(tooltip);
1332 return 0;
1333 }
1334
1335 /* the handler might be triggered too fast via input device motion, so
1336 * we need this check here to make sure tooltip is fully initialized */
1337 if (parent->tooltip_count > 1)
1338 return 0;
1339
1340 tooltip = malloc(sizeof *tooltip);
1341 if (!tooltip)
1342 return -1;
1343
1344 parent->tooltip = tooltip;
1345 tooltip->parent = parent;
1346 tooltip->widget = NULL;
1347 tooltip->window = NULL;
1348 tooltip->x = x;
1349 tooltip->y = y;
1350 tooltip->entry = strdup(entry);
1351 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1352 if (tooltip->tooltip_fd < 0) {
1353 fprintf(stderr, "could not create timerfd\n: %m");
1354 return -1;
1355 }
1356
1357 tooltip->tooltip_task.run = tooltip_func;
1358 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1359 EPOLLIN, &tooltip->tooltip_task);
1360 tooltip_timer_reset(tooltip);
1361
1362 return 0;
1363}
1364
1365static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001366workspace_manager_state(void *data,
1367 struct workspace_manager *workspace_manager,
1368 uint32_t current,
1369 uint32_t count)
1370{
1371 struct display *display = data;
1372
1373 display->workspace = current;
1374 display->workspace_count = count;
1375}
1376
1377static const struct workspace_manager_listener workspace_manager_listener = {
1378 workspace_manager_state
1379};
1380
1381static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001382frame_resize_handler(struct widget *widget,
1383 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001384{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385 struct frame *frame = data;
1386 struct widget *child = frame->child;
1387 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001388 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001389 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001390 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001391 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001392 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001393 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001394
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001395 switch (widget->window->type) {
1396 case TYPE_FULLSCREEN:
1397 decoration_width = 0;
1398 decoration_height = 0;
1399
1400 allocation.x = 0;
1401 allocation.y = 0;
1402 allocation.width = width;
1403 allocation.height = height;
1404 opaque_margin = 0;
1405
1406 wl_list_for_each(button, &frame->buttons_list, link)
1407 button->widget->opaque = 1;
1408 break;
1409 case TYPE_MAXIMIZED:
1410 decoration_width = t->width * 2;
1411 decoration_height = t->width + t->titlebar_height;
1412
1413 allocation.x = t->width;
1414 allocation.y = t->titlebar_height;
1415 allocation.width = width - decoration_width;
1416 allocation.height = height - decoration_height;
1417
1418 opaque_margin = 0;
1419
1420 wl_list_for_each(button, &frame->buttons_list, link)
1421 button->widget->opaque = 0;
1422 break;
1423 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001424 decoration_width = (t->width + t->margin) * 2;
1425 decoration_height = t->width +
1426 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 allocation.x = t->width + t->margin;
1429 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001430 allocation.width = width - decoration_width;
1431 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001432
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001433 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001434
1435 wl_list_for_each(button, &frame->buttons_list, link)
1436 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001437 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001438 }
1439
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001440 widget_set_allocation(child, allocation.x, allocation.y,
1441 allocation.width, allocation.height);
1442
1443 if (child->resize_handler)
1444 child->resize_handler(child,
1445 allocation.width,
1446 allocation.height,
1447 child->user_data);
1448
Scott Moreauf7e498c2012-05-14 11:39:29 -06001449 width = child->allocation.width + decoration_width;
1450 height = child->allocation.height + decoration_height;
1451
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001452 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1453
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001454 widget->window->input_region =
1455 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001456 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001457 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001458 shadow_margin, shadow_margin,
1459 width - 2 * shadow_margin,
1460 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001461 } else {
1462 wl_region_add(widget->window->input_region,
1463 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001464 }
1465
Scott Moreauf7e498c2012-05-14 11:39:29 -06001466 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001467
1468 if (child->opaque) {
1469 widget->window->opaque_region =
1470 wl_compositor_create_region(display->compositor);
1471 wl_region_add(widget->window->opaque_region,
1472 opaque_margin, opaque_margin,
1473 widget->allocation.width - 2 * opaque_margin,
1474 widget->allocation.height - 2 * opaque_margin);
1475 }
Martin Minarik1998b152012-05-10 02:04:35 +02001476
1477 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001478 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1479 x_l = t->width + shadow_margin;
1480 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001481 wl_list_for_each(button, &frame->buttons_list, link) {
1482 const int button_padding = 4;
1483 w = cairo_image_surface_get_width(button->icon);
1484 h = cairo_image_surface_get_height(button->icon);
1485
1486 if (button->decoration == FRAME_BUTTON_FANCY)
1487 w += 10;
1488
1489 if (button->align == FRAME_BUTTON_LEFT) {
1490 widget_set_allocation(button->widget,
1491 x_l, y , w + 1, h + 1);
1492 x_l += w;
1493 x_l += button_padding;
1494 } else {
1495 x_r -= w;
1496 widget_set_allocation(button->widget,
1497 x_r, y , w + 1, h + 1);
1498 x_r -= button_padding;
1499 }
1500 }
1501}
1502
1503static int
1504frame_button_enter_handler(struct widget *widget,
1505 struct input *input, float x, float y, void *data)
1506{
1507 struct frame_button *frame_button = data;
1508
1509 widget_schedule_redraw(frame_button->widget);
1510 frame_button->state = FRAME_BUTTON_OVER;
1511
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001512 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001513}
1514
1515static void
1516frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1517{
1518 struct frame_button *frame_button = data;
1519
1520 widget_schedule_redraw(frame_button->widget);
1521 frame_button->state = FRAME_BUTTON_DEFAULT;
1522}
1523
1524static void
1525frame_button_button_handler(struct widget *widget,
1526 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001527 uint32_t button,
1528 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001529{
1530 struct frame_button *frame_button = data;
1531 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001532 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001533
1534 if (button != BTN_LEFT)
1535 return;
1536
1537 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001538 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001539 frame_button->state = FRAME_BUTTON_ACTIVE;
1540 widget_schedule_redraw(frame_button->widget);
1541
1542 if (frame_button->type == FRAME_BUTTON_ICON)
1543 window_show_frame_menu(window, input, time);
1544 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001545 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001546 frame_button->state = FRAME_BUTTON_DEFAULT;
1547 widget_schedule_redraw(frame_button->widget);
1548 break;
1549 }
1550
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001551 if (!was_pressed)
1552 return;
1553
Martin Minarik1998b152012-05-10 02:04:35 +02001554 switch (frame_button->type) {
1555 case FRAME_BUTTON_CLOSE:
1556 if (window->close_handler)
1557 window->close_handler(window->parent,
1558 window->user_data);
1559 else
1560 display_exit(window->display);
1561 break;
1562 case FRAME_BUTTON_MINIMIZE:
1563 fprintf(stderr,"Minimize stub\n");
1564 break;
1565 case FRAME_BUTTON_MAXIMIZE:
1566 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1567 break;
1568 default:
1569 /* Unknown operation */
1570 break;
1571 }
1572}
1573
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001574static int
1575frame_button_motion_handler(struct widget *widget,
1576 struct input *input, uint32_t time,
1577 float x, float y, void *data)
1578{
1579 struct frame_button *frame_button = data;
1580 enum frame_button_pointer previous_button_state = frame_button->state;
1581
1582 /* only track state for a pressed button */
1583 if (input->grab != widget)
1584 return CURSOR_LEFT_PTR;
1585
1586 if (x > widget->allocation.x &&
1587 x < (widget->allocation.x + widget->allocation.width) &&
1588 y > widget->allocation.y &&
1589 y < (widget->allocation.y + widget->allocation.height)) {
1590 frame_button->state = FRAME_BUTTON_ACTIVE;
1591 } else {
1592 frame_button->state = FRAME_BUTTON_DEFAULT;
1593 }
1594
1595 if (frame_button->state != previous_button_state)
1596 widget_schedule_redraw(frame_button->widget);
1597
1598 return CURSOR_LEFT_PTR;
1599}
1600
Martin Minarik1998b152012-05-10 02:04:35 +02001601static void
1602frame_button_redraw_handler(struct widget *widget, void *data)
1603{
1604 struct frame_button *frame_button = data;
1605 cairo_t *cr;
1606 int width, height, x, y;
1607 struct window *window = widget->window;
1608
1609 x = widget->allocation.x;
1610 y = widget->allocation.y;
1611 width = widget->allocation.width;
1612 height = widget->allocation.height;
1613
1614 if (!width)
1615 return;
1616 if (!height)
1617 return;
1618 if (widget->opaque)
1619 return;
1620
1621 cr = cairo_create(window->cairo_surface);
1622
1623 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1624 cairo_set_line_width(cr, 1);
1625
1626 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1627 cairo_rectangle (cr, x, y, 25, 16);
1628
1629 cairo_stroke_preserve(cr);
1630
1631 switch (frame_button->state) {
1632 case FRAME_BUTTON_DEFAULT:
1633 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1634 break;
1635 case FRAME_BUTTON_OVER:
1636 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1637 break;
1638 case FRAME_BUTTON_ACTIVE:
1639 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1640 break;
1641 }
1642
1643 cairo_fill (cr);
1644
1645 x += 4;
1646 }
1647
1648 cairo_set_source_surface(cr, frame_button->icon, x, y);
1649 cairo_paint(cr);
1650
1651 cairo_destroy(cr);
1652}
1653
1654static struct widget *
1655frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1656 enum frame_button_align align, enum frame_button_decoration style)
1657{
1658 struct frame_button *frame_button;
1659 const char *icon = data;
1660
1661 frame_button = malloc (sizeof *frame_button);
1662 memset(frame_button, 0, sizeof *frame_button);
1663
1664 frame_button->icon = cairo_image_surface_create_from_png(icon);
1665 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1666 frame_button->frame = frame;
1667 frame_button->type = type;
1668 frame_button->align = align;
1669 frame_button->decoration = style;
1670
1671 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1672
1673 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1674 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1675 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1676 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001677 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001678 return frame_button->widget;
1679}
1680
1681static void
1682frame_button_destroy(struct frame_button *frame_button)
1683{
1684 widget_destroy(frame_button->widget);
1685 wl_list_remove(&frame_button->link);
1686 cairo_surface_destroy(frame_button->icon);
1687 free(frame_button);
1688
1689 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001690}
1691
1692static void
1693frame_redraw_handler(struct widget *widget, void *data)
1694{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001696 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001697 struct theme *t = window->display->theme;
1698 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001699
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001700 if (window->type == TYPE_FULLSCREEN)
1701 return;
1702
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001703 cr = cairo_create(window->cairo_surface);
1704
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001705 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001706 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001707 if (window->type == TYPE_MAXIMIZED)
1708 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001709 theme_render_frame(t, cr, widget->allocation.width,
1710 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001711
1712 cairo_destroy(cr);
1713}
1714
1715static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001716frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001717{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001718 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001719 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001720 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001721
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001722 location = theme_get_location(t, input->sx, input->sy,
1723 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001724 frame->widget->allocation.height,
1725 window->type == TYPE_MAXIMIZED ?
1726 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001727
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001728 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001729 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001730 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001732 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001745 case THEME_LOCATION_EXTERIOR:
1746 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001747 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001748 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001749 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001750}
1751
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001752static void
1753frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001754{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001755 struct display *display;
1756
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001757 switch (index) {
1758 case 0: /* close */
1759 if (window->close_handler)
1760 window->close_handler(window->parent,
1761 window->user_data);
1762 else
1763 display_exit(window->display);
1764 break;
1765 case 1: /* fullscreen */
1766 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001767 if (window->fullscreen_handler)
1768 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001769 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001770 case 2: /* move to workspace above */
1771 display = window->display;
1772 if (display->workspace > 0)
1773 workspace_manager_move_surface(display->workspace_manager,
1774 window->surface,
1775 display->workspace - 1);
1776 break;
1777 case 3: /* move to workspace below */
1778 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001779 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001780 workspace_manager_move_surface(display->workspace_manager,
1781 window->surface,
1782 display->workspace + 1);
1783 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001784 }
1785}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001786
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001787void
1788window_show_frame_menu(struct window *window,
1789 struct input *input, uint32_t time)
1790{
1791 int32_t x, y;
1792
1793 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001794 "Close", "Fullscreen",
1795 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001796 };
1797
1798 input_get_position(input, &x, &y);
1799 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001800 x - 10, y - 10, frame_menu_func, entries,
1801 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001802}
1803
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804static int
1805frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001806 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001807{
1808 return frame_get_pointer_image_for_location(data, input);
1809}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001810
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811static int
1812frame_motion_handler(struct widget *widget,
1813 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001814 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001815{
1816 return frame_get_pointer_image_for_location(data, input);
1817}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001818
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001819static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001820frame_button_handler(struct widget *widget,
1821 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001822 uint32_t button, enum wl_pointer_button_state state,
1823 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824
1825{
1826 struct frame *frame = data;
1827 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001828 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001829 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001830
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001831 location = theme_get_location(display->theme, input->sx, input->sy,
1832 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001833 frame->widget->allocation.height,
1834 window->type == TYPE_MAXIMIZED ?
1835 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001836
Daniel Stone4dbadb12012-05-30 16:31:51 +01001837 if (window->display->shell && button == BTN_LEFT &&
1838 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001839 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001840 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001841 if (!window->shell_surface)
1842 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001843 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001844 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001846 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001847 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001848 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001849 case THEME_LOCATION_RESIZING_TOP:
1850 case THEME_LOCATION_RESIZING_BOTTOM:
1851 case THEME_LOCATION_RESIZING_LEFT:
1852 case THEME_LOCATION_RESIZING_RIGHT:
1853 case THEME_LOCATION_RESIZING_TOP_LEFT:
1854 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1855 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1856 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001857 if (!window->shell_surface)
1858 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001859 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001860
1861 if (!display->dpy) {
1862 /* If we're using shm, allocate a big
1863 pool to create buffers out of while
1864 we resize. We should probably base
1865 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001866 window->pool =
1867 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001868 }
1869
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001870 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001871 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001872 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001873 break;
1874 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001875 } else if (button == BTN_RIGHT &&
1876 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001877 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001878 }
1879}
1880
1881struct widget *
1882frame_create(struct window *window, void *data)
1883{
1884 struct frame *frame;
1885
1886 frame = malloc(sizeof *frame);
1887 memset(frame, 0, sizeof *frame);
1888
1889 frame->widget = window_add_widget(window, frame);
1890 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001891
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001892 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1893 widget_set_resize_handler(frame->widget, frame_resize_handler);
1894 widget_set_enter_handler(frame->widget, frame_enter_handler);
1895 widget_set_motion_handler(frame->widget, frame_motion_handler);
1896 widget_set_button_handler(frame->widget, frame_button_handler);
1897
Martin Minarik1998b152012-05-10 02:04:35 +02001898 /* Create empty list for frame buttons */
1899 wl_list_init(&frame->buttons_list);
1900
1901 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1902 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1903
1904 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1905 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1906
1907 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1908 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1909
1910 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1911 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1912
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001913 window->frame = frame;
1914
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001915 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001916}
1917
Kristian Høgsberga1627922012-06-20 17:30:03 -04001918void
1919frame_set_child_size(struct widget *widget, int child_width, int child_height)
1920{
1921 struct display *display = widget->window->display;
1922 struct theme *t = display->theme;
1923 int decoration_width, decoration_height;
1924 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001925 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001926
1927 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001928 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001929 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001930 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001931
1932 width = child_width + decoration_width;
1933 height = child_height + decoration_height;
1934 } else {
1935 width = child_width;
1936 height = child_height;
1937 }
1938
1939 window_schedule_resize(widget->window, width, height);
1940}
1941
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001942static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001943frame_destroy(struct frame *frame)
1944{
Martin Minarik1998b152012-05-10 02:04:35 +02001945 struct frame_button *button, *tmp;
1946
1947 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1948 frame_button_destroy(button);
1949
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001950 /* frame->child must be destroyed by the application */
1951 widget_destroy(frame->widget);
1952 free(frame);
1953}
1954
1955static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001956input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001957 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001958{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001959 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001960 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001961
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001962 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001963 return;
1964
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001965 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001966 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001967 widget = old;
1968 if (input->grab)
1969 widget = input->grab;
1970 if (widget->leave_handler)
1971 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001972 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001973 }
1974
1975 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001976 widget = focus;
1977 if (input->grab)
1978 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001979 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001980 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001981 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001982 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001983
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001984 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001985 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001986}
1987
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001988void
1989input_grab(struct input *input, struct widget *widget, uint32_t button)
1990{
1991 input->grab = widget;
1992 input->grab_button = button;
1993}
1994
1995void
1996input_ungrab(struct input *input)
1997{
1998 struct widget *widget;
1999
2000 input->grab = NULL;
2001 if (input->pointer_focus) {
2002 widget = widget_find_widget(input->pointer_focus->widget,
2003 input->sx, input->sy);
2004 input_set_focus_widget(input, widget, input->sx, input->sy);
2005 }
2006}
2007
2008static void
2009input_remove_pointer_focus(struct input *input)
2010{
2011 struct window *window = input->pointer_focus;
2012
2013 if (!window)
2014 return;
2015
2016 input_set_focus_widget(input, NULL, 0, 0);
2017
2018 input->pointer_focus = NULL;
2019 input->current_cursor = CURSOR_UNSET;
2020}
2021
2022static void
2023pointer_handle_enter(void *data, struct wl_pointer *pointer,
2024 uint32_t serial, struct wl_surface *surface,
2025 wl_fixed_t sx_w, wl_fixed_t sy_w)
2026{
2027 struct input *input = data;
2028 struct window *window;
2029 struct widget *widget;
2030 float sx = wl_fixed_to_double(sx_w);
2031 float sy = wl_fixed_to_double(sy_w);
2032
2033 if (!surface) {
2034 /* enter event for a window we've just destroyed */
2035 return;
2036 }
2037
2038 input->display->serial = serial;
2039 input->pointer_enter_serial = serial;
2040 input->pointer_focus = wl_surface_get_user_data(surface);
2041 window = input->pointer_focus;
2042
2043 if (window->pool) {
2044 shm_pool_destroy(window->pool);
2045 window->pool = NULL;
2046 /* Schedule a redraw to free the pool */
2047 window_schedule_redraw(window);
2048 }
2049
2050 input->sx = sx;
2051 input->sy = sy;
2052
2053 widget = widget_find_widget(window->widget, sx, sy);
2054 input_set_focus_widget(input, widget, sx, sy);
2055}
2056
2057static void
2058pointer_handle_leave(void *data, struct wl_pointer *pointer,
2059 uint32_t serial, struct wl_surface *surface)
2060{
2061 struct input *input = data;
2062
2063 input->display->serial = serial;
2064 input_remove_pointer_focus(input);
2065}
2066
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002067static void
Daniel Stone37816df2012-05-16 18:45:18 +01002068pointer_handle_motion(void *data, struct wl_pointer *pointer,
2069 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002070{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002071 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002072 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002073 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002074 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002075 float sx = wl_fixed_to_double(sx_w);
2076 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002077
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002078 input->sx = sx;
2079 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002080
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002081 if (!window)
2082 return;
2083
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002084 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002085 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002086 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002087 }
2088
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002089 if (input->grab)
2090 widget = input->grab;
2091 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002092 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002093 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002094 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002095 input, time, sx, sy,
2096 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002097
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002098 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002099}
2100
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002101static void
Daniel Stone37816df2012-05-16 18:45:18 +01002102pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002103 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002104{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002105 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002106 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002107 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002108
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002109 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002110 if (input->focus_widget && input->grab == NULL &&
2111 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002112 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002113
Neil Roberts6b28aad2012-01-23 19:11:18 +00002114 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002115 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002116 (*widget->button_handler)(widget,
2117 input, time,
2118 button, state,
2119 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002120
Daniel Stone4dbadb12012-05-30 16:31:51 +01002121 if (input->grab && input->grab_button == button &&
2122 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002123 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002124}
2125
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002126static void
Daniel Stone37816df2012-05-16 18:45:18 +01002127pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002128 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002129{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002130 struct input *input = data;
2131 struct widget *widget;
2132
2133 widget = input->focus_widget;
2134 if (input->grab)
2135 widget = input->grab;
2136 if (widget && widget->axis_handler)
2137 (*widget->axis_handler)(widget,
2138 input, time,
2139 axis, value,
2140 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002141}
2142
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002143static const struct wl_pointer_listener pointer_listener = {
2144 pointer_handle_enter,
2145 pointer_handle_leave,
2146 pointer_handle_motion,
2147 pointer_handle_button,
2148 pointer_handle_axis,
2149};
2150
2151static void
2152input_remove_keyboard_focus(struct input *input)
2153{
2154 struct window *window = input->keyboard_focus;
2155 struct itimerspec its;
2156
2157 its.it_interval.tv_sec = 0;
2158 its.it_interval.tv_nsec = 0;
2159 its.it_value.tv_sec = 0;
2160 its.it_value.tv_nsec = 0;
2161 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2162
2163 if (!window)
2164 return;
2165
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002166 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002167 if (window->keyboard_focus_handler)
2168 (*window->keyboard_focus_handler)(window, NULL,
2169 window->user_data);
2170
2171 input->keyboard_focus = NULL;
2172}
2173
2174static void
2175keyboard_repeat_func(struct task *task, uint32_t events)
2176{
2177 struct input *input =
2178 container_of(task, struct input, repeat_task);
2179 struct window *window = input->keyboard_focus;
2180 uint64_t exp;
2181
2182 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2183 /* If we change the timer between the fd becoming
2184 * readable and getting here, there'll be nothing to
2185 * read and we get EAGAIN. */
2186 return;
2187
2188 if (window && window->key_handler) {
2189 (*window->key_handler)(window, input, input->repeat_time,
2190 input->repeat_key, input->repeat_sym,
2191 WL_KEYBOARD_KEY_STATE_PRESSED,
2192 window->user_data);
2193 }
2194}
2195
2196static void
2197keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2198 uint32_t format, int fd, uint32_t size)
2199{
2200 struct input *input = data;
2201 char *map_str;
2202
2203 if (!data) {
2204 close(fd);
2205 return;
2206 }
2207
2208 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2209 close(fd);
2210 return;
2211 }
2212
2213 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2214 if (map_str == MAP_FAILED) {
2215 close(fd);
2216 return;
2217 }
2218
2219 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2220 map_str,
2221 XKB_KEYMAP_FORMAT_TEXT_V1,
2222 0);
2223 munmap(map_str, size);
2224 close(fd);
2225
2226 if (!input->xkb.keymap) {
2227 fprintf(stderr, "failed to compile keymap\n");
2228 return;
2229 }
2230
2231 input->xkb.state = xkb_state_new(input->xkb.keymap);
2232 if (!input->xkb.state) {
2233 fprintf(stderr, "failed to create XKB state\n");
2234 xkb_map_unref(input->xkb.keymap);
2235 input->xkb.keymap = NULL;
2236 return;
2237 }
2238
2239 input->xkb.control_mask =
2240 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2241 input->xkb.alt_mask =
2242 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2243 input->xkb.shift_mask =
2244 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2245}
2246
2247static void
2248keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2249 uint32_t serial, struct wl_surface *surface,
2250 struct wl_array *keys)
2251{
2252 struct input *input = data;
2253 struct window *window;
2254
2255 input->display->serial = serial;
2256 input->keyboard_focus = wl_surface_get_user_data(surface);
2257
2258 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002259 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002260 if (window->keyboard_focus_handler)
2261 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002262 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002263}
2264
2265static void
2266keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2267 uint32_t serial, struct wl_surface *surface)
2268{
2269 struct input *input = data;
2270
2271 input->display->serial = serial;
2272 input_remove_keyboard_focus(input);
2273}
2274
Scott Moreau210d0792012-03-22 10:47:01 -06002275static void
Daniel Stone37816df2012-05-16 18:45:18 +01002276keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002277 uint32_t serial, uint32_t time, uint32_t key,
2278 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002279{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002280 struct input *input = data;
2281 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002282 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002283 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002284 const xkb_keysym_t *syms;
2285 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002286 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002287
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002288 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002289 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002290 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002291 return;
2292
Daniel Stone97f68542012-05-30 16:32:01 +01002293 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002294
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002295 sym = XKB_KEY_NoSymbol;
2296 if (num_syms == 1)
2297 sym = syms[0];
2298
2299 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002300 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002301 window_set_maximized(window,
2302 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002303 } else if (sym == XKB_KEY_F11 &&
2304 window->fullscreen_handler &&
2305 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2306 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002307 } else if (sym == XKB_KEY_F4 &&
2308 input->modifiers == MOD_ALT_MASK &&
2309 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2310 if (window->close_handler)
2311 window->close_handler(window->parent,
2312 window->user_data);
2313 else
2314 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002315 } else if (window->key_handler) {
2316 (*window->key_handler)(window, input, time, key,
2317 sym, state, window->user_data);
2318 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002319
2320 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2321 key == input->repeat_key) {
2322 its.it_interval.tv_sec = 0;
2323 its.it_interval.tv_nsec = 0;
2324 its.it_value.tv_sec = 0;
2325 its.it_value.tv_nsec = 0;
2326 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2327 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2328 input->repeat_sym = sym;
2329 input->repeat_key = key;
2330 input->repeat_time = time;
2331 its.it_interval.tv_sec = 0;
2332 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2333 its.it_value.tv_sec = 0;
2334 its.it_value.tv_nsec = 400 * 1000 * 1000;
2335 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2336 }
2337}
2338
2339static void
Daniel Stone351eb612012-05-31 15:27:47 -04002340keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2341 uint32_t serial, uint32_t mods_depressed,
2342 uint32_t mods_latched, uint32_t mods_locked,
2343 uint32_t group)
2344{
2345 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002346 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002347
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002348 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2349 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002350 mask = xkb_state_serialize_mods(input->xkb.state,
2351 XKB_STATE_DEPRESSED |
2352 XKB_STATE_LATCHED);
2353 input->modifiers = 0;
2354 if (mask & input->xkb.control_mask)
2355 input->modifiers |= MOD_CONTROL_MASK;
2356 if (mask & input->xkb.alt_mask)
2357 input->modifiers |= MOD_ALT_MASK;
2358 if (mask & input->xkb.shift_mask)
2359 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002360}
2361
Daniel Stone37816df2012-05-16 18:45:18 +01002362static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002363 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002364 keyboard_handle_enter,
2365 keyboard_handle_leave,
2366 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002367 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002368};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002369
2370static void
Daniel Stone37816df2012-05-16 18:45:18 +01002371seat_handle_capabilities(void *data, struct wl_seat *seat,
2372 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002373{
Daniel Stone37816df2012-05-16 18:45:18 +01002374 struct input *input = data;
2375
2376 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2377 input->pointer = wl_seat_get_pointer(seat);
2378 wl_pointer_set_user_data(input->pointer, input);
2379 wl_pointer_add_listener(input->pointer, &pointer_listener,
2380 input);
2381 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2382 wl_pointer_destroy(input->pointer);
2383 input->pointer = NULL;
2384 }
2385
2386 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2387 input->keyboard = wl_seat_get_keyboard(seat);
2388 wl_keyboard_set_user_data(input->keyboard, input);
2389 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2390 input);
2391 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2392 wl_keyboard_destroy(input->keyboard);
2393 input->keyboard = NULL;
2394 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002395}
2396
Daniel Stone37816df2012-05-16 18:45:18 +01002397static const struct wl_seat_listener seat_listener = {
2398 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002399};
2400
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002401void
2402input_get_position(struct input *input, int32_t *x, int32_t *y)
2403{
2404 *x = input->sx;
2405 *y = input->sy;
2406}
2407
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002408struct display *
2409input_get_display(struct input *input)
2410{
2411 return input->display;
2412}
2413
Daniel Stone37816df2012-05-16 18:45:18 +01002414struct wl_seat *
2415input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002416{
Daniel Stone37816df2012-05-16 18:45:18 +01002417 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002418}
2419
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002420uint32_t
2421input_get_modifiers(struct input *input)
2422{
2423 return input->modifiers;
2424}
2425
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002426struct widget *
2427input_get_focus_widget(struct input *input)
2428{
2429 return input->focus_widget;
2430}
2431
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002432struct data_offer {
2433 struct wl_data_offer *offer;
2434 struct input *input;
2435 struct wl_array types;
2436 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002437
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002438 struct task io_task;
2439 int fd;
2440 data_func_t func;
2441 int32_t x, y;
2442 void *user_data;
2443};
2444
2445static void
2446data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2447{
2448 struct data_offer *offer = data;
2449 char **p;
2450
2451 p = wl_array_add(&offer->types, sizeof *p);
2452 *p = strdup(type);
2453}
2454
2455static const struct wl_data_offer_listener data_offer_listener = {
2456 data_offer_offer,
2457};
2458
2459static void
2460data_offer_destroy(struct data_offer *offer)
2461{
2462 char **p;
2463
2464 offer->refcount--;
2465 if (offer->refcount == 0) {
2466 wl_data_offer_destroy(offer->offer);
2467 for (p = offer->types.data; *p; p++)
2468 free(*p);
2469 wl_array_release(&offer->types);
2470 free(offer);
2471 }
2472}
2473
2474static void
2475data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002476 struct wl_data_device *data_device,
2477 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002478{
2479 struct data_offer *offer;
2480
2481 offer = malloc(sizeof *offer);
2482
2483 wl_array_init(&offer->types);
2484 offer->refcount = 1;
2485 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002486 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002487 wl_data_offer_add_listener(offer->offer,
2488 &data_offer_listener, offer);
2489}
2490
2491static void
2492data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002493 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002494 wl_fixed_t x_w, wl_fixed_t y_w,
2495 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002496{
2497 struct input *input = data;
2498 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002499 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002500 float x = wl_fixed_to_double(x_w);
2501 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002502 char **p;
2503
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002504 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002505 window = wl_surface_get_user_data(surface);
2506 input->pointer_focus = window;
2507
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002508 if (offer) {
2509 input->drag_offer = wl_data_offer_get_user_data(offer);
2510
2511 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2512 *p = NULL;
2513
2514 types_data = input->drag_offer->types.data;
2515 } else {
2516 input->drag_offer = NULL;
2517 types_data = NULL;
2518 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002519
2520 window = input->pointer_focus;
2521 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002522 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002523 window->user_data);
2524}
2525
2526static void
2527data_device_leave(void *data, struct wl_data_device *data_device)
2528{
2529 struct input *input = data;
2530
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002531 if (input->drag_offer) {
2532 data_offer_destroy(input->drag_offer);
2533 input->drag_offer = NULL;
2534 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002535}
2536
2537static void
2538data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002539 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002540{
2541 struct input *input = data;
2542 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002543 float x = wl_fixed_to_double(x_w);
2544 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002545 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002546
2547 input->sx = x;
2548 input->sy = y;
2549
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002550 if (input->drag_offer)
2551 types_data = input->drag_offer->types.data;
2552 else
2553 types_data = NULL;
2554
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002555 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002556 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002557 window->user_data);
2558}
2559
2560static void
2561data_device_drop(void *data, struct wl_data_device *data_device)
2562{
2563 struct input *input = data;
2564 struct window *window = input->pointer_focus;
2565
2566 if (window->drop_handler)
2567 window->drop_handler(window, input,
2568 input->sx, input->sy, window->user_data);
2569}
2570
2571static void
2572data_device_selection(void *data,
2573 struct wl_data_device *wl_data_device,
2574 struct wl_data_offer *offer)
2575{
2576 struct input *input = data;
2577 char **p;
2578
2579 if (input->selection_offer)
2580 data_offer_destroy(input->selection_offer);
2581
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002582 if (offer) {
2583 input->selection_offer = wl_data_offer_get_user_data(offer);
2584 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2585 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002586 } else {
2587 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002588 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002589}
2590
2591static const struct wl_data_device_listener data_device_listener = {
2592 data_device_data_offer,
2593 data_device_enter,
2594 data_device_leave,
2595 data_device_motion,
2596 data_device_drop,
2597 data_device_selection
2598};
2599
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002600static void
2601input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002602{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002603 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002604 struct wl_cursor *cursor;
2605 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002606
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002607 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002608 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002609 return;
2610
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002611 if (index >= (int) cursor->image_count) {
2612 fprintf(stderr, "cursor index out of range\n");
2613 return;
2614 }
2615
2616 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002617 buffer = wl_cursor_image_get_buffer(image);
2618 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002619 return;
2620
Kristian Høgsbergae277372012-08-01 09:41:08 -04002621 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002622 input->pointer_surface,
2623 image->hotspot_x, image->hotspot_y);
2624 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2625 wl_surface_damage(input->pointer_surface, 0, 0,
2626 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002627 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002628}
2629
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002630static const struct wl_callback_listener pointer_surface_listener;
2631
2632static void
2633pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2634 uint32_t time)
2635{
2636 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002637 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002638 int i;
2639
2640 if (callback) {
2641 assert(callback == input->cursor_frame_cb);
2642 wl_callback_destroy(callback);
2643 input->cursor_frame_cb = NULL;
2644 }
2645
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002646 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002647 wl_pointer_set_cursor(input->pointer,
2648 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002649 NULL, 0, 0);
2650 return;
2651 }
2652
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002653 if (input->current_cursor == CURSOR_UNSET)
2654 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002655 cursor = input->display->cursors[input->current_cursor];
2656 if (!cursor)
2657 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002658
2659 /* FIXME We don't have the current time on the first call so we set
2660 * the animation start to the time of the first frame callback. */
2661 if (time == 0)
2662 input->cursor_anim_start = 0;
2663 else if (input->cursor_anim_start == 0)
2664 input->cursor_anim_start = time;
2665
2666 if (time == 0 || input->cursor_anim_start == 0)
2667 i = 0;
2668 else
2669 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2670
Pekka Paalanenbc106382012-10-10 12:49:31 +03002671 if (cursor->image_count > 1) {
2672 input->cursor_frame_cb =
2673 wl_surface_frame(input->pointer_surface);
2674 wl_callback_add_listener(input->cursor_frame_cb,
2675 &pointer_surface_listener, input);
2676 }
2677
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002678 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002679}
2680
2681static const struct wl_callback_listener pointer_surface_listener = {
2682 pointer_surface_frame_callback
2683};
2684
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002685void
2686input_set_pointer_image(struct input *input, int pointer)
2687{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002688 int force = 0;
2689
2690 if (input->pointer_enter_serial > input->cursor_serial)
2691 force = 1;
2692
2693 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002694 return;
2695
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002696 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002697 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002698 if (!input->cursor_frame_cb)
2699 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002700 else if (force) {
2701 /* The current frame callback may be stuck if, for instance,
2702 * the set cursor request was processed by the server after
2703 * this client lost the focus. In this case the cursor surface
2704 * might not be mapped and the frame callback wouldn't ever
2705 * complete. Send a set_cursor and attach to try to map the
2706 * cursor surface again so that the callback will finish */
2707 input_set_pointer_image_index(input, 0);
2708 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002709}
2710
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002711struct wl_data_device *
2712input_get_data_device(struct input *input)
2713{
2714 return input->data_device;
2715}
2716
2717void
2718input_set_selection(struct input *input,
2719 struct wl_data_source *source, uint32_t time)
2720{
2721 wl_data_device_set_selection(input->data_device, source, time);
2722}
2723
2724void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002725input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002726{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002727 wl_data_offer_accept(input->drag_offer->offer,
2728 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002729}
2730
2731static void
2732offer_io_func(struct task *task, uint32_t events)
2733{
2734 struct data_offer *offer =
2735 container_of(task, struct data_offer, io_task);
2736 unsigned int len;
2737 char buffer[4096];
2738
2739 len = read(offer->fd, buffer, sizeof buffer);
2740 offer->func(buffer, len,
2741 offer->x, offer->y, offer->user_data);
2742
2743 if (len == 0) {
2744 close(offer->fd);
2745 data_offer_destroy(offer);
2746 }
2747}
2748
2749static void
2750data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2751 data_func_t func, void *user_data)
2752{
2753 int p[2];
2754
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002755 if (pipe2(p, O_CLOEXEC) == -1)
2756 return;
2757
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002758 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2759 close(p[1]);
2760
2761 offer->io_task.run = offer_io_func;
2762 offer->fd = p[0];
2763 offer->func = func;
2764 offer->refcount++;
2765 offer->user_data = user_data;
2766
2767 display_watch_fd(offer->input->display,
2768 offer->fd, EPOLLIN, &offer->io_task);
2769}
2770
2771void
2772input_receive_drag_data(struct input *input, const char *mime_type,
2773 data_func_t func, void *data)
2774{
2775 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2776 input->drag_offer->x = input->sx;
2777 input->drag_offer->y = input->sy;
2778}
2779
2780int
2781input_receive_selection_data(struct input *input, const char *mime_type,
2782 data_func_t func, void *data)
2783{
2784 char **p;
2785
2786 if (input->selection_offer == NULL)
2787 return -1;
2788
2789 for (p = input->selection_offer->types.data; *p; p++)
2790 if (strcmp(mime_type, *p) == 0)
2791 break;
2792
2793 if (*p == NULL)
2794 return -1;
2795
2796 data_offer_receive_data(input->selection_offer,
2797 mime_type, func, data);
2798 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002799}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002800
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002801int
2802input_receive_selection_data_to_fd(struct input *input,
2803 const char *mime_type, int fd)
2804{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002805 if (input->selection_offer)
2806 wl_data_offer_receive(input->selection_offer->offer,
2807 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002808
2809 return 0;
2810}
2811
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002812void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002813window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002814{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002815 if (!window->shell_surface)
2816 return;
2817
Daniel Stone37816df2012-05-16 18:45:18 +01002818 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002819}
2820
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002821static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002822idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002823{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002824 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002825
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002826 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002827 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002828 widget_set_allocation(widget,
2829 window->pending_allocation.x,
2830 window->pending_allocation.y,
2831 window->pending_allocation.width,
2832 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002833
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002834 if (window->input_region) {
2835 wl_region_destroy(window->input_region);
2836 window->input_region = NULL;
2837 }
2838
2839 if (window->opaque_region) {
2840 wl_region_destroy(window->opaque_region);
2841 window->opaque_region = NULL;
2842 }
2843
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002844 if (widget->resize_handler)
2845 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002846 widget->allocation.width,
2847 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002848 widget->user_data);
2849
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002850 if (window->allocation.width != widget->allocation.width ||
2851 window->allocation.height != widget->allocation.height) {
2852 window->allocation = widget->allocation;
2853 window_schedule_redraw(window);
2854 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002855}
2856
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002857void
2858window_schedule_resize(struct window *window, int width, int height)
2859{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002860 window->pending_allocation.x = 0;
2861 window->pending_allocation.y = 0;
2862 window->pending_allocation.width = width;
2863 window->pending_allocation.height = height;
2864
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002865 if (window->min_allocation.width == 0)
2866 window->min_allocation = window->pending_allocation;
2867 if (window->pending_allocation.width < window->min_allocation.width)
2868 window->pending_allocation.width = window->min_allocation.width;
2869 if (window->pending_allocation.height < window->min_allocation.height)
2870 window->pending_allocation.height = window->min_allocation.height;
2871
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002872 window->resize_needed = 1;
2873 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002874}
2875
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002876void
2877widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2878{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002879 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002880}
2881
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002882static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002883handle_ping(void *data, struct wl_shell_surface *shell_surface,
2884 uint32_t serial)
2885{
2886 wl_shell_surface_pong(shell_surface, serial);
2887}
2888
2889static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002890handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002891 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002892{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002893 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002894
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002895 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002896 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002897}
2898
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002899static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002900menu_destroy(struct menu *menu)
2901{
2902 widget_destroy(menu->widget);
2903 window_destroy(menu->window);
2904 free(menu);
2905}
2906
2907static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002908handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2909{
2910 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002911 struct menu *menu = window->widget->user_data;
2912
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002913 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002914 * device. Or just use wl_callback. And this really needs to
2915 * be a window vfunc that the menu can set. And we need the
2916 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002918 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002919 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002920 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921}
2922
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002923static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002924 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002925 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002926 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002927};
2928
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002929void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002930window_get_allocation(struct window *window,
2931 struct rectangle *allocation)
2932{
2933 *allocation = window->allocation;
2934}
2935
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002936static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002937widget_redraw(struct widget *widget)
2938{
2939 struct widget *child;
2940
2941 if (widget->redraw_handler)
2942 widget->redraw_handler(widget, widget->user_data);
2943 wl_list_for_each(child, &widget->child_list, link)
2944 widget_redraw(child);
2945}
2946
2947static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002948frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2949{
2950 struct window *window = data;
2951
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002952 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002953 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002954 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002955 window->redraw_scheduled = 0;
2956 if (window->redraw_needed)
2957 window_schedule_redraw(window);
2958}
2959
2960static const struct wl_callback_listener listener = {
2961 frame_callback
2962};
2963
2964static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002965idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002966{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002967 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002968
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002969 if (window->resize_needed)
2970 idle_resize(window);
2971
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002972 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002973 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002974 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002975 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002976
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002977 window->frame_cb = wl_surface_frame(window->surface);
2978 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002979 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002980}
2981
2982void
2983window_schedule_redraw(struct window *window)
2984{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002985 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002986 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002987 window->redraw_task.run = idle_redraw;
2988 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002989 window->redraw_scheduled = 1;
2990 }
2991}
2992
Kristian Høgsberg1671e112012-10-10 11:36:24 -04002993int
2994window_is_fullscreen(struct window *window)
2995{
2996 return window->type == TYPE_FULLSCREEN;
2997}
2998
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002999void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003000window_set_fullscreen(struct window *window, int fullscreen)
3001{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003002 if (!window->display->shell)
3003 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003004
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003005 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003006 return;
3007
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003008 if (fullscreen) {
3009 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003010 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003011 wl_shell_surface_set_fullscreen(window->shell_surface,
3012 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3013 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003014 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003015 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003016 wl_shell_surface_set_toplevel(window->shell_surface);
3017 window_schedule_resize(window,
3018 window->saved_allocation.width,
3019 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003020 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003021}
3022
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003023int
3024window_is_maximized(struct window *window)
3025{
3026 return window->type == TYPE_MAXIMIZED;
3027}
3028
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003029void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003030window_set_maximized(struct window *window, int maximized)
3031{
3032 if (!window->display->shell)
3033 return;
3034
3035 if ((window->type == TYPE_MAXIMIZED) == maximized)
3036 return;
3037
3038 if (window->type == TYPE_TOPLEVEL) {
3039 window->saved_allocation = window->allocation;
3040 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3041 window->type = TYPE_MAXIMIZED;
3042 } else {
3043 wl_shell_surface_set_toplevel(window->shell_surface);
3044 window->type = TYPE_TOPLEVEL;
3045 window_schedule_resize(window,
3046 window->saved_allocation.width,
3047 window->saved_allocation.height);
3048 }
3049}
3050
3051void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003052window_set_user_data(struct window *window, void *data)
3053{
3054 window->user_data = data;
3055}
3056
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003057void *
3058window_get_user_data(struct window *window)
3059{
3060 return window->user_data;
3061}
3062
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003063void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003064window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003065 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003066{
3067 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003068}
3069
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003070void
3071window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003072 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003073{
3074 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003075}
3076
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003077void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003078window_set_data_handler(struct window *window, window_data_handler_t handler)
3079{
3080 window->data_handler = handler;
3081}
3082
3083void
3084window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3085{
3086 window->drop_handler = handler;
3087}
3088
3089void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003090window_set_close_handler(struct window *window,
3091 window_close_handler_t handler)
3092{
3093 window->close_handler = handler;
3094}
3095
3096void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003097window_set_fullscreen_handler(struct window *window,
3098 window_fullscreen_handler_t handler)
3099{
3100 window->fullscreen_handler = handler;
3101}
3102
3103void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003104window_set_title(struct window *window, const char *title)
3105{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003106 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003107 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003108 if (window->shell_surface)
3109 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003110}
3111
3112const char *
3113window_get_title(struct window *window)
3114{
3115 return window->title;
3116}
3117
3118void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003119window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3120{
3121 struct text_cursor_position *text_cursor_position =
3122 window->display->text_cursor_position;
3123
Scott Moreau9295ce02012-06-01 12:46:10 -06003124 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003125 return;
3126
3127 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003128 window->surface,
3129 wl_fixed_from_int(x),
3130 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003131}
3132
3133void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003134window_damage(struct window *window, int32_t x, int32_t y,
3135 int32_t width, int32_t height)
3136{
3137 wl_surface_damage(window->surface, x, y, width, height);
3138}
3139
Casey Dahlin9074db52012-04-19 22:50:09 -04003140static void
3141surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003142 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003143{
Rob Bradford7507b572012-05-15 17:55:34 +01003144 struct window *window = data;
3145 struct output *output;
3146 struct output *output_found = NULL;
3147 struct window_output *window_output;
3148
3149 wl_list_for_each(output, &window->display->output_list, link) {
3150 if (output->output == wl_output) {
3151 output_found = output;
3152 break;
3153 }
3154 }
3155
3156 if (!output_found)
3157 return;
3158
3159 window_output = malloc (sizeof *window_output);
3160 window_output->output = output_found;
3161
3162 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003163}
3164
3165static void
3166surface_leave(void *data,
3167 struct wl_surface *wl_surface, struct wl_output *output)
3168{
Rob Bradford7507b572012-05-15 17:55:34 +01003169 struct window *window = data;
3170 struct window_output *window_output;
3171 struct window_output *window_output_found = NULL;
3172
3173 wl_list_for_each(window_output, &window->window_output_list, link) {
3174 if (window_output->output->output == output) {
3175 window_output_found = window_output;
3176 break;
3177 }
3178 }
3179
3180 if (window_output_found) {
3181 wl_list_remove(&window_output_found->link);
3182 free(window_output_found);
3183 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003184}
3185
3186static const struct wl_surface_listener surface_listener = {
3187 surface_enter,
3188 surface_leave
3189};
3190
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003191static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003192window_create_internal(struct display *display,
3193 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003194{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003195 struct window *window;
3196
3197 window = malloc(sizeof *window);
3198 if (window == NULL)
3199 return NULL;
3200
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003201 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003202 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003203 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003204 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003205 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003206 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003207 window->shell_surface =
3208 wl_shell_get_shell_surface(display->shell,
3209 window->surface);
3210 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003211 window->allocation.x = 0;
3212 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003213 window->allocation.width = 0;
3214 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003215 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003216 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003217 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003218 window->input_region = NULL;
3219 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003220
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003221 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003222#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003223 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003224#else
3225 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3226#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003227 else
3228 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003229
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003230 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003231 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003232 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003233
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003234 if (window->shell_surface) {
3235 wl_shell_surface_set_user_data(window->shell_surface, window);
3236 wl_shell_surface_add_listener(window->shell_surface,
3237 &shell_surface_listener, window);
3238 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003239
Rob Bradford7507b572012-05-15 17:55:34 +01003240 wl_list_init (&window->window_output_list);
3241
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003242 return window;
3243}
3244
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003245struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003246window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003247{
3248 struct window *window;
3249
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003250 window = window_create_internal(display, NULL, TYPE_NONE);
3251 if (!window)
3252 return NULL;
3253
3254 return window;
3255}
3256
3257struct window *
3258window_create_custom(struct display *display)
3259{
3260 struct window *window;
3261
3262 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003263 if (!window)
3264 return NULL;
3265
3266 return window;
3267}
3268
3269struct window *
3270window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003271 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003272{
3273 struct window *window;
3274
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003275 window = window_create_internal(parent->display,
3276 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003277 if (!window)
3278 return NULL;
3279
3280 window->x = x;
3281 window->y = y;
3282
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003283 if (display->shell)
3284 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003285 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003286 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003287
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003288 return window;
3289}
3290
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003291static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003292menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003293{
3294 int next;
3295
3296 next = (sy - 8) / 20;
3297 if (menu->current != next) {
3298 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003299 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003300 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003301}
3302
3303static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003304menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003306 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003307{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003308 struct menu *menu = data;
3309
3310 if (widget == menu->widget)
3311 menu_set_item(data, y);
3312
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003313 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003314}
3315
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003316static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003317menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003318 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003320 struct menu *menu = data;
3321
3322 if (widget == menu->widget)
3323 menu_set_item(data, y);
3324
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003325 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003326}
3327
3328static void
3329menu_leave_handler(struct widget *widget, struct input *input, void *data)
3330{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003331 struct menu *menu = data;
3332
3333 if (widget == menu->widget)
3334 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003335}
3336
3337static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003338menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003339 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003340 uint32_t button, enum wl_pointer_button_state state,
3341 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003342
3343{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003344 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003345
Daniel Stone4dbadb12012-05-30 16:31:51 +01003346 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3347 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003348 /* Either relase after press-drag-release or
3349 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003350 menu->func(menu->window->parent,
3351 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003352 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003353 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003354 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003355}
3356
3357static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003358menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003359{
3360 cairo_t *cr;
3361 const int32_t r = 3, margin = 3;
3362 struct menu *menu = data;
3363 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003364 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003365
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366 cr = cairo_create(window->cairo_surface);
3367 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3368 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3369 cairo_paint(cr);
3370
3371 width = window->allocation.width;
3372 height = window->allocation.height;
3373 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003374
3375 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003376 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3377 cairo_fill(cr);
3378
3379 for (i = 0; i < menu->count; i++) {
3380 if (i == menu->current) {
3381 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3382 cairo_rectangle(cr, margin, i * 20 + margin,
3383 width - 2 * margin, 20);
3384 cairo_fill(cr);
3385 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3386 cairo_move_to(cr, 10, i * 20 + 16);
3387 cairo_show_text(cr, menu->entries[i]);
3388 } else {
3389 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3390 cairo_move_to(cr, 10, i * 20 + 16);
3391 cairo_show_text(cr, menu->entries[i]);
3392 }
3393 }
3394
3395 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003396}
3397
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003398void
3399window_show_menu(struct display *display,
3400 struct input *input, uint32_t time, struct window *parent,
3401 int32_t x, int32_t y,
3402 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003403{
3404 struct window *window;
3405 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003406 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003407
3408 menu = malloc(sizeof *menu);
3409 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003410 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003411
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003412 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003413 if (!window) {
3414 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003415 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003416 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003417
3418 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003419 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003420 menu->entries = entries;
3421 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003422 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003423 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003424 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003425 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003426 window->type = TYPE_MENU;
3427 window->x = x;
3428 window->y = y;
3429
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003430 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003431 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003432 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003433 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003434 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003435
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003436 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003437 widget_set_enter_handler(menu->widget, menu_enter_handler);
3438 widget_set_leave_handler(menu->widget, menu_leave_handler);
3439 widget_set_motion_handler(menu->widget, menu_motion_handler);
3440 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003441
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003442 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003443 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003444}
3445
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003446void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003447window_set_buffer_type(struct window *window, enum window_buffer_type type)
3448{
3449 window->buffer_type = type;
3450}
3451
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003452
3453static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003454display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003455 struct wl_output *wl_output,
3456 int x, int y,
3457 int physical_width,
3458 int physical_height,
3459 int subpixel,
3460 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003461 const char *model,
3462 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003463{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003464 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003465
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003466 output->allocation.x = x;
3467 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003468 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003469}
3470
3471static void
3472display_handle_mode(void *data,
3473 struct wl_output *wl_output,
3474 uint32_t flags,
3475 int width,
3476 int height,
3477 int refresh)
3478{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003479 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003480 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003481
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003482 if (flags & WL_OUTPUT_MODE_CURRENT) {
3483 output->allocation.width = width;
3484 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003485 if (display->output_configure_handler)
3486 (*display->output_configure_handler)(
3487 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003488 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003489}
3490
3491static const struct wl_output_listener output_listener = {
3492 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003493 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003494};
3495
3496static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003497display_add_output(struct display *d, uint32_t id)
3498{
3499 struct output *output;
3500
3501 output = malloc(sizeof *output);
3502 if (output == NULL)
3503 return;
3504
3505 memset(output, 0, sizeof *output);
3506 output->display = d;
3507 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003508 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003509 wl_list_insert(d->output_list.prev, &output->link);
3510
3511 wl_output_add_listener(output->output, &output_listener, output);
3512}
3513
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003514static void
3515output_destroy(struct output *output)
3516{
3517 if (output->destroy_handler)
3518 (*output->destroy_handler)(output, output->user_data);
3519
3520 wl_output_destroy(output->output);
3521 wl_list_remove(&output->link);
3522 free(output);
3523}
3524
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003525void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003526display_set_global_handler(struct display *display,
3527 display_global_handler_t handler)
3528{
3529 struct global *global;
3530
3531 display->global_handler = handler;
3532 if (!handler)
3533 return;
3534
3535 wl_list_for_each(global, &display->global_list, link)
3536 display->global_handler(display,
3537 global->name, global->interface,
3538 global->version, display->user_data);
3539}
3540
3541void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003542display_set_output_configure_handler(struct display *display,
3543 display_output_handler_t handler)
3544{
3545 struct output *output;
3546
3547 display->output_configure_handler = handler;
3548 if (!handler)
3549 return;
3550
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003551 wl_list_for_each(output, &display->output_list, link) {
3552 if (output->allocation.width == 0 &&
3553 output->allocation.height == 0)
3554 continue;
3555
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003556 (*display->output_configure_handler)(output,
3557 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003558 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003559}
3560
3561void
3562output_set_user_data(struct output *output, void *data)
3563{
3564 output->user_data = data;
3565}
3566
3567void *
3568output_get_user_data(struct output *output)
3569{
3570 return output->user_data;
3571}
3572
3573void
3574output_set_destroy_handler(struct output *output,
3575 display_output_handler_t handler)
3576{
3577 output->destroy_handler = handler;
3578 /* FIXME: implement this, once we have way to remove outputs */
3579}
3580
3581void
Scott Moreau4e072362012-09-29 02:03:11 -06003582output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003583{
Scott Moreau4e072362012-09-29 02:03:11 -06003584 struct rectangle allocation = output->allocation;
3585
3586 switch (output->transform) {
3587 case WL_OUTPUT_TRANSFORM_90:
3588 case WL_OUTPUT_TRANSFORM_270:
3589 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3590 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3591 /* Swap width and height */
3592 allocation.width = output->allocation.height;
3593 allocation.height = output->allocation.width;
3594 break;
3595 }
3596
3597 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003598}
3599
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003600struct wl_output *
3601output_get_wl_output(struct output *output)
3602{
3603 return output->output;
3604}
3605
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003606static void
Daniel Stone97f68542012-05-30 16:32:01 +01003607fini_xkb(struct input *input)
3608{
3609 xkb_state_unref(input->xkb.state);
3610 xkb_map_unref(input->xkb.keymap);
3611}
3612
3613static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003614display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003615{
3616 struct input *input;
3617
3618 input = malloc(sizeof *input);
3619 if (input == NULL)
3620 return;
3621
3622 memset(input, 0, sizeof *input);
3623 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003624 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003625 input->pointer_focus = NULL;
3626 input->keyboard_focus = NULL;
3627 wl_list_insert(d->input_list.prev, &input->link);
3628
Daniel Stone37816df2012-05-16 18:45:18 +01003629 wl_seat_add_listener(input->seat, &seat_listener, input);
3630 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003631
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003632 input->data_device =
3633 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003634 input->seat);
3635 wl_data_device_add_listener(input->data_device, &data_device_listener,
3636 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003637
3638 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003639
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003640 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3641 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003642 input->repeat_task.run = keyboard_repeat_func;
3643 display_watch_fd(d, input->repeat_timer_fd,
3644 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003645}
3646
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003647static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003648input_destroy(struct input *input)
3649{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003650 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003651 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003652
3653 if (input->drag_offer)
3654 data_offer_destroy(input->drag_offer);
3655
3656 if (input->selection_offer)
3657 data_offer_destroy(input->selection_offer);
3658
3659 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003660 fini_xkb(input);
3661
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003662 wl_surface_destroy(input->pointer_surface);
3663
Pekka Paalanene1207c72011-12-16 12:02:09 +02003664 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003665 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003666 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003667 free(input);
3668}
3669
3670static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003671init_workspace_manager(struct display *d, uint32_t id)
3672{
3673 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003674 wl_registry_bind(d->registry, id,
3675 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003676 if (d->workspace_manager != NULL)
3677 workspace_manager_add_listener(d->workspace_manager,
3678 &workspace_manager_listener,
3679 d);
3680}
3681
3682static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003683registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3684 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003685{
3686 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003687 struct global *global;
3688
3689 global = malloc(sizeof *global);
3690 global->name = id;
3691 global->interface = strdup(interface);
3692 global->version = version;
3693 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003694
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003695 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003696 d->compositor = wl_registry_bind(registry, id,
3697 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003698 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003699 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003700 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003701 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003702 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003703 d->shell = wl_registry_bind(registry,
3704 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003705 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003706 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003707 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3708 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003709 wl_registry_bind(registry, id,
3710 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003711 } else if (strcmp(interface, "text_cursor_position") == 0) {
3712 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003713 wl_registry_bind(registry, id,
3714 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003715 } else if (strcmp(interface, "workspace_manager") == 0) {
3716 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003717 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003718
3719 if (d->global_handler)
3720 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003721}
3722
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003723void *
3724display_bind(struct display *display, uint32_t name,
3725 const struct wl_interface *interface, uint32_t version)
3726{
3727 return wl_registry_bind(display->registry, name, interface, version);
3728}
3729
3730static const struct wl_registry_listener registry_listener = {
3731 registry_handle_global
3732};
3733
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003734#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003735static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003736init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003737{
3738 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003739 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003740
Rob Clark6396ed32012-03-11 19:48:41 -05003741#ifdef USE_CAIRO_GLESV2
3742# define GL_BIT EGL_OPENGL_ES2_BIT
3743#else
3744# define GL_BIT EGL_OPENGL_BIT
3745#endif
3746
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003747 static const EGLint argb_cfg_attribs[] = {
3748 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003749 EGL_RED_SIZE, 1,
3750 EGL_GREEN_SIZE, 1,
3751 EGL_BLUE_SIZE, 1,
3752 EGL_ALPHA_SIZE, 1,
3753 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003754 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003755 EGL_NONE
3756 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003757
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003758#ifdef USE_CAIRO_GLESV2
3759 static const EGLint context_attribs[] = {
3760 EGL_CONTEXT_CLIENT_VERSION, 2,
3761 EGL_NONE
3762 };
3763 EGLint api = EGL_OPENGL_ES_API;
3764#else
3765 EGLint *context_attribs = NULL;
3766 EGLint api = EGL_OPENGL_API;
3767#endif
3768
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003769 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003770 if (!eglInitialize(d->dpy, &major, &minor)) {
3771 fprintf(stderr, "failed to initialize display\n");
3772 return -1;
3773 }
3774
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003775 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003776 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3777 return -1;
3778 }
3779
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003780 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3781 &d->argb_config, 1, &n) || n != 1) {
3782 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003783 return -1;
3784 }
3785
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003786 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003787 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003788 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003789 fprintf(stderr, "failed to create context\n");
3790 return -1;
3791 }
3792
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003793 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003794 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003795 return -1;
3796 }
3797
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003798#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003799 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3800 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3801 fprintf(stderr, "failed to get cairo egl argb device\n");
3802 return -1;
3803 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003804#endif
3805
3806 return 0;
3807}
3808
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003809static void
3810fini_egl(struct display *display)
3811{
3812#ifdef HAVE_CAIRO_EGL
3813 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003814#endif
3815
3816 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3817 EGL_NO_CONTEXT);
3818
3819 eglTerminate(display->dpy);
3820 eglReleaseThread();
3821}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003822#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003823
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003824static void
3825handle_display_data(struct task *task, uint32_t events)
3826{
3827 struct display *display =
3828 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003829 struct epoll_event ep;
3830 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003831
3832 display->display_fd_events = events;
3833
3834 if (events & EPOLLERR || events & EPOLLHUP) {
3835 display_exit(display);
3836 return;
3837 }
3838
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003839 if (events & EPOLLIN)
3840 wl_display_dispatch(display->display);
3841
3842 if (events & EPOLLOUT) {
3843 ret = wl_display_flush(display->display);
3844 if (ret == 0) {
3845 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3846 ep.data.ptr = &display->display_task;
3847 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3848 display->display_fd, &ep);
3849 } else if (ret == -1 && errno != EAGAIN) {
3850 display_exit(display);
3851 return;
3852 }
3853 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003854}
3855
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003856struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003857display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003858{
3859 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003860
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003861 d = malloc(sizeof *d);
3862 if (d == NULL)
3863 return NULL;
3864
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003865 memset(d, 0, sizeof *d);
3866
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003867 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003868 if (d->display == NULL) {
3869 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003870 return NULL;
3871 }
3872
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003873 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003874 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003875 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003876 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3877 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003878
3879 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003880 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003881 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003882 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003883
Daniel Stone97f68542012-05-30 16:32:01 +01003884 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003885 if (d->xkb_context == NULL) {
3886 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003887 return NULL;
3888 }
3889
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003890 d->workspace = 0;
3891 d->workspace_count = 1;
3892
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003893 d->registry = wl_display_get_registry(d->display);
3894 wl_registry_add_listener(d->registry, &registry_listener, d);
3895 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003896#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003897 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003898 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003899#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003900
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003901 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003902
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003903 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003904
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003905 wl_list_init(&d->window_list);
3906
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003907 return d;
3908}
3909
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003910static void
3911display_destroy_outputs(struct display *display)
3912{
3913 struct output *tmp;
3914 struct output *output;
3915
3916 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3917 output_destroy(output);
3918}
3919
Pekka Paalanene1207c72011-12-16 12:02:09 +02003920static void
3921display_destroy_inputs(struct display *display)
3922{
3923 struct input *tmp;
3924 struct input *input;
3925
3926 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3927 input_destroy(input);
3928}
3929
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003930void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003931display_destroy(struct display *display)
3932{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003933 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003934 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3935 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003936
3937 if (!wl_list_empty(&display->deferred_list))
3938 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3939
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003940 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003941 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003942
Daniel Stone97f68542012-05-30 16:32:01 +01003943 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003944
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003945 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003946 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003947
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003948#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003949 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003950#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003951
Pekka Paalanenc2052982011-12-16 11:41:32 +02003952 if (display->shell)
3953 wl_shell_destroy(display->shell);
3954
3955 if (display->shm)
3956 wl_shm_destroy(display->shm);
3957
3958 if (display->data_device_manager)
3959 wl_data_device_manager_destroy(display->data_device_manager);
3960
3961 wl_compositor_destroy(display->compositor);
3962
3963 close(display->epoll_fd);
3964
U. Artie Eoff44874d92012-10-02 21:12:35 -07003965 if (!(display->display_fd_events & EPOLLERR) &&
3966 !(display->display_fd_events & EPOLLHUP))
3967 wl_display_flush(display->display);
3968
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003969 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003970 free(display);
3971}
3972
3973void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003974display_set_user_data(struct display *display, void *data)
3975{
3976 display->user_data = data;
3977}
3978
3979void *
3980display_get_user_data(struct display *display)
3981{
3982 return display->user_data;
3983}
3984
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003985struct wl_display *
3986display_get_display(struct display *display)
3987{
3988 return display->display;
3989}
3990
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003991struct output *
3992display_get_output(struct display *display)
3993{
3994 return container_of(display->output_list.next, struct output, link);
3995}
3996
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003997struct wl_compositor *
3998display_get_compositor(struct display *display)
3999{
4000 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004001}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004002
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004003uint32_t
4004display_get_serial(struct display *display)
4005{
4006 return display->serial;
4007}
4008
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004009EGLDisplay
4010display_get_egl_display(struct display *d)
4011{
4012 return d->dpy;
4013}
4014
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004015struct wl_data_source *
4016display_create_data_source(struct display *display)
4017{
4018 return wl_data_device_manager_create_data_source(display->data_device_manager);
4019}
4020
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004021EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004022display_get_argb_egl_config(struct display *d)
4023{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004024 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004025}
4026
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004027struct wl_shell *
4028display_get_shell(struct display *display)
4029{
4030 return display->shell;
4031}
4032
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004033int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004034display_acquire_window_surface(struct display *display,
4035 struct window *window,
4036 EGLContext ctx)
4037{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004038#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004039 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004040 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004041
4042 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004043 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004044 device = cairo_surface_get_device(window->cairo_surface);
4045 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004046 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004047
Benjamin Franzke0c991632011-09-27 21:57:31 +02004048 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004049 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004050 ctx = display->argb_ctx;
4051 else
4052 assert(0);
4053 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004054
4055 data = cairo_surface_get_user_data(window->cairo_surface,
4056 &surface_data_key);
4057
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004058 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004059 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004060 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4061 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004062
4063 return 0;
4064#else
4065 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004066#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004067}
4068
4069void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004070display_release_window_surface(struct display *display,
4071 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004072{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004073#ifdef HAVE_CAIRO_EGL
4074 cairo_device_t *device;
4075
4076 device = cairo_surface_get_device(window->cairo_surface);
4077 if (!device)
4078 return;
4079
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004080 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004081 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004082 cairo_device_release(device);
4083#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004084}
4085
4086void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004087display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004088{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004089 wl_list_insert(&display->deferred_list, &task->link);
4090}
4091
4092void
4093display_watch_fd(struct display *display,
4094 int fd, uint32_t events, struct task *task)
4095{
4096 struct epoll_event ep;
4097
4098 ep.events = events;
4099 ep.data.ptr = task;
4100 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4101}
4102
4103void
4104display_run(struct display *display)
4105{
4106 struct task *task;
4107 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004108 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004109
Pekka Paalanen826d7952011-12-15 10:14:07 +02004110 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004111 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004112 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004113 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004114 struct task, link);
4115 wl_list_remove(&task->link);
4116 task->run(task, 0);
4117 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004118
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004119 wl_display_dispatch_pending(display->display);
4120
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004121 if (!display->running)
4122 break;
4123
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004124 ret = wl_display_flush(display->display);
4125 if (ret < 0 && errno == EAGAIN) {
4126 ep[0].events =
4127 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4128 ep[0].data.ptr = &display->display_task;
4129
4130 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4131 display->display_fd, &ep[0]);
4132 } else if (ret < 0) {
4133 break;
4134 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004135
4136 count = epoll_wait(display->epoll_fd,
4137 ep, ARRAY_LENGTH(ep), -1);
4138 for (i = 0; i < count; i++) {
4139 task = ep[i].data.ptr;
4140 task->run(task, ep[i].events);
4141 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004142 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004143}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004144
4145void
4146display_exit(struct display *display)
4147{
4148 display->running = 0;
4149}