blob: 9bc9e1921f66099a4b8a03ab8c0f48689b22371c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040042#include <pixman.h>
43
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050044#include <wayland-egl.h>
45
Rob Clark6396ed32012-03-11 19:48:41 -050046#ifdef USE_CAIRO_GLESV2
47#include <GLES2/gl2.h>
48#include <GLES2/gl2ext.h>
49#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050051#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <EGL/egl.h>
53#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040054
Kristian Høgsberg8def2642011-01-14 17:41:33 -050055#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040057#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050058
Daniel Stone9d4f0302012-02-15 16:33:21 +000059#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030060#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040061
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020063#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040064#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060065#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020066#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030067#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050069#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050070
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070071struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030072
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040073struct global {
74 uint32_t name;
75 char *interface;
76 uint32_t version;
77 struct wl_list link;
78};
79
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050080struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050081 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040082 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050083 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040084 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040085 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040086 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060087 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020088 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040089 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050090 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020091 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020092 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040093 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040094
95 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -070096 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040097 struct task display_task;
98
99 int epoll_fd;
100 struct wl_list deferred_list;
101
Pekka Paalanen826d7952011-12-15 10:14:07 +0200102 int running;
103
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400104 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400105 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400106 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500107 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400108
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400109 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400110
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300111 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300112 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400113
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500114 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
115 PFNEGLCREATEIMAGEKHRPROC create_image;
116 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200117
118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500127};
128
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400130 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400131 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400132 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500133 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400134 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500135 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136 TYPE_CUSTOM
137};
Rob Bradford7507b572012-05-15 17:55:34 +0100138
139struct window_output {
140 struct output *output;
141 struct wl_list link;
142};
143
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500144struct window {
145 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500146 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100147 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500148 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200149 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500150 struct wl_region *input_region;
151 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500152 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500153 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400154 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500155 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500156 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400157 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400158 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400159 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400160 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400161 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400162 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400163 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400164 int focus_count;
165
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400166 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500167
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400168 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500169
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700170 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400171
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500172 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500173 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400174 window_data_handler_t data_handler;
175 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500176 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400177 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400178
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300179 struct wl_callback *frame_cb;
180
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200181 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500182 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500183
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500184 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400185 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500186};
187
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500188struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500189 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300190 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500191 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400192 struct wl_list link;
193 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194 widget_resize_handler_t resize_handler;
195 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500196 widget_enter_handler_t enter_handler;
197 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500198 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500199 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200200 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400201 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500202 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300203 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400204};
205
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400206struct input {
207 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100208 struct wl_seat *seat;
209 struct wl_pointer *pointer;
210 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400211 struct window *pointer_focus;
212 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300213 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300214 uint32_t cursor_anim_start;
215 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300216 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400217 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400218 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400219 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400220 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400221 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400222
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500223 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500224 struct widget *grab;
225 uint32_t grab_button;
226
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400227 struct wl_data_device *data_device;
228 struct data_offer *drag_offer;
229 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100230
231 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100232 struct xkb_keymap *keymap;
233 struct xkb_state *state;
234 xkb_mod_mask_t control_mask;
235 xkb_mod_mask_t alt_mask;
236 xkb_mod_mask_t shift_mask;
237 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400238
239 struct task repeat_task;
240 int repeat_timer_fd;
241 uint32_t repeat_sym;
242 uint32_t repeat_key;
243 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400244};
245
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500246struct output {
247 struct display *display;
248 struct wl_output *output;
249 struct rectangle allocation;
250 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600251 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200252
253 display_output_handler_t destroy_handler;
254 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500255};
256
Martin Minarik1998b152012-05-10 02:04:35 +0200257enum frame_button_action {
258 FRAME_BUTTON_NULL = 0,
259 FRAME_BUTTON_ICON = 1,
260 FRAME_BUTTON_CLOSE = 2,
261 FRAME_BUTTON_MINIMIZE = 3,
262 FRAME_BUTTON_MAXIMIZE = 4,
263};
264
265enum frame_button_pointer {
266 FRAME_BUTTON_DEFAULT = 0,
267 FRAME_BUTTON_OVER = 1,
268 FRAME_BUTTON_ACTIVE = 2,
269};
270
271enum frame_button_align {
272 FRAME_BUTTON_RIGHT = 0,
273 FRAME_BUTTON_LEFT = 1,
274};
275
276enum frame_button_decoration {
277 FRAME_BUTTON_NONE = 0,
278 FRAME_BUTTON_FANCY = 1,
279};
280
281struct frame_button {
282 struct widget *widget;
283 struct frame *frame;
284 cairo_surface_t *icon;
285 enum frame_button_action type;
286 enum frame_button_pointer state;
287 struct wl_list link; /* buttons_list */
288 enum frame_button_align align;
289 enum frame_button_decoration decoration;
290};
291
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500292struct frame {
293 struct widget *widget;
294 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200295 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500296};
297
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500298struct menu {
299 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500300 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500301 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500302 const char **entries;
303 uint32_t time;
304 int current;
305 int count;
306 menu_func_t func;
307};
308
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300309struct tooltip {
310 struct widget *parent;
311 struct window *window;
312 struct widget *widget;
313 char *entry;
314 struct task tooltip_task;
315 int tooltip_fd;
316 float x, y;
317};
318
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700319struct shm_pool {
320 struct wl_shm_pool *pool;
321 size_t size;
322 size_t used;
323 void *data;
324};
325
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400326enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300327 CURSOR_DEFAULT = 100,
328 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400329};
330
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500331enum window_location {
332 WINDOW_INTERIOR = 0,
333 WINDOW_RESIZING_TOP = 1,
334 WINDOW_RESIZING_BOTTOM = 2,
335 WINDOW_RESIZING_LEFT = 4,
336 WINDOW_RESIZING_TOP_LEFT = 5,
337 WINDOW_RESIZING_BOTTOM_LEFT = 6,
338 WINDOW_RESIZING_RIGHT = 8,
339 WINDOW_RESIZING_TOP_RIGHT = 9,
340 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
341 WINDOW_RESIZING_MASK = 15,
342 WINDOW_EXTERIOR = 16,
343 WINDOW_TITLEBAR = 17,
344 WINDOW_CLIENT_AREA = 18,
345};
346
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400347static const cairo_user_data_key_t surface_data_key;
348struct surface_data {
349 struct wl_buffer *buffer;
350};
351
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500352#define MULT(_d,c,a,t) \
353 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
354
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500355#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100357struct egl_window_surface_data {
358 struct display *display;
359 struct wl_surface *surface;
360 struct wl_egl_window *window;
361 EGLSurface surf;
362};
363
364static void
365egl_window_surface_data_destroy(void *p)
366{
367 struct egl_window_surface_data *data = p;
368 struct display *d = data->display;
369
370 eglDestroySurface(d->dpy, data->surf);
371 wl_egl_window_destroy(data->window);
372 data->surface = NULL;
373
374 free(p);
375}
376
377static cairo_surface_t *
378display_create_egl_window_surface(struct display *display,
379 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381 struct rectangle *rectangle)
382{
383 cairo_surface_t *cairo_surface;
384 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200386 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387
388 data = malloc(sizeof *data);
389 if (data == NULL)
390 return NULL;
391
392 data->display = display;
393 data->surface = surface;
394
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500395 config = display->argb_config;
396 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400397
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400398 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100399 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400400 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100401
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400402 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500403 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100404
Benjamin Franzke0c991632011-09-27 21:57:31 +0200405 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100406 data->surf,
407 rectangle->width,
408 rectangle->height);
409
410 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
411 data, egl_window_surface_data_destroy);
412
413 return cairo_surface;
414}
415
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416#endif
417
418struct wl_buffer *
419display_get_buffer_for_surface(struct display *display,
420 cairo_surface_t *surface)
421{
422 struct surface_data *data;
423
424 data = cairo_surface_get_user_data (surface, &surface_data_key);
425
426 return data->buffer;
427}
428
429struct shm_surface_data {
430 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432};
433
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500434static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700435shm_pool_destroy(struct shm_pool *pool);
436
437static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438shm_surface_data_destroy(void *p)
439{
440 struct shm_surface_data *data = p;
441
442 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700443 if (data->pool)
444 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300445
446 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447}
448
Kristian Høgsberg16626282012-04-03 11:21:27 -0400449static struct wl_shm_pool *
450make_shm_pool(struct display *display, int size, void **data)
451{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 struct wl_shm_pool *pool;
453 int fd;
454
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300455 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400456 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300457 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
458 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400459 return NULL;
460 }
461
462 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400463 if (*data == MAP_FAILED) {
464 fprintf(stderr, "mmap failed: %m\n");
465 close(fd);
466 return NULL;
467 }
468
469 pool = wl_shm_create_pool(display->shm, fd, size);
470
471 close(fd);
472
473 return pool;
474}
475
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700476static struct shm_pool *
477shm_pool_create(struct display *display, size_t size)
478{
479 struct shm_pool *pool = malloc(sizeof *pool);
480
481 if (!pool)
482 return NULL;
483
484 pool->pool = make_shm_pool(display, size, &pool->data);
485 if (!pool->pool) {
486 free(pool);
487 return NULL;
488 }
489
490 pool->size = size;
491 pool->used = 0;
492
493 return pool;
494}
495
496static void *
497shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
498{
499 if (pool->used + size > pool->size)
500 return NULL;
501
502 *offset = pool->used;
503 pool->used += size;
504
505 return (char *) pool->data + *offset;
506}
507
508/* destroy the pool. this does not unmap the memory though */
509static void
510shm_pool_destroy(struct shm_pool *pool)
511{
512 munmap(pool->data, pool->size);
513 wl_shm_pool_destroy(pool->pool);
514 free(pool);
515}
516
517/* Start allocating from the beginning of the pool again */
518static void
519shm_pool_reset(struct shm_pool *pool)
520{
521 pool->used = 0;
522}
523
524static int
525data_length_for_shm_surface(struct rectangle *rect)
526{
527 int stride;
528
529 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530 rect->width);
531 return stride * rect->height;
532}
533
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500534static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700535display_create_shm_surface_from_pool(struct display *display,
536 struct rectangle *rectangle,
537 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538{
539 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400540 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400541 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700542 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400543 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
545 data = malloc(sizeof *data);
546 if (data == NULL)
547 return NULL;
548
549 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
550 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700551 length = stride * rectangle->height;
552 data->pool = NULL;
553 map = shm_pool_allocate(pool, length, &offset);
554
555 if (!map) {
556 free(data);
557 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400558 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400560 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561 CAIRO_FORMAT_ARGB32,
562 rectangle->width,
563 rectangle->height,
564 stride);
565
566 cairo_surface_set_user_data (surface, &surface_data_key,
567 data, shm_surface_data_destroy);
568
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500570 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400571 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500572 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400573
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700574 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400575 rectangle->width,
576 rectangle->height,
577 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700579 return surface;
580}
581
582static cairo_surface_t *
583display_create_shm_surface(struct display *display,
584 struct rectangle *rectangle, uint32_t flags,
585 struct window *window)
586{
587 struct shm_surface_data *data;
588 struct shm_pool *pool;
589 cairo_surface_t *surface;
590
591 if (window && window->pool) {
592 shm_pool_reset(window->pool);
593 surface = display_create_shm_surface_from_pool(display,
594 rectangle,
595 flags,
596 window->pool);
597 if (surface)
598 return surface;
599 }
600
601 pool = shm_pool_create(display,
602 data_length_for_shm_surface(rectangle));
603 if (!pool)
604 return NULL;
605
606 surface =
607 display_create_shm_surface_from_pool(display, rectangle,
608 flags, pool);
609
610 if (!surface) {
611 shm_pool_destroy(pool);
612 return NULL;
613 }
614
615 /* make sure we destroy the pool when the surface is destroyed */
616 data = cairo_surface_get_user_data(surface, &surface_data_key);
617 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618
619 return surface;
620}
621
nobled7b87cb02011-02-01 18:51:47 +0000622static int
623check_size(struct rectangle *rect)
624{
625 if (rect->width && rect->height)
626 return 0;
627
628 fprintf(stderr, "tried to create surface of "
629 "width: %d, height: %d\n", rect->width, rect->height);
630 return -1;
631}
632
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633cairo_surface_t *
634display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400636 struct rectangle *rectangle,
637 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638{
nobled7b87cb02011-02-01 18:51:47 +0000639 if (check_size(rectangle) < 0)
640 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300642 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400643 return display_create_egl_window_surface(display,
644 surface,
645 flags,
646 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400648 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649}
650
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200651/*
652 * The following correspondences between file names and cursors was copied
653 * from: https://bugs.kde.org/attachment.cgi?id=67313
654 */
655
656static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300657 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200658 "sw-resize"
659};
660
661static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300662 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200663 "se-resize"
664};
665
666static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300667 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200668 "s-resize"
669};
670
671static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300672 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200673 "closedhand",
674 "208530c400c041818281048008011002"
675};
676
677static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300678 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200679 "default",
680 "top_left_arrow",
681 "left-arrow"
682};
683
684static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300685 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200686 "w-resize"
687};
688
689static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300690 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200691 "e-resize"
692};
693
694static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300695 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200696 "nw-resize"
697};
698
699static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300700 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200701 "ne-resize"
702};
703
704static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300705 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200706 "n-resize"
707};
708
709static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300710 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200711 "ibeam",
712 "text"
713};
714
715static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300716 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200717 "pointer",
718 "pointing_hand",
719 "e29285e634086352946a0e7090d73106"
720};
721
722static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400723 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200724 "wait",
725 "0426c94ea35c87780ff01dc239897213"
726};
727
728struct cursor_alternatives {
729 const char **names;
730 size_t count;
731};
732
733static const struct cursor_alternatives cursors[] = {
734 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
735 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
736 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
737 {grabbings, ARRAY_LENGTH(grabbings)},
738 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
739 {left_sides, ARRAY_LENGTH(left_sides)},
740 {right_sides, ARRAY_LENGTH(right_sides)},
741 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
742 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
743 {top_sides, ARRAY_LENGTH(top_sides)},
744 {xterms, ARRAY_LENGTH(xterms)},
745 {hand1s, ARRAY_LENGTH(hand1s)},
746 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300747};
748
749static void
750create_cursors(struct display *display)
751{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100752 char *config_file;
753 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200754 unsigned int i, j;
755 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100756 struct config_key shell_keys[] = {
757 { "cursor-theme", CONFIG_KEY_STRING, &theme },
758 };
759 struct config_section cs[] = {
760 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
761 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300762
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100763 config_file = config_file_path("weston.ini");
764 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
765 free(config_file);
766
767 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300768 display->cursors =
769 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
770
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300771 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200772 cursor = NULL;
773 for (j = 0; !cursor && j < cursors[i].count; ++j)
774 cursor = wl_cursor_theme_get_cursor(
775 display->cursor_theme, cursors[i].names[j]);
776
777 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300778 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200779 cursors[i].names[0]);
780
781 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300782 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300783}
784
785static void
786destroy_cursors(struct display *display)
787{
788 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800789 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300790}
791
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300792struct wl_cursor_image *
793display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400794{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200795 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400796
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300797 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400798}
799
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400800static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200801window_get_resize_dx_dy(struct window *window, int *x, int *y)
802{
803 if (window->resize_edges & WINDOW_RESIZING_LEFT)
804 *x = window->server_allocation.width - window->allocation.width;
805 else
806 *x = 0;
807
808 if (window->resize_edges & WINDOW_RESIZING_TOP)
809 *y = window->server_allocation.height -
810 window->allocation.height;
811 else
812 *y = 0;
813
814 window->resize_edges = 0;
815}
816
817static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500818window_attach_surface(struct window *window)
819{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400820 struct display *display = window->display;
821 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500825 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400827 if (window->type == TYPE_NONE) {
828 window->type = TYPE_TOPLEVEL;
829 if (display->shell)
830 wl_shell_surface_set_toplevel(window->shell_surface);
831 }
832
Pekka Paalanen512dde82012-10-10 12:49:27 +0300833 if (window->opaque_region) {
834 wl_surface_set_opaque_region(window->surface,
835 window->opaque_region);
836 wl_region_destroy(window->opaque_region);
837 window->opaque_region = NULL;
838 }
839
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300840 if (window->input_region) {
841 wl_surface_set_input_region(window->surface,
842 window->input_region);
843 wl_region_destroy(window->input_region);
844 window->input_region = NULL;
845 }
846
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000848#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
850 data = cairo_surface_get_user_data(window->cairo_surface,
851 &surface_data_key);
852
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 cairo_gl_surface_swapbuffers(window->cairo_surface);
854 wl_egl_window_get_attached_size(data->window,
855 &window->server_allocation.width,
856 &window->server_allocation.height);
857 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000858#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 buffer =
861 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400862 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200864 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100865 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400866 wl_surface_damage(window->surface, 0, 0,
867 window->allocation.width,
868 window->allocation.height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300869 wl_surface_commit(window->surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100870 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400871 cairo_surface_destroy(window->cairo_surface);
872 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100873 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000874 default:
875 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100876 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500877}
878
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400879int
880window_has_focus(struct window *window)
881{
882 return window->focus_count > 0;
883}
884
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500885void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400886window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400888 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100889 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500890}
891
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892void
893window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500895 cairo_surface_reference(surface);
896
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400897 if (window->cairo_surface != NULL)
898 cairo_surface_destroy(window->cairo_surface);
899
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500900 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901}
902
Benjamin Franzke22d54812011-07-16 19:50:32 +0000903#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904static void
905window_resize_cairo_window_surface(struct window *window)
906{
907 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909
910 data = cairo_surface_get_user_data(window->cairo_surface,
911 &surface_data_key);
912
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200913 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914 wl_egl_window_resize(data->window,
915 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200916 window->allocation.height,
917 x,y);
918
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919 cairo_gl_surface_set_size(window->cairo_surface,
920 window->allocation.width,
921 window->allocation.height);
922}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000923#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100924
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400925struct display *
926window_get_display(struct window *window)
927{
928 return window->display;
929}
930
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400931void
932window_create_surface(struct window *window)
933{
934 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400935 uint32_t flags = 0;
936
937 if (!window->transparent)
938 flags = SURFACE_OPAQUE;
939
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500941#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
943 if (window->cairo_surface) {
944 window_resize_cairo_window_surface(window);
945 return;
946 }
947 surface = display_create_surface(window->display,
948 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951#endif
952 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400954 &window->allocation,
955 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800957 default:
958 surface = NULL;
959 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400961
962 window_set_surface(window, surface);
963 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400964}
965
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200966static void frame_destroy(struct frame *frame);
967
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400968void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500969window_destroy(struct window *window)
970{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971 struct display *display = window->display;
972 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100973 struct window_output *window_output;
974 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200975
976 if (window->redraw_scheduled)
977 wl_list_remove(&window->redraw_task.link);
978
979 wl_list_for_each(input, &display->input_list, link) {
980 if (input->pointer_focus == window)
981 input->pointer_focus = NULL;
982 if (input->keyboard_focus == window)
983 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500984 if (input->focus_widget &&
985 input->focus_widget->window == window)
986 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200987 }
988
Rob Bradford7507b572012-05-15 17:55:34 +0100989 wl_list_for_each_safe(window_output, window_output_tmp,
990 &window->window_output_list, link) {
991 free (window_output);
992 }
993
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500994 if (window->input_region)
995 wl_region_destroy(window->input_region);
996 if (window->opaque_region)
997 wl_region_destroy(window->opaque_region);
998
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200999 if (window->frame)
1000 frame_destroy(window->frame);
1001
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001002 if (window->shell_surface)
1003 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001004 wl_surface_destroy(window->surface);
1005 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001006
1007 if (window->cairo_surface != NULL)
1008 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001009
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03001010 if (window->frame_cb)
1011 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001012 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001013 free(window);
1014}
1015
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001016static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001017widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021 wl_list_for_each(child, &widget->child_list, link) {
1022 target = widget_find_widget(child, x, y);
1023 if (target)
1024 return target;
1025 }
1026
1027 if (widget->allocation.x <= x &&
1028 x < widget->allocation.x + widget->allocation.width &&
1029 widget->allocation.y <= y &&
1030 y < widget->allocation.y + widget->allocation.height) {
1031 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001032 }
1033
1034 return NULL;
1035}
1036
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001037static struct widget *
1038widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 widget = malloc(sizeof *widget);
1043 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001044 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001046 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001047 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001048 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001049 widget->tooltip = NULL;
1050 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001051
1052 return widget;
1053}
1054
1055struct widget *
1056window_add_widget(struct window *window, void *data)
1057{
1058 window->widget = widget_create(window, data);
1059 wl_list_init(&window->widget->link);
1060
1061 return window->widget;
1062}
1063
1064struct widget *
1065widget_add_widget(struct widget *parent, void *data)
1066{
1067 struct widget *widget;
1068
1069 widget = widget_create(parent->window, data);
1070 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
1075void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001076widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001078 struct display *display = widget->window->display;
1079 struct input *input;
1080
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001081 if (widget->tooltip) {
1082 free(widget->tooltip);
1083 widget->tooltip = NULL;
1084 }
1085
Pekka Paalanene156fb62012-01-19 13:51:38 +02001086 wl_list_for_each(input, &display->input_list, link) {
1087 if (input->focus_widget == widget)
1088 input->focus_widget = NULL;
1089 }
1090
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001091 wl_list_remove(&widget->link);
1092 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
1101void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001102widget_set_size(struct widget *widget, int32_t width, int32_t height)
1103{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001104 widget->allocation.width = width;
1105 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001106}
1107
1108void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109widget_set_allocation(struct widget *widget,
1110 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001112 widget->allocation.x = x;
1113 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001114 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001115}
1116
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001117void
1118widget_set_transparent(struct widget *widget, int transparent)
1119{
1120 widget->opaque = !transparent;
1121}
1122
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001123void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001124widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001125{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001126 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001127}
1128
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001129void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001130widget_set_resize_handler(struct widget *widget,
1131 widget_resize_handler_t handler)
1132{
1133 widget->resize_handler = handler;
1134}
1135
1136void
1137widget_set_redraw_handler(struct widget *widget,
1138 widget_redraw_handler_t handler)
1139{
1140 widget->redraw_handler = handler;
1141}
1142
1143void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001144widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001146 widget->enter_handler = handler;
1147}
1148
1149void
1150widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1151{
1152 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001153}
1154
1155void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001156widget_set_motion_handler(struct widget *widget,
1157 widget_motion_handler_t handler)
1158{
1159 widget->motion_handler = handler;
1160}
1161
1162void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001163widget_set_button_handler(struct widget *widget,
1164 widget_button_handler_t handler)
1165{
1166 widget->button_handler = handler;
1167}
1168
1169void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001170widget_set_axis_handler(struct widget *widget,
1171 widget_axis_handler_t handler)
1172{
1173 widget->axis_handler = handler;
1174}
1175
1176void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001177widget_schedule_redraw(struct widget *widget)
1178{
1179 window_schedule_redraw(widget->window);
1180}
1181
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001182cairo_surface_t *
1183window_get_surface(struct window *window)
1184{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001185 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001186}
1187
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001188struct wl_surface *
1189window_get_wl_surface(struct window *window)
1190{
1191 return window->surface;
1192}
1193
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001194struct wl_shell_surface *
1195window_get_wl_shell_surface(struct window *window)
1196{
1197 return window->shell_surface;
1198}
1199
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001200static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001201tooltip_redraw_handler(struct widget *widget, void *data)
1202{
1203 cairo_t *cr;
1204 const int32_t r = 3;
1205 struct tooltip *tooltip = data;
1206 int32_t width, height;
1207 struct window *window = widget->window;
1208
1209 cr = cairo_create(window->cairo_surface);
1210 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1211 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1212 cairo_paint(cr);
1213
1214 width = window->allocation.width;
1215 height = window->allocation.height;
1216 rounded_rect(cr, 0, 0, width, height, r);
1217
1218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1219 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1220 cairo_fill(cr);
1221
1222 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1223 cairo_move_to(cr, 10, 16);
1224 cairo_show_text(cr, tooltip->entry);
1225 cairo_destroy(cr);
1226}
1227
1228static cairo_text_extents_t
1229get_text_extents(struct tooltip *tooltip)
1230{
1231 struct window *window;
1232 cairo_t *cr;
1233 cairo_text_extents_t extents;
1234
1235 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1236 * created yet */
1237 window = tooltip->widget->window->parent;
1238 cr = cairo_create(window->cairo_surface);
1239 cairo_text_extents(cr, tooltip->entry, &extents);
1240 cairo_destroy(cr);
1241
1242 return extents;
1243}
1244
1245static int
1246window_create_tooltip(struct tooltip *tooltip)
1247{
1248 struct widget *parent = tooltip->parent;
1249 struct display *display = parent->window->display;
1250 struct window *window;
1251 const int offset_y = 27;
1252 const int margin = 3;
1253 cairo_text_extents_t extents;
1254
1255 if (tooltip->widget)
1256 return 0;
1257
1258 window = window_create_transient(display, parent->window, tooltip->x,
1259 tooltip->y + offset_y,
1260 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1261 if (!window)
1262 return -1;
1263
1264 tooltip->window = window;
1265 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1266
1267 extents = get_text_extents(tooltip);
1268 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1269 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1270
1271 return 0;
1272}
1273
1274void
1275widget_destroy_tooltip(struct widget *parent)
1276{
1277 struct tooltip *tooltip = parent->tooltip;
1278
1279 parent->tooltip_count = 0;
1280 if (!tooltip)
1281 return;
1282
1283 if (tooltip->widget) {
1284 widget_destroy(tooltip->widget);
1285 window_destroy(tooltip->window);
1286 tooltip->widget = NULL;
1287 tooltip->window = NULL;
1288 }
1289
1290 close(tooltip->tooltip_fd);
1291 free(tooltip->entry);
1292 free(tooltip);
1293 parent->tooltip = NULL;
1294}
1295
1296static void
1297tooltip_func(struct task *task, uint32_t events)
1298{
1299 struct tooltip *tooltip =
1300 container_of(task, struct tooltip, tooltip_task);
1301 uint64_t exp;
1302
Martin Olsson8df662a2012-07-08 03:03:47 +02001303 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1304 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001305 window_create_tooltip(tooltip);
1306}
1307
1308#define TOOLTIP_TIMEOUT 500
1309static int
1310tooltip_timer_reset(struct tooltip *tooltip)
1311{
1312 struct itimerspec its;
1313
1314 its.it_interval.tv_sec = 0;
1315 its.it_interval.tv_nsec = 0;
1316 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1317 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1318 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1319 fprintf(stderr, "could not set timerfd\n: %m");
1320 return -1;
1321 }
1322
1323 return 0;
1324}
1325
1326int
1327widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1328{
1329 struct tooltip *tooltip = parent->tooltip;
1330
1331 parent->tooltip_count++;
1332 if (tooltip) {
1333 tooltip->x = x;
1334 tooltip->y = y;
1335 tooltip_timer_reset(tooltip);
1336 return 0;
1337 }
1338
1339 /* the handler might be triggered too fast via input device motion, so
1340 * we need this check here to make sure tooltip is fully initialized */
1341 if (parent->tooltip_count > 1)
1342 return 0;
1343
1344 tooltip = malloc(sizeof *tooltip);
1345 if (!tooltip)
1346 return -1;
1347
1348 parent->tooltip = tooltip;
1349 tooltip->parent = parent;
1350 tooltip->widget = NULL;
1351 tooltip->window = NULL;
1352 tooltip->x = x;
1353 tooltip->y = y;
1354 tooltip->entry = strdup(entry);
1355 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1356 if (tooltip->tooltip_fd < 0) {
1357 fprintf(stderr, "could not create timerfd\n: %m");
1358 return -1;
1359 }
1360
1361 tooltip->tooltip_task.run = tooltip_func;
1362 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1363 EPOLLIN, &tooltip->tooltip_task);
1364 tooltip_timer_reset(tooltip);
1365
1366 return 0;
1367}
1368
1369static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001370workspace_manager_state(void *data,
1371 struct workspace_manager *workspace_manager,
1372 uint32_t current,
1373 uint32_t count)
1374{
1375 struct display *display = data;
1376
1377 display->workspace = current;
1378 display->workspace_count = count;
1379}
1380
1381static const struct workspace_manager_listener workspace_manager_listener = {
1382 workspace_manager_state
1383};
1384
1385static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386frame_resize_handler(struct widget *widget,
1387 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001388{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389 struct frame *frame = data;
1390 struct widget *child = frame->child;
1391 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001392 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001393 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001394 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001395 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001396 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001397 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001398
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001399 switch (widget->window->type) {
1400 case TYPE_FULLSCREEN:
1401 decoration_width = 0;
1402 decoration_height = 0;
1403
1404 allocation.x = 0;
1405 allocation.y = 0;
1406 allocation.width = width;
1407 allocation.height = height;
1408 opaque_margin = 0;
1409
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
1412 break;
1413 case TYPE_MAXIMIZED:
1414 decoration_width = t->width * 2;
1415 decoration_height = t->width + t->titlebar_height;
1416
1417 allocation.x = t->width;
1418 allocation.y = t->titlebar_height;
1419 allocation.width = width - decoration_width;
1420 allocation.height = height - decoration_height;
1421
1422 opaque_margin = 0;
1423
1424 wl_list_for_each(button, &frame->buttons_list, link)
1425 button->widget->opaque = 0;
1426 break;
1427 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 decoration_width = (t->width + t->margin) * 2;
1429 decoration_height = t->width +
1430 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001431
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001432 allocation.x = t->width + t->margin;
1433 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001434 allocation.width = width - decoration_width;
1435 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001436
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001437 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001438
1439 wl_list_for_each(button, &frame->buttons_list, link)
1440 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001441 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001442 }
1443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001444 widget_set_allocation(child, allocation.x, allocation.y,
1445 allocation.width, allocation.height);
1446
1447 if (child->resize_handler)
1448 child->resize_handler(child,
1449 allocation.width,
1450 allocation.height,
1451 child->user_data);
1452
Scott Moreauf7e498c2012-05-14 11:39:29 -06001453 width = child->allocation.width + decoration_width;
1454 height = child->allocation.height + decoration_height;
1455
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001456 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1457
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001458 widget->window->input_region =
1459 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001460 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg023be102012-07-31 11:59:12 -04001461 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001462 shadow_margin, shadow_margin,
1463 width - 2 * shadow_margin,
1464 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04001465 } else {
1466 wl_region_add(widget->window->input_region,
1467 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001468 }
1469
Scott Moreauf7e498c2012-05-14 11:39:29 -06001470 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001471
1472 if (child->opaque) {
1473 widget->window->opaque_region =
1474 wl_compositor_create_region(display->compositor);
1475 wl_region_add(widget->window->opaque_region,
1476 opaque_margin, opaque_margin,
1477 widget->allocation.width - 2 * opaque_margin,
1478 widget->allocation.height - 2 * opaque_margin);
1479 }
Martin Minarik1998b152012-05-10 02:04:35 +02001480
1481 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001482 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1483 x_l = t->width + shadow_margin;
1484 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001485 wl_list_for_each(button, &frame->buttons_list, link) {
1486 const int button_padding = 4;
1487 w = cairo_image_surface_get_width(button->icon);
1488 h = cairo_image_surface_get_height(button->icon);
1489
1490 if (button->decoration == FRAME_BUTTON_FANCY)
1491 w += 10;
1492
1493 if (button->align == FRAME_BUTTON_LEFT) {
1494 widget_set_allocation(button->widget,
1495 x_l, y , w + 1, h + 1);
1496 x_l += w;
1497 x_l += button_padding;
1498 } else {
1499 x_r -= w;
1500 widget_set_allocation(button->widget,
1501 x_r, y , w + 1, h + 1);
1502 x_r -= button_padding;
1503 }
1504 }
1505}
1506
1507static int
1508frame_button_enter_handler(struct widget *widget,
1509 struct input *input, float x, float y, void *data)
1510{
1511 struct frame_button *frame_button = data;
1512
1513 widget_schedule_redraw(frame_button->widget);
1514 frame_button->state = FRAME_BUTTON_OVER;
1515
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001516 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001517}
1518
1519static void
1520frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1521{
1522 struct frame_button *frame_button = data;
1523
1524 widget_schedule_redraw(frame_button->widget);
1525 frame_button->state = FRAME_BUTTON_DEFAULT;
1526}
1527
1528static void
1529frame_button_button_handler(struct widget *widget,
1530 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001531 uint32_t button,
1532 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001533{
1534 struct frame_button *frame_button = data;
1535 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001536 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001537
1538 if (button != BTN_LEFT)
1539 return;
1540
1541 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001542 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001543 frame_button->state = FRAME_BUTTON_ACTIVE;
1544 widget_schedule_redraw(frame_button->widget);
1545
1546 if (frame_button->type == FRAME_BUTTON_ICON)
1547 window_show_frame_menu(window, input, time);
1548 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001549 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001550 frame_button->state = FRAME_BUTTON_DEFAULT;
1551 widget_schedule_redraw(frame_button->widget);
1552 break;
1553 }
1554
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001555 if (!was_pressed)
1556 return;
1557
Martin Minarik1998b152012-05-10 02:04:35 +02001558 switch (frame_button->type) {
1559 case FRAME_BUTTON_CLOSE:
1560 if (window->close_handler)
1561 window->close_handler(window->parent,
1562 window->user_data);
1563 else
1564 display_exit(window->display);
1565 break;
1566 case FRAME_BUTTON_MINIMIZE:
1567 fprintf(stderr,"Minimize stub\n");
1568 break;
1569 case FRAME_BUTTON_MAXIMIZE:
1570 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1571 break;
1572 default:
1573 /* Unknown operation */
1574 break;
1575 }
1576}
1577
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001578static int
1579frame_button_motion_handler(struct widget *widget,
1580 struct input *input, uint32_t time,
1581 float x, float y, void *data)
1582{
1583 struct frame_button *frame_button = data;
1584 enum frame_button_pointer previous_button_state = frame_button->state;
1585
1586 /* only track state for a pressed button */
1587 if (input->grab != widget)
1588 return CURSOR_LEFT_PTR;
1589
1590 if (x > widget->allocation.x &&
1591 x < (widget->allocation.x + widget->allocation.width) &&
1592 y > widget->allocation.y &&
1593 y < (widget->allocation.y + widget->allocation.height)) {
1594 frame_button->state = FRAME_BUTTON_ACTIVE;
1595 } else {
1596 frame_button->state = FRAME_BUTTON_DEFAULT;
1597 }
1598
1599 if (frame_button->state != previous_button_state)
1600 widget_schedule_redraw(frame_button->widget);
1601
1602 return CURSOR_LEFT_PTR;
1603}
1604
Martin Minarik1998b152012-05-10 02:04:35 +02001605static void
1606frame_button_redraw_handler(struct widget *widget, void *data)
1607{
1608 struct frame_button *frame_button = data;
1609 cairo_t *cr;
1610 int width, height, x, y;
1611 struct window *window = widget->window;
1612
1613 x = widget->allocation.x;
1614 y = widget->allocation.y;
1615 width = widget->allocation.width;
1616 height = widget->allocation.height;
1617
1618 if (!width)
1619 return;
1620 if (!height)
1621 return;
1622 if (widget->opaque)
1623 return;
1624
1625 cr = cairo_create(window->cairo_surface);
1626
1627 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1628 cairo_set_line_width(cr, 1);
1629
1630 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1631 cairo_rectangle (cr, x, y, 25, 16);
1632
1633 cairo_stroke_preserve(cr);
1634
1635 switch (frame_button->state) {
1636 case FRAME_BUTTON_DEFAULT:
1637 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1638 break;
1639 case FRAME_BUTTON_OVER:
1640 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1641 break;
1642 case FRAME_BUTTON_ACTIVE:
1643 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1644 break;
1645 }
1646
1647 cairo_fill (cr);
1648
1649 x += 4;
1650 }
1651
1652 cairo_set_source_surface(cr, frame_button->icon, x, y);
1653 cairo_paint(cr);
1654
1655 cairo_destroy(cr);
1656}
1657
1658static struct widget *
1659frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1660 enum frame_button_align align, enum frame_button_decoration style)
1661{
1662 struct frame_button *frame_button;
1663 const char *icon = data;
1664
1665 frame_button = malloc (sizeof *frame_button);
1666 memset(frame_button, 0, sizeof *frame_button);
1667
1668 frame_button->icon = cairo_image_surface_create_from_png(icon);
1669 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1670 frame_button->frame = frame;
1671 frame_button->type = type;
1672 frame_button->align = align;
1673 frame_button->decoration = style;
1674
1675 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1676
1677 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1678 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1679 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1680 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001681 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001682 return frame_button->widget;
1683}
1684
1685static void
1686frame_button_destroy(struct frame_button *frame_button)
1687{
1688 widget_destroy(frame_button->widget);
1689 wl_list_remove(&frame_button->link);
1690 cairo_surface_destroy(frame_button->icon);
1691 free(frame_button);
1692
1693 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001694}
1695
1696static void
1697frame_redraw_handler(struct widget *widget, void *data)
1698{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001699 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001700 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001701 struct theme *t = window->display->theme;
1702 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001703
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001704 if (window->type == TYPE_FULLSCREEN)
1705 return;
1706
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001707 cr = cairo_create(window->cairo_surface);
1708
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001709 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001710 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001711 if (window->type == TYPE_MAXIMIZED)
1712 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001713 theme_render_frame(t, cr, widget->allocation.width,
1714 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001715
1716 cairo_destroy(cr);
1717}
1718
1719static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001720frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001721{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001722 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001723 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001724 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001725
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001726 location = theme_get_location(t, input->sx, input->sy,
1727 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001728 frame->widget->allocation.height,
1729 window->type == TYPE_MAXIMIZED ?
1730 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001732 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001736 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001739 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001740 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001741 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001742 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001743 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001745 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001746 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001747 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001748 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001749 case THEME_LOCATION_EXTERIOR:
1750 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001751 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001752 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001753 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001754}
1755
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001756static void
1757frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001758{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001759 struct display *display;
1760
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001761 switch (index) {
1762 case 0: /* close */
1763 if (window->close_handler)
1764 window->close_handler(window->parent,
1765 window->user_data);
1766 else
1767 display_exit(window->display);
1768 break;
1769 case 1: /* fullscreen */
1770 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001771 if (window->fullscreen_handler)
1772 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001773 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001774 case 2: /* move to workspace above */
1775 display = window->display;
1776 if (display->workspace > 0)
1777 workspace_manager_move_surface(display->workspace_manager,
1778 window->surface,
1779 display->workspace - 1);
1780 break;
1781 case 3: /* move to workspace below */
1782 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001783 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001784 workspace_manager_move_surface(display->workspace_manager,
1785 window->surface,
1786 display->workspace + 1);
1787 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001788 }
1789}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001790
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001791void
1792window_show_frame_menu(struct window *window,
1793 struct input *input, uint32_t time)
1794{
1795 int32_t x, y;
1796
1797 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001798 "Close", "Fullscreen",
1799 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001800 };
1801
1802 input_get_position(input, &x, &y);
1803 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001804 x - 10, y - 10, frame_menu_func, entries,
1805 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001806}
1807
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808static int
1809frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001810 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811{
1812 return frame_get_pointer_image_for_location(data, input);
1813}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001814
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001815static int
1816frame_motion_handler(struct widget *widget,
1817 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001818 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001819{
1820 return frame_get_pointer_image_for_location(data, input);
1821}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001822
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001823static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001824frame_button_handler(struct widget *widget,
1825 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001826 uint32_t button, enum wl_pointer_button_state state,
1827 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001828
1829{
1830 struct frame *frame = data;
1831 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001832 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001833 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001834
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001835 location = theme_get_location(display->theme, input->sx, input->sy,
1836 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001837 frame->widget->allocation.height,
1838 window->type == TYPE_MAXIMIZED ?
1839 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001840
Daniel Stone4dbadb12012-05-30 16:31:51 +01001841 if (window->display->shell && button == BTN_LEFT &&
1842 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001843 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001844 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001845 if (!window->shell_surface)
1846 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001847 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001849 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001850 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001851 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001852 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001853 case THEME_LOCATION_RESIZING_TOP:
1854 case THEME_LOCATION_RESIZING_BOTTOM:
1855 case THEME_LOCATION_RESIZING_LEFT:
1856 case THEME_LOCATION_RESIZING_RIGHT:
1857 case THEME_LOCATION_RESIZING_TOP_LEFT:
1858 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1859 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1860 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001861 if (!window->shell_surface)
1862 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001863 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001864
1865 if (!display->dpy) {
1866 /* If we're using shm, allocate a big
1867 pool to create buffers out of while
1868 we resize. We should probably base
1869 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001870 window->pool =
1871 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001872 }
1873
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001874 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001875 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001876 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001877 break;
1878 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001879 } else if (button == BTN_RIGHT &&
1880 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001881 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001882 }
1883}
1884
1885struct widget *
1886frame_create(struct window *window, void *data)
1887{
1888 struct frame *frame;
1889
1890 frame = malloc(sizeof *frame);
1891 memset(frame, 0, sizeof *frame);
1892
1893 frame->widget = window_add_widget(window, frame);
1894 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001895
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001896 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1897 widget_set_resize_handler(frame->widget, frame_resize_handler);
1898 widget_set_enter_handler(frame->widget, frame_enter_handler);
1899 widget_set_motion_handler(frame->widget, frame_motion_handler);
1900 widget_set_button_handler(frame->widget, frame_button_handler);
1901
Martin Minarik1998b152012-05-10 02:04:35 +02001902 /* Create empty list for frame buttons */
1903 wl_list_init(&frame->buttons_list);
1904
1905 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1906 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1907
1908 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1909 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1910
1911 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1912 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1913
1914 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1915 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1916
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001917 window->frame = frame;
1918
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001919 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001920}
1921
Kristian Høgsberga1627922012-06-20 17:30:03 -04001922void
1923frame_set_child_size(struct widget *widget, int child_width, int child_height)
1924{
1925 struct display *display = widget->window->display;
1926 struct theme *t = display->theme;
1927 int decoration_width, decoration_height;
1928 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001929 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001930
1931 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001932 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001933 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001934 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001935
1936 width = child_width + decoration_width;
1937 height = child_height + decoration_height;
1938 } else {
1939 width = child_width;
1940 height = child_height;
1941 }
1942
1943 window_schedule_resize(widget->window, width, height);
1944}
1945
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001946static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001947frame_destroy(struct frame *frame)
1948{
Martin Minarik1998b152012-05-10 02:04:35 +02001949 struct frame_button *button, *tmp;
1950
1951 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1952 frame_button_destroy(button);
1953
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001954 /* frame->child must be destroyed by the application */
1955 widget_destroy(frame->widget);
1956 free(frame);
1957}
1958
1959static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001960input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001961 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001962{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001963 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001964 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001965
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001966 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001967 return;
1968
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001969 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001970 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001971 widget = old;
1972 if (input->grab)
1973 widget = input->grab;
1974 if (widget->leave_handler)
1975 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001976 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001977 }
1978
1979 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001980 widget = focus;
1981 if (input->grab)
1982 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001983 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001984 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001985 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001986 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001987
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001988 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001989 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001990}
1991
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001992void
1993input_grab(struct input *input, struct widget *widget, uint32_t button)
1994{
1995 input->grab = widget;
1996 input->grab_button = button;
1997}
1998
1999void
2000input_ungrab(struct input *input)
2001{
2002 struct widget *widget;
2003
2004 input->grab = NULL;
2005 if (input->pointer_focus) {
2006 widget = widget_find_widget(input->pointer_focus->widget,
2007 input->sx, input->sy);
2008 input_set_focus_widget(input, widget, input->sx, input->sy);
2009 }
2010}
2011
2012static void
2013input_remove_pointer_focus(struct input *input)
2014{
2015 struct window *window = input->pointer_focus;
2016
2017 if (!window)
2018 return;
2019
2020 input_set_focus_widget(input, NULL, 0, 0);
2021
2022 input->pointer_focus = NULL;
2023 input->current_cursor = CURSOR_UNSET;
2024}
2025
2026static void
2027pointer_handle_enter(void *data, struct wl_pointer *pointer,
2028 uint32_t serial, struct wl_surface *surface,
2029 wl_fixed_t sx_w, wl_fixed_t sy_w)
2030{
2031 struct input *input = data;
2032 struct window *window;
2033 struct widget *widget;
2034 float sx = wl_fixed_to_double(sx_w);
2035 float sy = wl_fixed_to_double(sy_w);
2036
2037 if (!surface) {
2038 /* enter event for a window we've just destroyed */
2039 return;
2040 }
2041
2042 input->display->serial = serial;
2043 input->pointer_enter_serial = serial;
2044 input->pointer_focus = wl_surface_get_user_data(surface);
2045 window = input->pointer_focus;
2046
2047 if (window->pool) {
2048 shm_pool_destroy(window->pool);
2049 window->pool = NULL;
2050 /* Schedule a redraw to free the pool */
2051 window_schedule_redraw(window);
2052 }
2053
2054 input->sx = sx;
2055 input->sy = sy;
2056
2057 widget = widget_find_widget(window->widget, sx, sy);
2058 input_set_focus_widget(input, widget, sx, sy);
2059}
2060
2061static void
2062pointer_handle_leave(void *data, struct wl_pointer *pointer,
2063 uint32_t serial, struct wl_surface *surface)
2064{
2065 struct input *input = data;
2066
2067 input->display->serial = serial;
2068 input_remove_pointer_focus(input);
2069}
2070
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002071static void
Daniel Stone37816df2012-05-16 18:45:18 +01002072pointer_handle_motion(void *data, struct wl_pointer *pointer,
2073 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002074{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002075 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002076 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002077 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002078 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002079 float sx = wl_fixed_to_double(sx_w);
2080 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002081
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002082 input->sx = sx;
2083 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002084
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002085 if (!window)
2086 return;
2087
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002088 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002089 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002090 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002091 }
2092
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002093 if (input->grab)
2094 widget = input->grab;
2095 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002096 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002097 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002098 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002099 input, time, sx, sy,
2100 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002101
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002102 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002103}
2104
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002105static void
Daniel Stone37816df2012-05-16 18:45:18 +01002106pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002107 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002108{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002109 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002110 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002111 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002112
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002113 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002114 if (input->focus_widget && input->grab == NULL &&
2115 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002116 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002117
Neil Roberts6b28aad2012-01-23 19:11:18 +00002118 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002120 (*widget->button_handler)(widget,
2121 input, time,
2122 button, state,
2123 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002124
Daniel Stone4dbadb12012-05-30 16:31:51 +01002125 if (input->grab && input->grab_button == button &&
2126 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002127 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002128}
2129
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002130static void
Daniel Stone37816df2012-05-16 18:45:18 +01002131pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002132 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002133{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002134 struct input *input = data;
2135 struct widget *widget;
2136
2137 widget = input->focus_widget;
2138 if (input->grab)
2139 widget = input->grab;
2140 if (widget && widget->axis_handler)
2141 (*widget->axis_handler)(widget,
2142 input, time,
2143 axis, value,
2144 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002145}
2146
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002147static const struct wl_pointer_listener pointer_listener = {
2148 pointer_handle_enter,
2149 pointer_handle_leave,
2150 pointer_handle_motion,
2151 pointer_handle_button,
2152 pointer_handle_axis,
2153};
2154
2155static void
2156input_remove_keyboard_focus(struct input *input)
2157{
2158 struct window *window = input->keyboard_focus;
2159 struct itimerspec its;
2160
2161 its.it_interval.tv_sec = 0;
2162 its.it_interval.tv_nsec = 0;
2163 its.it_value.tv_sec = 0;
2164 its.it_value.tv_nsec = 0;
2165 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2166
2167 if (!window)
2168 return;
2169
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002170 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002171 if (window->keyboard_focus_handler)
2172 (*window->keyboard_focus_handler)(window, NULL,
2173 window->user_data);
2174
2175 input->keyboard_focus = NULL;
2176}
2177
2178static void
2179keyboard_repeat_func(struct task *task, uint32_t events)
2180{
2181 struct input *input =
2182 container_of(task, struct input, repeat_task);
2183 struct window *window = input->keyboard_focus;
2184 uint64_t exp;
2185
2186 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2187 /* If we change the timer between the fd becoming
2188 * readable and getting here, there'll be nothing to
2189 * read and we get EAGAIN. */
2190 return;
2191
2192 if (window && window->key_handler) {
2193 (*window->key_handler)(window, input, input->repeat_time,
2194 input->repeat_key, input->repeat_sym,
2195 WL_KEYBOARD_KEY_STATE_PRESSED,
2196 window->user_data);
2197 }
2198}
2199
2200static void
2201keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2202 uint32_t format, int fd, uint32_t size)
2203{
2204 struct input *input = data;
2205 char *map_str;
2206
2207 if (!data) {
2208 close(fd);
2209 return;
2210 }
2211
2212 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2213 close(fd);
2214 return;
2215 }
2216
2217 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2218 if (map_str == MAP_FAILED) {
2219 close(fd);
2220 return;
2221 }
2222
2223 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2224 map_str,
2225 XKB_KEYMAP_FORMAT_TEXT_V1,
2226 0);
2227 munmap(map_str, size);
2228 close(fd);
2229
2230 if (!input->xkb.keymap) {
2231 fprintf(stderr, "failed to compile keymap\n");
2232 return;
2233 }
2234
2235 input->xkb.state = xkb_state_new(input->xkb.keymap);
2236 if (!input->xkb.state) {
2237 fprintf(stderr, "failed to create XKB state\n");
2238 xkb_map_unref(input->xkb.keymap);
2239 input->xkb.keymap = NULL;
2240 return;
2241 }
2242
2243 input->xkb.control_mask =
2244 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2245 input->xkb.alt_mask =
2246 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2247 input->xkb.shift_mask =
2248 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2249}
2250
2251static void
2252keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2253 uint32_t serial, struct wl_surface *surface,
2254 struct wl_array *keys)
2255{
2256 struct input *input = data;
2257 struct window *window;
2258
2259 input->display->serial = serial;
2260 input->keyboard_focus = wl_surface_get_user_data(surface);
2261
2262 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002263 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002264 if (window->keyboard_focus_handler)
2265 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002266 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002267}
2268
2269static void
2270keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2271 uint32_t serial, struct wl_surface *surface)
2272{
2273 struct input *input = data;
2274
2275 input->display->serial = serial;
2276 input_remove_keyboard_focus(input);
2277}
2278
Scott Moreau210d0792012-03-22 10:47:01 -06002279static void
Daniel Stone37816df2012-05-16 18:45:18 +01002280keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002281 uint32_t serial, uint32_t time, uint32_t key,
2282 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002283{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002284 struct input *input = data;
2285 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002286 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002287 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002288 const xkb_keysym_t *syms;
2289 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002290 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002291
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002292 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002293 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002294 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002295 return;
2296
Daniel Stone97f68542012-05-30 16:32:01 +01002297 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002298
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002299 sym = XKB_KEY_NoSymbol;
2300 if (num_syms == 1)
2301 sym = syms[0];
2302
2303 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002304 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002305 window_set_maximized(window,
2306 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002307 } else if (sym == XKB_KEY_F11 &&
2308 window->fullscreen_handler &&
2309 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2310 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002311 } else if (sym == XKB_KEY_F4 &&
2312 input->modifiers == MOD_ALT_MASK &&
2313 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2314 if (window->close_handler)
2315 window->close_handler(window->parent,
2316 window->user_data);
2317 else
2318 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002319 } else if (window->key_handler) {
2320 (*window->key_handler)(window, input, time, key,
2321 sym, state, window->user_data);
2322 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002323
2324 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2325 key == input->repeat_key) {
2326 its.it_interval.tv_sec = 0;
2327 its.it_interval.tv_nsec = 0;
2328 its.it_value.tv_sec = 0;
2329 its.it_value.tv_nsec = 0;
2330 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2331 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2332 input->repeat_sym = sym;
2333 input->repeat_key = key;
2334 input->repeat_time = time;
2335 its.it_interval.tv_sec = 0;
2336 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2337 its.it_value.tv_sec = 0;
2338 its.it_value.tv_nsec = 400 * 1000 * 1000;
2339 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2340 }
2341}
2342
2343static void
Daniel Stone351eb612012-05-31 15:27:47 -04002344keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2345 uint32_t serial, uint32_t mods_depressed,
2346 uint32_t mods_latched, uint32_t mods_locked,
2347 uint32_t group)
2348{
2349 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002350 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002351
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002352 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2353 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002354 mask = xkb_state_serialize_mods(input->xkb.state,
2355 XKB_STATE_DEPRESSED |
2356 XKB_STATE_LATCHED);
2357 input->modifiers = 0;
2358 if (mask & input->xkb.control_mask)
2359 input->modifiers |= MOD_CONTROL_MASK;
2360 if (mask & input->xkb.alt_mask)
2361 input->modifiers |= MOD_ALT_MASK;
2362 if (mask & input->xkb.shift_mask)
2363 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002364}
2365
Daniel Stone37816df2012-05-16 18:45:18 +01002366static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002367 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002368 keyboard_handle_enter,
2369 keyboard_handle_leave,
2370 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002371 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002372};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002373
2374static void
Daniel Stone37816df2012-05-16 18:45:18 +01002375seat_handle_capabilities(void *data, struct wl_seat *seat,
2376 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002377{
Daniel Stone37816df2012-05-16 18:45:18 +01002378 struct input *input = data;
2379
2380 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2381 input->pointer = wl_seat_get_pointer(seat);
2382 wl_pointer_set_user_data(input->pointer, input);
2383 wl_pointer_add_listener(input->pointer, &pointer_listener,
2384 input);
2385 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2386 wl_pointer_destroy(input->pointer);
2387 input->pointer = NULL;
2388 }
2389
2390 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2391 input->keyboard = wl_seat_get_keyboard(seat);
2392 wl_keyboard_set_user_data(input->keyboard, input);
2393 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2394 input);
2395 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2396 wl_keyboard_destroy(input->keyboard);
2397 input->keyboard = NULL;
2398 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002399}
2400
Daniel Stone37816df2012-05-16 18:45:18 +01002401static const struct wl_seat_listener seat_listener = {
2402 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002403};
2404
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002405void
2406input_get_position(struct input *input, int32_t *x, int32_t *y)
2407{
2408 *x = input->sx;
2409 *y = input->sy;
2410}
2411
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002412struct display *
2413input_get_display(struct input *input)
2414{
2415 return input->display;
2416}
2417
Daniel Stone37816df2012-05-16 18:45:18 +01002418struct wl_seat *
2419input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002420{
Daniel Stone37816df2012-05-16 18:45:18 +01002421 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002422}
2423
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002424uint32_t
2425input_get_modifiers(struct input *input)
2426{
2427 return input->modifiers;
2428}
2429
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002430struct widget *
2431input_get_focus_widget(struct input *input)
2432{
2433 return input->focus_widget;
2434}
2435
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002436struct data_offer {
2437 struct wl_data_offer *offer;
2438 struct input *input;
2439 struct wl_array types;
2440 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002441
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002442 struct task io_task;
2443 int fd;
2444 data_func_t func;
2445 int32_t x, y;
2446 void *user_data;
2447};
2448
2449static void
2450data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2451{
2452 struct data_offer *offer = data;
2453 char **p;
2454
2455 p = wl_array_add(&offer->types, sizeof *p);
2456 *p = strdup(type);
2457}
2458
2459static const struct wl_data_offer_listener data_offer_listener = {
2460 data_offer_offer,
2461};
2462
2463static void
2464data_offer_destroy(struct data_offer *offer)
2465{
2466 char **p;
2467
2468 offer->refcount--;
2469 if (offer->refcount == 0) {
2470 wl_data_offer_destroy(offer->offer);
2471 for (p = offer->types.data; *p; p++)
2472 free(*p);
2473 wl_array_release(&offer->types);
2474 free(offer);
2475 }
2476}
2477
2478static void
2479data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002480 struct wl_data_device *data_device,
2481 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002482{
2483 struct data_offer *offer;
2484
2485 offer = malloc(sizeof *offer);
2486
2487 wl_array_init(&offer->types);
2488 offer->refcount = 1;
2489 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002490 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002491 wl_data_offer_add_listener(offer->offer,
2492 &data_offer_listener, offer);
2493}
2494
2495static void
2496data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002497 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002498 wl_fixed_t x_w, wl_fixed_t y_w,
2499 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002500{
2501 struct input *input = data;
2502 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002503 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002504 float x = wl_fixed_to_double(x_w);
2505 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002506 char **p;
2507
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002508 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002509 window = wl_surface_get_user_data(surface);
2510 input->pointer_focus = window;
2511
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002512 if (offer) {
2513 input->drag_offer = wl_data_offer_get_user_data(offer);
2514
2515 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2516 *p = NULL;
2517
2518 types_data = input->drag_offer->types.data;
2519 } else {
2520 input->drag_offer = NULL;
2521 types_data = NULL;
2522 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002523
2524 window = input->pointer_focus;
2525 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002526 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002527 window->user_data);
2528}
2529
2530static void
2531data_device_leave(void *data, struct wl_data_device *data_device)
2532{
2533 struct input *input = data;
2534
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002535 if (input->drag_offer) {
2536 data_offer_destroy(input->drag_offer);
2537 input->drag_offer = NULL;
2538 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002539}
2540
2541static void
2542data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002543 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002544{
2545 struct input *input = data;
2546 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002547 float x = wl_fixed_to_double(x_w);
2548 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002549 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002550
2551 input->sx = x;
2552 input->sy = y;
2553
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002554 if (input->drag_offer)
2555 types_data = input->drag_offer->types.data;
2556 else
2557 types_data = NULL;
2558
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002559 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002560 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002561 window->user_data);
2562}
2563
2564static void
2565data_device_drop(void *data, struct wl_data_device *data_device)
2566{
2567 struct input *input = data;
2568 struct window *window = input->pointer_focus;
2569
2570 if (window->drop_handler)
2571 window->drop_handler(window, input,
2572 input->sx, input->sy, window->user_data);
2573}
2574
2575static void
2576data_device_selection(void *data,
2577 struct wl_data_device *wl_data_device,
2578 struct wl_data_offer *offer)
2579{
2580 struct input *input = data;
2581 char **p;
2582
2583 if (input->selection_offer)
2584 data_offer_destroy(input->selection_offer);
2585
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002586 if (offer) {
2587 input->selection_offer = wl_data_offer_get_user_data(offer);
2588 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2589 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002590 } else {
2591 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002592 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002593}
2594
2595static const struct wl_data_device_listener data_device_listener = {
2596 data_device_data_offer,
2597 data_device_enter,
2598 data_device_leave,
2599 data_device_motion,
2600 data_device_drop,
2601 data_device_selection
2602};
2603
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002604static void
2605input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002606{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002608 struct wl_cursor *cursor;
2609 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002610
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002611 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002612 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002613 return;
2614
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002615 if (index >= (int) cursor->image_count) {
2616 fprintf(stderr, "cursor index out of range\n");
2617 return;
2618 }
2619
2620 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002621 buffer = wl_cursor_image_get_buffer(image);
2622 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002623 return;
2624
Kristian Høgsbergae277372012-08-01 09:41:08 -04002625 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002626 input->pointer_surface,
2627 image->hotspot_x, image->hotspot_y);
2628 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2629 wl_surface_damage(input->pointer_surface, 0, 0,
2630 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03002631 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002632}
2633
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002634static const struct wl_callback_listener pointer_surface_listener;
2635
2636static void
2637pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2638 uint32_t time)
2639{
2640 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002641 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002642 int i;
2643
2644 if (callback) {
2645 assert(callback == input->cursor_frame_cb);
2646 wl_callback_destroy(callback);
2647 input->cursor_frame_cb = NULL;
2648 }
2649
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002650 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002651 wl_pointer_set_cursor(input->pointer,
2652 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002653 NULL, 0, 0);
2654 return;
2655 }
2656
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002657 if (input->current_cursor == CURSOR_UNSET)
2658 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002659 cursor = input->display->cursors[input->current_cursor];
2660 if (!cursor)
2661 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002662
2663 /* FIXME We don't have the current time on the first call so we set
2664 * the animation start to the time of the first frame callback. */
2665 if (time == 0)
2666 input->cursor_anim_start = 0;
2667 else if (input->cursor_anim_start == 0)
2668 input->cursor_anim_start = time;
2669
2670 if (time == 0 || input->cursor_anim_start == 0)
2671 i = 0;
2672 else
2673 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2674
Pekka Paalanenbc106382012-10-10 12:49:31 +03002675 if (cursor->image_count > 1) {
2676 input->cursor_frame_cb =
2677 wl_surface_frame(input->pointer_surface);
2678 wl_callback_add_listener(input->cursor_frame_cb,
2679 &pointer_surface_listener, input);
2680 }
2681
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002682 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002683}
2684
2685static const struct wl_callback_listener pointer_surface_listener = {
2686 pointer_surface_frame_callback
2687};
2688
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002689void
2690input_set_pointer_image(struct input *input, int pointer)
2691{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002692 int force = 0;
2693
2694 if (input->pointer_enter_serial > input->cursor_serial)
2695 force = 1;
2696
2697 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002698 return;
2699
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002700 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002701 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002702 if (!input->cursor_frame_cb)
2703 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002704 else if (force) {
2705 /* The current frame callback may be stuck if, for instance,
2706 * the set cursor request was processed by the server after
2707 * this client lost the focus. In this case the cursor surface
2708 * might not be mapped and the frame callback wouldn't ever
2709 * complete. Send a set_cursor and attach to try to map the
2710 * cursor surface again so that the callback will finish */
2711 input_set_pointer_image_index(input, 0);
2712 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002713}
2714
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002715struct wl_data_device *
2716input_get_data_device(struct input *input)
2717{
2718 return input->data_device;
2719}
2720
2721void
2722input_set_selection(struct input *input,
2723 struct wl_data_source *source, uint32_t time)
2724{
2725 wl_data_device_set_selection(input->data_device, source, time);
2726}
2727
2728void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002729input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002730{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 wl_data_offer_accept(input->drag_offer->offer,
2732 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002733}
2734
2735static void
2736offer_io_func(struct task *task, uint32_t events)
2737{
2738 struct data_offer *offer =
2739 container_of(task, struct data_offer, io_task);
2740 unsigned int len;
2741 char buffer[4096];
2742
2743 len = read(offer->fd, buffer, sizeof buffer);
2744 offer->func(buffer, len,
2745 offer->x, offer->y, offer->user_data);
2746
2747 if (len == 0) {
2748 close(offer->fd);
2749 data_offer_destroy(offer);
2750 }
2751}
2752
2753static void
2754data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2755 data_func_t func, void *user_data)
2756{
2757 int p[2];
2758
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002759 if (pipe2(p, O_CLOEXEC) == -1)
2760 return;
2761
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002762 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2763 close(p[1]);
2764
2765 offer->io_task.run = offer_io_func;
2766 offer->fd = p[0];
2767 offer->func = func;
2768 offer->refcount++;
2769 offer->user_data = user_data;
2770
2771 display_watch_fd(offer->input->display,
2772 offer->fd, EPOLLIN, &offer->io_task);
2773}
2774
2775void
2776input_receive_drag_data(struct input *input, const char *mime_type,
2777 data_func_t func, void *data)
2778{
2779 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2780 input->drag_offer->x = input->sx;
2781 input->drag_offer->y = input->sy;
2782}
2783
2784int
2785input_receive_selection_data(struct input *input, const char *mime_type,
2786 data_func_t func, void *data)
2787{
2788 char **p;
2789
2790 if (input->selection_offer == NULL)
2791 return -1;
2792
2793 for (p = input->selection_offer->types.data; *p; p++)
2794 if (strcmp(mime_type, *p) == 0)
2795 break;
2796
2797 if (*p == NULL)
2798 return -1;
2799
2800 data_offer_receive_data(input->selection_offer,
2801 mime_type, func, data);
2802 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002803}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002804
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002805int
2806input_receive_selection_data_to_fd(struct input *input,
2807 const char *mime_type, int fd)
2808{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002809 if (input->selection_offer)
2810 wl_data_offer_receive(input->selection_offer->offer,
2811 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002812
2813 return 0;
2814}
2815
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002816void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002817window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002818{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002819 if (!window->shell_surface)
2820 return;
2821
Daniel Stone37816df2012-05-16 18:45:18 +01002822 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002823}
2824
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002825static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002826idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002827{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002828 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002829
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002830 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002831 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002832 widget_set_allocation(widget,
2833 window->pending_allocation.x,
2834 window->pending_allocation.y,
2835 window->pending_allocation.width,
2836 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002837
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002838 if (window->input_region) {
2839 wl_region_destroy(window->input_region);
2840 window->input_region = NULL;
2841 }
2842
2843 if (window->opaque_region) {
2844 wl_region_destroy(window->opaque_region);
2845 window->opaque_region = NULL;
2846 }
2847
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002848 if (widget->resize_handler)
2849 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002850 widget->allocation.width,
2851 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002852 widget->user_data);
2853
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002854 if (window->allocation.width != widget->allocation.width ||
2855 window->allocation.height != widget->allocation.height) {
2856 window->allocation = widget->allocation;
2857 window_schedule_redraw(window);
2858 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002859}
2860
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002861void
2862window_schedule_resize(struct window *window, int width, int height)
2863{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002864 window->pending_allocation.x = 0;
2865 window->pending_allocation.y = 0;
2866 window->pending_allocation.width = width;
2867 window->pending_allocation.height = height;
2868
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002869 if (window->min_allocation.width == 0)
2870 window->min_allocation = window->pending_allocation;
2871 if (window->pending_allocation.width < window->min_allocation.width)
2872 window->pending_allocation.width = window->min_allocation.width;
2873 if (window->pending_allocation.height < window->min_allocation.height)
2874 window->pending_allocation.height = window->min_allocation.height;
2875
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002876 window->resize_needed = 1;
2877 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002878}
2879
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002880void
2881widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2882{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002883 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002884}
2885
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002886static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002887handle_ping(void *data, struct wl_shell_surface *shell_surface,
2888 uint32_t serial)
2889{
2890 wl_shell_surface_pong(shell_surface, serial);
2891}
2892
2893static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002894handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002895 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002896{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002897 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002898
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002899 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002900 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002901}
2902
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002903static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002904menu_destroy(struct menu *menu)
2905{
2906 widget_destroy(menu->widget);
2907 window_destroy(menu->window);
2908 free(menu);
2909}
2910
2911static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002912handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2913{
2914 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002915 struct menu *menu = window->widget->user_data;
2916
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002917 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002918 * device. Or just use wl_callback. And this really needs to
2919 * be a window vfunc that the menu can set. And we need the
2920 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002921
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002922 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002923 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002924 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002925}
2926
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002927static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002928 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002929 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002930 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002931};
2932
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002933void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002934window_get_allocation(struct window *window,
2935 struct rectangle *allocation)
2936{
2937 *allocation = window->allocation;
2938}
2939
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002940static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002941widget_redraw(struct widget *widget)
2942{
2943 struct widget *child;
2944
2945 if (widget->redraw_handler)
2946 widget->redraw_handler(widget, widget->user_data);
2947 wl_list_for_each(child, &widget->child_list, link)
2948 widget_redraw(child);
2949}
2950
2951static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002952frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2953{
2954 struct window *window = data;
2955
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002956 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002957 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002958 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002959 window->redraw_scheduled = 0;
2960 if (window->redraw_needed)
2961 window_schedule_redraw(window);
2962}
2963
2964static const struct wl_callback_listener listener = {
2965 frame_callback
2966};
2967
2968static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002969idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002970{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002971 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002972
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002973 if (window->resize_needed)
2974 idle_resize(window);
2975
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002976 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002977 widget_redraw(window->widget);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002978 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002979 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002980
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002981 window->frame_cb = wl_surface_frame(window->surface);
2982 wl_callback_add_listener(window->frame_cb, &listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002983 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002984}
2985
2986void
2987window_schedule_redraw(struct window *window)
2988{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002989 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002990 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002991 window->redraw_task.run = idle_redraw;
2992 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002993 window->redraw_scheduled = 1;
2994 }
2995}
2996
Kristian Høgsberg1671e112012-10-10 11:36:24 -04002997int
2998window_is_fullscreen(struct window *window)
2999{
3000 return window->type == TYPE_FULLSCREEN;
3001}
3002
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003003void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003004window_set_fullscreen(struct window *window, int fullscreen)
3005{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003006 if (!window->display->shell)
3007 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003008
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003009 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003010 return;
3011
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003012 if (fullscreen) {
3013 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003014 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003015 wl_shell_surface_set_fullscreen(window->shell_surface,
3016 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3017 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003018 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003019 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003020 wl_shell_surface_set_toplevel(window->shell_surface);
3021 window_schedule_resize(window,
3022 window->saved_allocation.width,
3023 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003024 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003025}
3026
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003027int
3028window_is_maximized(struct window *window)
3029{
3030 return window->type == TYPE_MAXIMIZED;
3031}
3032
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003033void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003034window_set_maximized(struct window *window, int maximized)
3035{
3036 if (!window->display->shell)
3037 return;
3038
3039 if ((window->type == TYPE_MAXIMIZED) == maximized)
3040 return;
3041
3042 if (window->type == TYPE_TOPLEVEL) {
3043 window->saved_allocation = window->allocation;
3044 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3045 window->type = TYPE_MAXIMIZED;
3046 } else {
3047 wl_shell_surface_set_toplevel(window->shell_surface);
3048 window->type = TYPE_TOPLEVEL;
3049 window_schedule_resize(window,
3050 window->saved_allocation.width,
3051 window->saved_allocation.height);
3052 }
3053}
3054
3055void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003056window_set_user_data(struct window *window, void *data)
3057{
3058 window->user_data = data;
3059}
3060
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003061void *
3062window_get_user_data(struct window *window)
3063{
3064 return window->user_data;
3065}
3066
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003067void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003068window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003069 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003070{
3071 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003072}
3073
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003074void
3075window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003076 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003077{
3078 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003079}
3080
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003081void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003082window_set_data_handler(struct window *window, window_data_handler_t handler)
3083{
3084 window->data_handler = handler;
3085}
3086
3087void
3088window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3089{
3090 window->drop_handler = handler;
3091}
3092
3093void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003094window_set_close_handler(struct window *window,
3095 window_close_handler_t handler)
3096{
3097 window->close_handler = handler;
3098}
3099
3100void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003101window_set_fullscreen_handler(struct window *window,
3102 window_fullscreen_handler_t handler)
3103{
3104 window->fullscreen_handler = handler;
3105}
3106
3107void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003108window_set_title(struct window *window, const char *title)
3109{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003110 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003111 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003112 if (window->shell_surface)
3113 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003114}
3115
3116const char *
3117window_get_title(struct window *window)
3118{
3119 return window->title;
3120}
3121
3122void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003123window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3124{
3125 struct text_cursor_position *text_cursor_position =
3126 window->display->text_cursor_position;
3127
Scott Moreau9295ce02012-06-01 12:46:10 -06003128 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003129 return;
3130
3131 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003132 window->surface,
3133 wl_fixed_from_int(x),
3134 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003135}
3136
3137void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003138window_damage(struct window *window, int32_t x, int32_t y,
3139 int32_t width, int32_t height)
3140{
3141 wl_surface_damage(window->surface, x, y, width, height);
3142}
3143
Casey Dahlin9074db52012-04-19 22:50:09 -04003144static void
3145surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003146 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003147{
Rob Bradford7507b572012-05-15 17:55:34 +01003148 struct window *window = data;
3149 struct output *output;
3150 struct output *output_found = NULL;
3151 struct window_output *window_output;
3152
3153 wl_list_for_each(output, &window->display->output_list, link) {
3154 if (output->output == wl_output) {
3155 output_found = output;
3156 break;
3157 }
3158 }
3159
3160 if (!output_found)
3161 return;
3162
3163 window_output = malloc (sizeof *window_output);
3164 window_output->output = output_found;
3165
3166 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003167}
3168
3169static void
3170surface_leave(void *data,
3171 struct wl_surface *wl_surface, struct wl_output *output)
3172{
Rob Bradford7507b572012-05-15 17:55:34 +01003173 struct window *window = data;
3174 struct window_output *window_output;
3175 struct window_output *window_output_found = NULL;
3176
3177 wl_list_for_each(window_output, &window->window_output_list, link) {
3178 if (window_output->output->output == output) {
3179 window_output_found = window_output;
3180 break;
3181 }
3182 }
3183
3184 if (window_output_found) {
3185 wl_list_remove(&window_output_found->link);
3186 free(window_output_found);
3187 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003188}
3189
3190static const struct wl_surface_listener surface_listener = {
3191 surface_enter,
3192 surface_leave
3193};
3194
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003195static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003196window_create_internal(struct display *display,
3197 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003198{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003199 struct window *window;
3200
3201 window = malloc(sizeof *window);
3202 if (window == NULL)
3203 return NULL;
3204
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003205 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003206 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003207 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003208 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003209 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003210 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003211 window->shell_surface =
3212 wl_shell_get_shell_surface(display->shell,
3213 window->surface);
3214 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003215 window->allocation.x = 0;
3216 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003217 window->allocation.width = 0;
3218 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003219 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003220 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003221 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003222 window->input_region = NULL;
3223 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003224
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003225 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003226#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003227 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003228#else
3229 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3230#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003231 else
3232 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003233
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003234 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003235 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003236 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003237
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003238 if (window->shell_surface) {
3239 wl_shell_surface_set_user_data(window->shell_surface, window);
3240 wl_shell_surface_add_listener(window->shell_surface,
3241 &shell_surface_listener, window);
3242 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243
Rob Bradford7507b572012-05-15 17:55:34 +01003244 wl_list_init (&window->window_output_list);
3245
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003246 return window;
3247}
3248
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003249struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003250window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003251{
3252 struct window *window;
3253
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003254 window = window_create_internal(display, NULL, TYPE_NONE);
3255 if (!window)
3256 return NULL;
3257
3258 return window;
3259}
3260
3261struct window *
3262window_create_custom(struct display *display)
3263{
3264 struct window *window;
3265
3266 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003267 if (!window)
3268 return NULL;
3269
3270 return window;
3271}
3272
3273struct window *
3274window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003275 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003276{
3277 struct window *window;
3278
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003279 window = window_create_internal(parent->display,
3280 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003281 if (!window)
3282 return NULL;
3283
3284 window->x = x;
3285 window->y = y;
3286
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003287 if (display->shell)
3288 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003289 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003290 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003291
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003292 return window;
3293}
3294
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003295static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003296menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003297{
3298 int next;
3299
3300 next = (sy - 8) / 20;
3301 if (menu->current != next) {
3302 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003303 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003304 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305}
3306
3307static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003308menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003309 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003310 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003311{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003312 struct menu *menu = data;
3313
3314 if (widget == menu->widget)
3315 menu_set_item(data, y);
3316
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003317 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003318}
3319
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003320static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003321menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003322 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003323{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003324 struct menu *menu = data;
3325
3326 if (widget == menu->widget)
3327 menu_set_item(data, y);
3328
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003329 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003330}
3331
3332static void
3333menu_leave_handler(struct widget *widget, struct input *input, void *data)
3334{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003335 struct menu *menu = data;
3336
3337 if (widget == menu->widget)
3338 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003339}
3340
3341static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003342menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003343 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003344 uint32_t button, enum wl_pointer_button_state state,
3345 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003346
3347{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003348 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003349
Daniel Stone4dbadb12012-05-30 16:31:51 +01003350 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3351 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003352 /* Either relase after press-drag-release or
3353 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003354 menu->func(menu->window->parent,
3355 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003356 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003357 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003358 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003359}
3360
3361static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003362menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003363{
3364 cairo_t *cr;
3365 const int32_t r = 3, margin = 3;
3366 struct menu *menu = data;
3367 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003368 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003369
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003370 cr = cairo_create(window->cairo_surface);
3371 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3372 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3373 cairo_paint(cr);
3374
3375 width = window->allocation.width;
3376 height = window->allocation.height;
3377 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003378
3379 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003380 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3381 cairo_fill(cr);
3382
3383 for (i = 0; i < menu->count; i++) {
3384 if (i == menu->current) {
3385 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3386 cairo_rectangle(cr, margin, i * 20 + margin,
3387 width - 2 * margin, 20);
3388 cairo_fill(cr);
3389 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3390 cairo_move_to(cr, 10, i * 20 + 16);
3391 cairo_show_text(cr, menu->entries[i]);
3392 } else {
3393 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3394 cairo_move_to(cr, 10, i * 20 + 16);
3395 cairo_show_text(cr, menu->entries[i]);
3396 }
3397 }
3398
3399 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003400}
3401
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003402void
3403window_show_menu(struct display *display,
3404 struct input *input, uint32_t time, struct window *parent,
3405 int32_t x, int32_t y,
3406 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003407{
3408 struct window *window;
3409 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003410 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003411
3412 menu = malloc(sizeof *menu);
3413 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003414 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003415
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003416 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003417 if (!window) {
3418 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003419 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003420 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003421
3422 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003423 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003424 menu->entries = entries;
3425 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003426 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003427 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003428 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003429 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003430 window->type = TYPE_MENU;
3431 window->x = x;
3432 window->y = y;
3433
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003434 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003435 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003436 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003437 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003438 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003439
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003440 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003441 widget_set_enter_handler(menu->widget, menu_enter_handler);
3442 widget_set_leave_handler(menu->widget, menu_leave_handler);
3443 widget_set_motion_handler(menu->widget, menu_motion_handler);
3444 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003445
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003446 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003447 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003448}
3449
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003450void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003451window_set_buffer_type(struct window *window, enum window_buffer_type type)
3452{
3453 window->buffer_type = type;
3454}
3455
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003456
3457static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003458display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003459 struct wl_output *wl_output,
3460 int x, int y,
3461 int physical_width,
3462 int physical_height,
3463 int subpixel,
3464 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003465 const char *model,
3466 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003467{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003468 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003469
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003470 output->allocation.x = x;
3471 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003472 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003473}
3474
3475static void
3476display_handle_mode(void *data,
3477 struct wl_output *wl_output,
3478 uint32_t flags,
3479 int width,
3480 int height,
3481 int refresh)
3482{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003483 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003484 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003485
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003486 if (flags & WL_OUTPUT_MODE_CURRENT) {
3487 output->allocation.width = width;
3488 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003489 if (display->output_configure_handler)
3490 (*display->output_configure_handler)(
3491 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003492 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003493}
3494
3495static const struct wl_output_listener output_listener = {
3496 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003497 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003498};
3499
3500static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003501display_add_output(struct display *d, uint32_t id)
3502{
3503 struct output *output;
3504
3505 output = malloc(sizeof *output);
3506 if (output == NULL)
3507 return;
3508
3509 memset(output, 0, sizeof *output);
3510 output->display = d;
3511 output->output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003512 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003513 wl_list_insert(d->output_list.prev, &output->link);
3514
3515 wl_output_add_listener(output->output, &output_listener, output);
3516}
3517
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003518static void
3519output_destroy(struct output *output)
3520{
3521 if (output->destroy_handler)
3522 (*output->destroy_handler)(output, output->user_data);
3523
3524 wl_output_destroy(output->output);
3525 wl_list_remove(&output->link);
3526 free(output);
3527}
3528
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003529void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003530display_set_global_handler(struct display *display,
3531 display_global_handler_t handler)
3532{
3533 struct global *global;
3534
3535 display->global_handler = handler;
3536 if (!handler)
3537 return;
3538
3539 wl_list_for_each(global, &display->global_list, link)
3540 display->global_handler(display,
3541 global->name, global->interface,
3542 global->version, display->user_data);
3543}
3544
3545void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003546display_set_output_configure_handler(struct display *display,
3547 display_output_handler_t handler)
3548{
3549 struct output *output;
3550
3551 display->output_configure_handler = handler;
3552 if (!handler)
3553 return;
3554
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003555 wl_list_for_each(output, &display->output_list, link) {
3556 if (output->allocation.width == 0 &&
3557 output->allocation.height == 0)
3558 continue;
3559
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003560 (*display->output_configure_handler)(output,
3561 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03003562 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003563}
3564
3565void
3566output_set_user_data(struct output *output, void *data)
3567{
3568 output->user_data = data;
3569}
3570
3571void *
3572output_get_user_data(struct output *output)
3573{
3574 return output->user_data;
3575}
3576
3577void
3578output_set_destroy_handler(struct output *output,
3579 display_output_handler_t handler)
3580{
3581 output->destroy_handler = handler;
3582 /* FIXME: implement this, once we have way to remove outputs */
3583}
3584
3585void
Scott Moreau4e072362012-09-29 02:03:11 -06003586output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003587{
Scott Moreau4e072362012-09-29 02:03:11 -06003588 struct rectangle allocation = output->allocation;
3589
3590 switch (output->transform) {
3591 case WL_OUTPUT_TRANSFORM_90:
3592 case WL_OUTPUT_TRANSFORM_270:
3593 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3594 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3595 /* Swap width and height */
3596 allocation.width = output->allocation.height;
3597 allocation.height = output->allocation.width;
3598 break;
3599 }
3600
3601 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003602}
3603
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003604struct wl_output *
3605output_get_wl_output(struct output *output)
3606{
3607 return output->output;
3608}
3609
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003610static void
Daniel Stone97f68542012-05-30 16:32:01 +01003611fini_xkb(struct input *input)
3612{
3613 xkb_state_unref(input->xkb.state);
3614 xkb_map_unref(input->xkb.keymap);
3615}
3616
3617static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003618display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003619{
3620 struct input *input;
3621
3622 input = malloc(sizeof *input);
3623 if (input == NULL)
3624 return;
3625
3626 memset(input, 0, sizeof *input);
3627 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003628 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003629 input->pointer_focus = NULL;
3630 input->keyboard_focus = NULL;
3631 wl_list_insert(d->input_list.prev, &input->link);
3632
Daniel Stone37816df2012-05-16 18:45:18 +01003633 wl_seat_add_listener(input->seat, &seat_listener, input);
3634 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003635
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636 input->data_device =
3637 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003638 input->seat);
3639 wl_data_device_add_listener(input->data_device, &data_device_listener,
3640 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003641
3642 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003643
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003644 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3645 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003646 input->repeat_task.run = keyboard_repeat_func;
3647 display_watch_fd(d, input->repeat_timer_fd,
3648 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003649}
3650
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003651static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003652input_destroy(struct input *input)
3653{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003654 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003655 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003656
3657 if (input->drag_offer)
3658 data_offer_destroy(input->drag_offer);
3659
3660 if (input->selection_offer)
3661 data_offer_destroy(input->selection_offer);
3662
3663 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003664 fini_xkb(input);
3665
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003666 wl_surface_destroy(input->pointer_surface);
3667
Pekka Paalanene1207c72011-12-16 12:02:09 +02003668 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003669 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003670 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003671 free(input);
3672}
3673
3674static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003675init_workspace_manager(struct display *d, uint32_t id)
3676{
3677 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003678 wl_registry_bind(d->registry, id,
3679 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003680 if (d->workspace_manager != NULL)
3681 workspace_manager_add_listener(d->workspace_manager,
3682 &workspace_manager_listener,
3683 d);
3684}
3685
3686static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003687registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3688 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003689{
3690 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003691 struct global *global;
3692
3693 global = malloc(sizeof *global);
3694 global->name = id;
3695 global->interface = strdup(interface);
3696 global->version = version;
3697 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003698
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003699 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003700 d->compositor = wl_registry_bind(registry, id,
3701 &wl_compositor_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003702 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003703 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003704 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003705 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003706 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003707 d->shell = wl_registry_bind(registry,
3708 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003709 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003710 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003711 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3712 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003713 wl_registry_bind(registry, id,
3714 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003715 } else if (strcmp(interface, "text_cursor_position") == 0) {
3716 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003717 wl_registry_bind(registry, id,
3718 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003719 } else if (strcmp(interface, "workspace_manager") == 0) {
3720 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003721 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003722
3723 if (d->global_handler)
3724 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003725}
3726
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003727void *
3728display_bind(struct display *display, uint32_t name,
3729 const struct wl_interface *interface, uint32_t version)
3730{
3731 return wl_registry_bind(display->registry, name, interface, version);
3732}
3733
3734static const struct wl_registry_listener registry_listener = {
3735 registry_handle_global
3736};
3737
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003738#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003739static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003740init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003741{
3742 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003743 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003744
Rob Clark6396ed32012-03-11 19:48:41 -05003745#ifdef USE_CAIRO_GLESV2
3746# define GL_BIT EGL_OPENGL_ES2_BIT
3747#else
3748# define GL_BIT EGL_OPENGL_BIT
3749#endif
3750
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003751 static const EGLint argb_cfg_attribs[] = {
3752 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003753 EGL_RED_SIZE, 1,
3754 EGL_GREEN_SIZE, 1,
3755 EGL_BLUE_SIZE, 1,
3756 EGL_ALPHA_SIZE, 1,
3757 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003758 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003759 EGL_NONE
3760 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003761
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003762#ifdef USE_CAIRO_GLESV2
3763 static const EGLint context_attribs[] = {
3764 EGL_CONTEXT_CLIENT_VERSION, 2,
3765 EGL_NONE
3766 };
3767 EGLint api = EGL_OPENGL_ES_API;
3768#else
3769 EGLint *context_attribs = NULL;
3770 EGLint api = EGL_OPENGL_API;
3771#endif
3772
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003773 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003774 if (!eglInitialize(d->dpy, &major, &minor)) {
3775 fprintf(stderr, "failed to initialize display\n");
3776 return -1;
3777 }
3778
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003779 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003780 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3781 return -1;
3782 }
3783
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003784 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3785 &d->argb_config, 1, &n) || n != 1) {
3786 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003787 return -1;
3788 }
3789
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003790 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003791 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003792 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003793 fprintf(stderr, "failed to create context\n");
3794 return -1;
3795 }
3796
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003797 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003798 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003799 return -1;
3800 }
3801
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003802#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003803 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3804 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3805 fprintf(stderr, "failed to get cairo egl argb device\n");
3806 return -1;
3807 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003808#endif
3809
3810 return 0;
3811}
3812
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003813static void
3814fini_egl(struct display *display)
3815{
3816#ifdef HAVE_CAIRO_EGL
3817 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003818#endif
3819
3820 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3821 EGL_NO_CONTEXT);
3822
3823 eglTerminate(display->dpy);
3824 eglReleaseThread();
3825}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003826#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003827
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003828static void
3829handle_display_data(struct task *task, uint32_t events)
3830{
3831 struct display *display =
3832 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003833 struct epoll_event ep;
3834 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003835
3836 display->display_fd_events = events;
3837
3838 if (events & EPOLLERR || events & EPOLLHUP) {
3839 display_exit(display);
3840 return;
3841 }
3842
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003843 if (events & EPOLLIN)
3844 wl_display_dispatch(display->display);
3845
3846 if (events & EPOLLOUT) {
3847 ret = wl_display_flush(display->display);
3848 if (ret == 0) {
3849 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3850 ep.data.ptr = &display->display_task;
3851 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3852 display->display_fd, &ep);
3853 } else if (ret == -1 && errno != EAGAIN) {
3854 display_exit(display);
3855 return;
3856 }
3857 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003858}
3859
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003860struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003861display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003862{
3863 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003865 d = malloc(sizeof *d);
3866 if (d == NULL)
3867 return NULL;
3868
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003869 memset(d, 0, sizeof *d);
3870
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003871 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003872 if (d->display == NULL) {
3873 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003874 return NULL;
3875 }
3876
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003877 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003878 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003879 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003880 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3881 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003882
3883 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003884 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003885 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003886 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003887
Daniel Stone97f68542012-05-30 16:32:01 +01003888 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003889 if (d->xkb_context == NULL) {
3890 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003891 return NULL;
3892 }
3893
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003894 d->workspace = 0;
3895 d->workspace_count = 1;
3896
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04003897 d->registry = wl_display_get_registry(d->display);
3898 wl_registry_add_listener(d->registry, &registry_listener, d);
3899 wl_display_dispatch(d->display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003900#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003901 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003902 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003903#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003904
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003905 d->image_target_texture_2d =
3906 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3907 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3908 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3909
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003910 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003911
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003912 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003913
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003914 wl_list_init(&d->window_list);
3915
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003916 return d;
3917}
3918
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003919static void
3920display_destroy_outputs(struct display *display)
3921{
3922 struct output *tmp;
3923 struct output *output;
3924
3925 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3926 output_destroy(output);
3927}
3928
Pekka Paalanene1207c72011-12-16 12:02:09 +02003929static void
3930display_destroy_inputs(struct display *display)
3931{
3932 struct input *tmp;
3933 struct input *input;
3934
3935 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3936 input_destroy(input);
3937}
3938
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003939void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003940display_destroy(struct display *display)
3941{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003942 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003943 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3944 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003945
3946 if (!wl_list_empty(&display->deferred_list))
3947 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3948
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003949 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003950 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003951
Daniel Stone97f68542012-05-30 16:32:01 +01003952 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003953
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003954 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003955 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003956
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003957#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003958 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003959#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003960
Pekka Paalanenc2052982011-12-16 11:41:32 +02003961 if (display->shell)
3962 wl_shell_destroy(display->shell);
3963
3964 if (display->shm)
3965 wl_shm_destroy(display->shm);
3966
3967 if (display->data_device_manager)
3968 wl_data_device_manager_destroy(display->data_device_manager);
3969
3970 wl_compositor_destroy(display->compositor);
3971
3972 close(display->epoll_fd);
3973
U. Artie Eoff44874d92012-10-02 21:12:35 -07003974 if (!(display->display_fd_events & EPOLLERR) &&
3975 !(display->display_fd_events & EPOLLHUP))
3976 wl_display_flush(display->display);
3977
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003978 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003979 free(display);
3980}
3981
3982void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003983display_set_user_data(struct display *display, void *data)
3984{
3985 display->user_data = data;
3986}
3987
3988void *
3989display_get_user_data(struct display *display)
3990{
3991 return display->user_data;
3992}
3993
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003994struct wl_display *
3995display_get_display(struct display *display)
3996{
3997 return display->display;
3998}
3999
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004000struct output *
4001display_get_output(struct display *display)
4002{
4003 return container_of(display->output_list.next, struct output, link);
4004}
4005
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004006struct wl_compositor *
4007display_get_compositor(struct display *display)
4008{
4009 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05004010}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004011
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004012uint32_t
4013display_get_serial(struct display *display)
4014{
4015 return display->serial;
4016}
4017
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004018EGLDisplay
4019display_get_egl_display(struct display *d)
4020{
4021 return d->dpy;
4022}
4023
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004024struct wl_data_source *
4025display_create_data_source(struct display *display)
4026{
4027 return wl_data_device_manager_create_data_source(display->data_device_manager);
4028}
4029
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004030EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02004031display_get_argb_egl_config(struct display *d)
4032{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004033 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004034}
4035
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004036struct wl_shell *
4037display_get_shell(struct display *display)
4038{
4039 return display->shell;
4040}
4041
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004042int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004043display_acquire_window_surface(struct display *display,
4044 struct window *window,
4045 EGLContext ctx)
4046{
Benjamin Franzke22d54812011-07-16 19:50:32 +00004047#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004048 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004049 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004050
4051 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004052 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02004053 device = cairo_surface_get_device(window->cairo_surface);
4054 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004055 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004056
Benjamin Franzke0c991632011-09-27 21:57:31 +02004057 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004058 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02004059 ctx = display->argb_ctx;
4060 else
4061 assert(0);
4062 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004063
4064 data = cairo_surface_get_user_data(window->cairo_surface,
4065 &surface_data_key);
4066
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004067 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004068 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004069 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4070 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004071
4072 return 0;
4073#else
4074 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004075#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004076}
4077
4078void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004079display_release_window_surface(struct display *display,
4080 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004081{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004082#ifdef HAVE_CAIRO_EGL
4083 cairo_device_t *device;
4084
4085 device = cairo_surface_get_device(window->cairo_surface);
4086 if (!device)
4087 return;
4088
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004089 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004090 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004091 cairo_device_release(device);
4092#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004093}
4094
4095void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004096display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004097{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004098 wl_list_insert(&display->deferred_list, &task->link);
4099}
4100
4101void
4102display_watch_fd(struct display *display,
4103 int fd, uint32_t events, struct task *task)
4104{
4105 struct epoll_event ep;
4106
4107 ep.events = events;
4108 ep.data.ptr = task;
4109 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4110}
4111
4112void
4113display_run(struct display *display)
4114{
4115 struct task *task;
4116 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004117 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004118
Pekka Paalanen826d7952011-12-15 10:14:07 +02004119 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004120 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004121 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004122 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004123 struct task, link);
4124 wl_list_remove(&task->link);
4125 task->run(task, 0);
4126 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004127
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04004128 wl_display_dispatch_pending(display->display);
4129
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004130 if (!display->running)
4131 break;
4132
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004133 ret = wl_display_flush(display->display);
4134 if (ret < 0 && errno == EAGAIN) {
4135 ep[0].events =
4136 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4137 ep[0].data.ptr = &display->display_task;
4138
4139 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4140 display->display_fd, &ep[0]);
4141 } else if (ret < 0) {
4142 break;
4143 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004144
4145 count = epoll_wait(display->epoll_fd,
4146 ep, ARRAY_LENGTH(ep), -1);
4147 for (i = 0; i < count; i++) {
4148 task = ep[i].data.ptr;
4149 task->run(task, ep[i].events);
4150 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004151 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004152}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004153
4154void
4155display_exit(struct display *display)
4156{
4157 display->running = 0;
4158}