blob: b19c579dfc526f0055a2f9d17abcd9e54404ef47 [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>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020065#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030066#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050069
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070070struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050074 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040076 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040077 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020079 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040080 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050081 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020082 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020083 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040084 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040085
86 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -070087 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040088 uint32_t mask;
89 struct task display_task;
90
91 int epoll_fd;
92 struct wl_list deferred_list;
93
Pekka Paalanen826d7952011-12-15 10:14:07 +020094 int running;
95
Kristian Høgsberg478d9262010-06-08 20:34:11 -040096 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040097 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050098 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040099
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400100 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400101
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300102 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300103 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400104
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500105 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
106 PFNEGLCREATEIMAGEKHRPROC create_image;
107 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200108
109 display_output_handler_t output_configure_handler;
110
111 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100112
113 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200114
115 uint32_t workspace;
116 uint32_t workspace_count;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500117};
118
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400119enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400120 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400122 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500123 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400124 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500125 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400126 TYPE_CUSTOM
127};
Rob Bradford7507b572012-05-15 17:55:34 +0100128
129struct window_output {
130 struct output *output;
131 struct wl_list link;
132};
133
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500134struct window {
135 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500136 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100137 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200139 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500140 struct wl_region *input_region;
141 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500142 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500143 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400144 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500145 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500146 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400147 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400148 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400149 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400150 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400151 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400152 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400153 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400154 int focus_count;
155
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400156 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500157
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400158 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500159
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700160 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400161
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500162 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500163 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400164 window_data_handler_t data_handler;
165 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500166 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400167 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400168
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300169 struct wl_callback *frame_cb;
170
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200171 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500172 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500173
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500174 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400175 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500176};
177
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500178struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500179 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300180 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500181 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182 struct wl_list link;
183 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500184 widget_resize_handler_t resize_handler;
185 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500186 widget_enter_handler_t enter_handler;
187 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500188 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500189 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200190 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400191 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500192 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300193 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400194};
195
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196struct input {
197 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100198 struct wl_seat *seat;
199 struct wl_pointer *pointer;
200 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400201 struct window *pointer_focus;
202 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300203 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300204 uint32_t cursor_anim_start;
205 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300206 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400207 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400208 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400209 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400210 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400211 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400212
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500213 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500214 struct widget *grab;
215 uint32_t grab_button;
216
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400217 struct wl_data_device *data_device;
218 struct data_offer *drag_offer;
219 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100220
221 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100222 struct xkb_keymap *keymap;
223 struct xkb_state *state;
224 xkb_mod_mask_t control_mask;
225 xkb_mod_mask_t alt_mask;
226 xkb_mod_mask_t shift_mask;
227 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400228
229 struct task repeat_task;
230 int repeat_timer_fd;
231 uint32_t repeat_sym;
232 uint32_t repeat_key;
233 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400234};
235
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500236struct output {
237 struct display *display;
238 struct wl_output *output;
239 struct rectangle allocation;
240 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600241 int transform;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200242
243 display_output_handler_t destroy_handler;
244 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500245};
246
Martin Minarik1998b152012-05-10 02:04:35 +0200247enum frame_button_action {
248 FRAME_BUTTON_NULL = 0,
249 FRAME_BUTTON_ICON = 1,
250 FRAME_BUTTON_CLOSE = 2,
251 FRAME_BUTTON_MINIMIZE = 3,
252 FRAME_BUTTON_MAXIMIZE = 4,
253};
254
255enum frame_button_pointer {
256 FRAME_BUTTON_DEFAULT = 0,
257 FRAME_BUTTON_OVER = 1,
258 FRAME_BUTTON_ACTIVE = 2,
259};
260
261enum frame_button_align {
262 FRAME_BUTTON_RIGHT = 0,
263 FRAME_BUTTON_LEFT = 1,
264};
265
266enum frame_button_decoration {
267 FRAME_BUTTON_NONE = 0,
268 FRAME_BUTTON_FANCY = 1,
269};
270
271struct frame_button {
272 struct widget *widget;
273 struct frame *frame;
274 cairo_surface_t *icon;
275 enum frame_button_action type;
276 enum frame_button_pointer state;
277 struct wl_list link; /* buttons_list */
278 enum frame_button_align align;
279 enum frame_button_decoration decoration;
280};
281
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500282struct frame {
283 struct widget *widget;
284 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200285 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500286};
287
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500288struct menu {
289 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500290 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500291 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500292 const char **entries;
293 uint32_t time;
294 int current;
295 int count;
296 menu_func_t func;
297};
298
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300299struct tooltip {
300 struct widget *parent;
301 struct window *window;
302 struct widget *widget;
303 char *entry;
304 struct task tooltip_task;
305 int tooltip_fd;
306 float x, y;
307};
308
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700309struct shm_pool {
310 struct wl_shm_pool *pool;
311 size_t size;
312 size_t used;
313 void *data;
314};
315
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400316enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300317 CURSOR_DEFAULT = 100,
318 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400319};
320
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500321enum window_location {
322 WINDOW_INTERIOR = 0,
323 WINDOW_RESIZING_TOP = 1,
324 WINDOW_RESIZING_BOTTOM = 2,
325 WINDOW_RESIZING_LEFT = 4,
326 WINDOW_RESIZING_TOP_LEFT = 5,
327 WINDOW_RESIZING_BOTTOM_LEFT = 6,
328 WINDOW_RESIZING_RIGHT = 8,
329 WINDOW_RESIZING_TOP_RIGHT = 9,
330 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
331 WINDOW_RESIZING_MASK = 15,
332 WINDOW_EXTERIOR = 16,
333 WINDOW_TITLEBAR = 17,
334 WINDOW_CLIENT_AREA = 18,
335};
336
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337static const cairo_user_data_key_t surface_data_key;
338struct surface_data {
339 struct wl_buffer *buffer;
340};
341
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500342#define MULT(_d,c,a,t) \
343 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
344
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500345#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400346
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100347struct egl_window_surface_data {
348 struct display *display;
349 struct wl_surface *surface;
350 struct wl_egl_window *window;
351 EGLSurface surf;
352};
353
354static void
355egl_window_surface_data_destroy(void *p)
356{
357 struct egl_window_surface_data *data = p;
358 struct display *d = data->display;
359
360 eglDestroySurface(d->dpy, data->surf);
361 wl_egl_window_destroy(data->window);
362 data->surface = NULL;
363
364 free(p);
365}
366
367static cairo_surface_t *
368display_create_egl_window_surface(struct display *display,
369 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400370 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100371 struct rectangle *rectangle)
372{
373 cairo_surface_t *cairo_surface;
374 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100377
378 data = malloc(sizeof *data);
379 if (data == NULL)
380 return NULL;
381
382 data->display = display;
383 data->surface = surface;
384
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500385 config = display->argb_config;
386 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400387
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400388 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100389 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400390 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100391
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400392 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500393 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100394
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100396 data->surf,
397 rectangle->width,
398 rectangle->height);
399
400 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
401 data, egl_window_surface_data_destroy);
402
403 return cairo_surface;
404}
405
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406#endif
407
408struct wl_buffer *
409display_get_buffer_for_surface(struct display *display,
410 cairo_surface_t *surface)
411{
412 struct surface_data *data;
413
414 data = cairo_surface_get_user_data (surface, &surface_data_key);
415
416 return data->buffer;
417}
418
419struct shm_surface_data {
420 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700421 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422};
423
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500424static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700425shm_pool_destroy(struct shm_pool *pool);
426
427static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428shm_surface_data_destroy(void *p)
429{
430 struct shm_surface_data *data = p;
431
432 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700433 if (data->pool)
434 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300435
436 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437}
438
Kristian Høgsberg16626282012-04-03 11:21:27 -0400439static struct wl_shm_pool *
440make_shm_pool(struct display *display, int size, void **data)
441{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400442 struct wl_shm_pool *pool;
443 int fd;
444
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300445 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400446 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300447 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
448 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400449 return NULL;
450 }
451
452 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400453 if (*data == MAP_FAILED) {
454 fprintf(stderr, "mmap failed: %m\n");
455 close(fd);
456 return NULL;
457 }
458
459 pool = wl_shm_create_pool(display->shm, fd, size);
460
461 close(fd);
462
463 return pool;
464}
465
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700466static struct shm_pool *
467shm_pool_create(struct display *display, size_t size)
468{
469 struct shm_pool *pool = malloc(sizeof *pool);
470
471 if (!pool)
472 return NULL;
473
474 pool->pool = make_shm_pool(display, size, &pool->data);
475 if (!pool->pool) {
476 free(pool);
477 return NULL;
478 }
479
480 pool->size = size;
481 pool->used = 0;
482
483 return pool;
484}
485
486static void *
487shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
488{
489 if (pool->used + size > pool->size)
490 return NULL;
491
492 *offset = pool->used;
493 pool->used += size;
494
495 return (char *) pool->data + *offset;
496}
497
498/* destroy the pool. this does not unmap the memory though */
499static void
500shm_pool_destroy(struct shm_pool *pool)
501{
502 munmap(pool->data, pool->size);
503 wl_shm_pool_destroy(pool->pool);
504 free(pool);
505}
506
507/* Start allocating from the beginning of the pool again */
508static void
509shm_pool_reset(struct shm_pool *pool)
510{
511 pool->used = 0;
512}
513
514static int
515data_length_for_shm_surface(struct rectangle *rect)
516{
517 int stride;
518
519 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
520 rect->width);
521 return stride * rect->height;
522}
523
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500524static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700525display_create_shm_surface_from_pool(struct display *display,
526 struct rectangle *rectangle,
527 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528{
529 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400530 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400531 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700532 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400533 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534
535 data = malloc(sizeof *data);
536 if (data == NULL)
537 return NULL;
538
539 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
540 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700541 length = stride * rectangle->height;
542 data->pool = NULL;
543 map = shm_pool_allocate(pool, length, &offset);
544
545 if (!map) {
546 free(data);
547 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400550 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 CAIRO_FORMAT_ARGB32,
552 rectangle->width,
553 rectangle->height,
554 stride);
555
556 cairo_surface_set_user_data (surface, &surface_data_key,
557 data, shm_surface_data_destroy);
558
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500560 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400561 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500562 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400563
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700564 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400565 rectangle->width,
566 rectangle->height,
567 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700569 return surface;
570}
571
572static cairo_surface_t *
573display_create_shm_surface(struct display *display,
574 struct rectangle *rectangle, uint32_t flags,
575 struct window *window)
576{
577 struct shm_surface_data *data;
578 struct shm_pool *pool;
579 cairo_surface_t *surface;
580
581 if (window && window->pool) {
582 shm_pool_reset(window->pool);
583 surface = display_create_shm_surface_from_pool(display,
584 rectangle,
585 flags,
586 window->pool);
587 if (surface)
588 return surface;
589 }
590
591 pool = shm_pool_create(display,
592 data_length_for_shm_surface(rectangle));
593 if (!pool)
594 return NULL;
595
596 surface =
597 display_create_shm_surface_from_pool(display, rectangle,
598 flags, pool);
599
600 if (!surface) {
601 shm_pool_destroy(pool);
602 return NULL;
603 }
604
605 /* make sure we destroy the pool when the surface is destroyed */
606 data = cairo_surface_get_user_data(surface, &surface_data_key);
607 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608
609 return surface;
610}
611
nobled7b87cb02011-02-01 18:51:47 +0000612static int
613check_size(struct rectangle *rect)
614{
615 if (rect->width && rect->height)
616 return 0;
617
618 fprintf(stderr, "tried to create surface of "
619 "width: %d, height: %d\n", rect->width, rect->height);
620 return -1;
621}
622
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623cairo_surface_t *
624display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100625 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400626 struct rectangle *rectangle,
627 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628{
nobled7b87cb02011-02-01 18:51:47 +0000629 if (check_size(rectangle) < 0)
630 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500631#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300632 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400633 return display_create_egl_window_surface(display,
634 surface,
635 flags,
636 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400638 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639}
640
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200641/*
642 * The following correspondences between file names and cursors was copied
643 * from: https://bugs.kde.org/attachment.cgi?id=67313
644 */
645
646static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300647 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200648 "sw-resize"
649};
650
651static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300652 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200653 "se-resize"
654};
655
656static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300657 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200658 "s-resize"
659};
660
661static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300662 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200663 "closedhand",
664 "208530c400c041818281048008011002"
665};
666
667static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200669 "default",
670 "top_left_arrow",
671 "left-arrow"
672};
673
674static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300675 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200676 "w-resize"
677};
678
679static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300680 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200681 "e-resize"
682};
683
684static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300685 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200686 "nw-resize"
687};
688
689static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300690 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200691 "ne-resize"
692};
693
694static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300695 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200696 "n-resize"
697};
698
699static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300700 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200701 "ibeam",
702 "text"
703};
704
705static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300706 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200707 "pointer",
708 "pointing_hand",
709 "e29285e634086352946a0e7090d73106"
710};
711
712static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400713 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200714 "wait",
715 "0426c94ea35c87780ff01dc239897213"
716};
717
718struct cursor_alternatives {
719 const char **names;
720 size_t count;
721};
722
723static const struct cursor_alternatives cursors[] = {
724 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
725 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
726 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
727 {grabbings, ARRAY_LENGTH(grabbings)},
728 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
729 {left_sides, ARRAY_LENGTH(left_sides)},
730 {right_sides, ARRAY_LENGTH(right_sides)},
731 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
732 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
733 {top_sides, ARRAY_LENGTH(top_sides)},
734 {xterms, ARRAY_LENGTH(xterms)},
735 {hand1s, ARRAY_LENGTH(hand1s)},
736 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300737};
738
739static void
740create_cursors(struct display *display)
741{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100742 char *config_file;
743 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200744 unsigned int i, j;
745 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100746 struct config_key shell_keys[] = {
747 { "cursor-theme", CONFIG_KEY_STRING, &theme },
748 };
749 struct config_section cs[] = {
750 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
751 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300752
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100753 config_file = config_file_path("weston.ini");
754 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
755 free(config_file);
756
757 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300758 display->cursors =
759 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
760
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300761 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200762 cursor = NULL;
763 for (j = 0; !cursor && j < cursors[i].count; ++j)
764 cursor = wl_cursor_theme_get_cursor(
765 display->cursor_theme, cursors[i].names[j]);
766
767 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300768 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200769 cursors[i].names[0]);
770
771 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300772 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300773}
774
775static void
776destroy_cursors(struct display *display)
777{
778 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800779 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300780}
781
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300782struct wl_cursor_image *
783display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400784{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200785 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400786
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300787 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400788}
789
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400790static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791window_get_resize_dx_dy(struct window *window, int *x, int *y)
792{
793 if (window->resize_edges & WINDOW_RESIZING_LEFT)
794 *x = window->server_allocation.width - window->allocation.width;
795 else
796 *x = 0;
797
798 if (window->resize_edges & WINDOW_RESIZING_TOP)
799 *y = window->server_allocation.height -
800 window->allocation.height;
801 else
802 *y = 0;
803
804 window->resize_edges = 0;
805}
806
807static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500808window_attach_surface(struct window *window)
809{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400810 struct display *display = window->display;
811 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500815 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400817 if (window->type == TYPE_NONE) {
818 window->type = TYPE_TOPLEVEL;
819 if (display->shell)
820 wl_shell_surface_set_toplevel(window->shell_surface);
821 }
822
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
826 data = cairo_surface_get_user_data(window->cairo_surface,
827 &surface_data_key);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_swapbuffers(window->cairo_surface);
830 wl_egl_window_get_attached_size(data->window,
831 &window->server_allocation.width,
832 &window->server_allocation.height);
833 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 buffer =
837 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400838 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200840 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400842 wl_surface_damage(window->surface, 0, 0,
843 window->allocation.width,
844 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400846 cairo_surface_destroy(window->cairo_surface);
847 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000849 default:
850 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500852
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500853 if (window->input_region) {
854 wl_surface_set_input_region(window->surface,
855 window->input_region);
856 wl_region_destroy(window->input_region);
857 window->input_region = NULL;
858 }
859
860 if (window->opaque_region) {
861 wl_surface_set_opaque_region(window->surface,
862 window->opaque_region);
863 wl_region_destroy(window->opaque_region);
864 window->opaque_region = NULL;
865 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500866}
867
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400868int
869window_has_focus(struct window *window)
870{
871 return window->focus_count > 0;
872}
873
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500874void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400875window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500876{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400877 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100878 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500879}
880
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881void
882window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500884 cairo_surface_reference(surface);
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
888
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890}
891
Benjamin Franzke22d54812011-07-16 19:50:32 +0000892#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893static void
894window_resize_cairo_window_surface(struct window *window)
895{
896 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898
899 data = cairo_surface_get_user_data(window->cairo_surface,
900 &surface_data_key);
901
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 wl_egl_window_resize(data->window,
904 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window->allocation.height,
906 x,y);
907
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 cairo_gl_surface_set_size(window->cairo_surface,
909 window->allocation.width,
910 window->allocation.height);
911}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000912#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400914struct display *
915window_get_display(struct window *window)
916{
917 return window->display;
918}
919
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920void
921window_create_surface(struct window *window)
922{
923 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 uint32_t flags = 0;
925
926 if (!window->transparent)
927 flags = SURFACE_OPAQUE;
928
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500930#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
932 if (window->cairo_surface) {
933 window_resize_cairo_window_surface(window);
934 return;
935 }
936 surface = display_create_surface(window->display,
937 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400938 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940#endif
941 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400943 &window->allocation,
944 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800946 default:
947 surface = NULL;
948 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400950
951 window_set_surface(window, surface);
952 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953}
954
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200955static void frame_destroy(struct frame *frame);
956
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400957void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500958window_destroy(struct window *window)
959{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200960 struct display *display = window->display;
961 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100962 struct window_output *window_output;
963 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200964
965 if (window->redraw_scheduled)
966 wl_list_remove(&window->redraw_task.link);
967
968 wl_list_for_each(input, &display->input_list, link) {
969 if (input->pointer_focus == window)
970 input->pointer_focus = NULL;
971 if (input->keyboard_focus == window)
972 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500973 if (input->focus_widget &&
974 input->focus_widget->window == window)
975 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200976 }
977
Rob Bradford7507b572012-05-15 17:55:34 +0100978 wl_list_for_each_safe(window_output, window_output_tmp,
979 &window->window_output_list, link) {
980 free (window_output);
981 }
982
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500983 if (window->input_region)
984 wl_region_destroy(window->input_region);
985 if (window->opaque_region)
986 wl_region_destroy(window->opaque_region);
987
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200988 if (window->frame)
989 frame_destroy(window->frame);
990
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200991 if (window->shell_surface)
992 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500993 wl_surface_destroy(window->surface);
994 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200995
996 if (window->cairo_surface != NULL)
997 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200998
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300999 if (window->frame_cb)
1000 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001001 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002 free(window);
1003}
1004
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001005static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001009
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001010 wl_list_for_each(child, &widget->child_list, link) {
1011 target = widget_find_widget(child, x, y);
1012 if (target)
1013 return target;
1014 }
1015
1016 if (widget->allocation.x <= x &&
1017 x < widget->allocation.x + widget->allocation.width &&
1018 widget->allocation.y <= y &&
1019 y < widget->allocation.y + widget->allocation.height) {
1020 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021 }
1022
1023 return NULL;
1024}
1025
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001026static struct widget *
1027widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001030
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001031 widget = malloc(sizeof *widget);
1032 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001033 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001034 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001035 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001036 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001037 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001038 widget->tooltip = NULL;
1039 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001040
1041 return widget;
1042}
1043
1044struct widget *
1045window_add_widget(struct window *window, void *data)
1046{
1047 window->widget = widget_create(window, data);
1048 wl_list_init(&window->widget->link);
1049
1050 return window->widget;
1051}
1052
1053struct widget *
1054widget_add_widget(struct widget *parent, void *data)
1055{
1056 struct widget *widget;
1057
1058 widget = widget_create(parent->window, data);
1059 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062}
1063
1064void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001065widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001067 struct display *display = widget->window->display;
1068 struct input *input;
1069
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001070 if (widget->tooltip) {
1071 free(widget->tooltip);
1072 widget->tooltip = NULL;
1073 }
1074
Pekka Paalanene156fb62012-01-19 13:51:38 +02001075 wl_list_for_each(input, &display->input_list, link) {
1076 if (input->focus_widget == widget)
1077 input->focus_widget = NULL;
1078 }
1079
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001080 wl_list_remove(&widget->link);
1081 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001082}
1083
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001084void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088}
1089
1090void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001091widget_set_size(struct widget *widget, int32_t width, int32_t height)
1092{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001093 widget->allocation.width = width;
1094 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001095}
1096
1097void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098widget_set_allocation(struct widget *widget,
1099 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101 widget->allocation.x = x;
1102 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001103 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001104}
1105
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001106void
1107widget_set_transparent(struct widget *widget, int transparent)
1108{
1109 widget->opaque = !transparent;
1110}
1111
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001113widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001114{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001115 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001116}
1117
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001118void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001119widget_set_resize_handler(struct widget *widget,
1120 widget_resize_handler_t handler)
1121{
1122 widget->resize_handler = handler;
1123}
1124
1125void
1126widget_set_redraw_handler(struct widget *widget,
1127 widget_redraw_handler_t handler)
1128{
1129 widget->redraw_handler = handler;
1130}
1131
1132void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001133widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001134{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001135 widget->enter_handler = handler;
1136}
1137
1138void
1139widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1140{
1141 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001142}
1143
1144void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001145widget_set_motion_handler(struct widget *widget,
1146 widget_motion_handler_t handler)
1147{
1148 widget->motion_handler = handler;
1149}
1150
1151void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001152widget_set_button_handler(struct widget *widget,
1153 widget_button_handler_t handler)
1154{
1155 widget->button_handler = handler;
1156}
1157
1158void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001159widget_set_axis_handler(struct widget *widget,
1160 widget_axis_handler_t handler)
1161{
1162 widget->axis_handler = handler;
1163}
1164
1165void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001166widget_schedule_redraw(struct widget *widget)
1167{
1168 window_schedule_redraw(widget->window);
1169}
1170
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001171cairo_surface_t *
1172window_get_surface(struct window *window)
1173{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001174 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001175}
1176
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001177struct wl_surface *
1178window_get_wl_surface(struct window *window)
1179{
1180 return window->surface;
1181}
1182
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001183struct wl_shell_surface *
1184window_get_wl_shell_surface(struct window *window)
1185{
1186 return window->shell_surface;
1187}
1188
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001189static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001190tooltip_redraw_handler(struct widget *widget, void *data)
1191{
1192 cairo_t *cr;
1193 const int32_t r = 3;
1194 struct tooltip *tooltip = data;
1195 int32_t width, height;
1196 struct window *window = widget->window;
1197
1198 cr = cairo_create(window->cairo_surface);
1199 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1200 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1201 cairo_paint(cr);
1202
1203 width = window->allocation.width;
1204 height = window->allocation.height;
1205 rounded_rect(cr, 0, 0, width, height, r);
1206
1207 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1208 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1209 cairo_fill(cr);
1210
1211 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1212 cairo_move_to(cr, 10, 16);
1213 cairo_show_text(cr, tooltip->entry);
1214 cairo_destroy(cr);
1215}
1216
1217static cairo_text_extents_t
1218get_text_extents(struct tooltip *tooltip)
1219{
1220 struct window *window;
1221 cairo_t *cr;
1222 cairo_text_extents_t extents;
1223
1224 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1225 * created yet */
1226 window = tooltip->widget->window->parent;
1227 cr = cairo_create(window->cairo_surface);
1228 cairo_text_extents(cr, tooltip->entry, &extents);
1229 cairo_destroy(cr);
1230
1231 return extents;
1232}
1233
1234static int
1235window_create_tooltip(struct tooltip *tooltip)
1236{
1237 struct widget *parent = tooltip->parent;
1238 struct display *display = parent->window->display;
1239 struct window *window;
1240 const int offset_y = 27;
1241 const int margin = 3;
1242 cairo_text_extents_t extents;
1243
1244 if (tooltip->widget)
1245 return 0;
1246
1247 window = window_create_transient(display, parent->window, tooltip->x,
1248 tooltip->y + offset_y,
1249 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1250 if (!window)
1251 return -1;
1252
1253 tooltip->window = window;
1254 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1255
1256 extents = get_text_extents(tooltip);
1257 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1258 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1259
1260 return 0;
1261}
1262
1263void
1264widget_destroy_tooltip(struct widget *parent)
1265{
1266 struct tooltip *tooltip = parent->tooltip;
1267
1268 parent->tooltip_count = 0;
1269 if (!tooltip)
1270 return;
1271
1272 if (tooltip->widget) {
1273 widget_destroy(tooltip->widget);
1274 window_destroy(tooltip->window);
1275 tooltip->widget = NULL;
1276 tooltip->window = NULL;
1277 }
1278
1279 close(tooltip->tooltip_fd);
1280 free(tooltip->entry);
1281 free(tooltip);
1282 parent->tooltip = NULL;
1283}
1284
1285static void
1286tooltip_func(struct task *task, uint32_t events)
1287{
1288 struct tooltip *tooltip =
1289 container_of(task, struct tooltip, tooltip_task);
1290 uint64_t exp;
1291
Martin Olsson8df662a2012-07-08 03:03:47 +02001292 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1293 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001294 window_create_tooltip(tooltip);
1295}
1296
1297#define TOOLTIP_TIMEOUT 500
1298static int
1299tooltip_timer_reset(struct tooltip *tooltip)
1300{
1301 struct itimerspec its;
1302
1303 its.it_interval.tv_sec = 0;
1304 its.it_interval.tv_nsec = 0;
1305 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1306 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1307 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1308 fprintf(stderr, "could not set timerfd\n: %m");
1309 return -1;
1310 }
1311
1312 return 0;
1313}
1314
1315int
1316widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1317{
1318 struct tooltip *tooltip = parent->tooltip;
1319
1320 parent->tooltip_count++;
1321 if (tooltip) {
1322 tooltip->x = x;
1323 tooltip->y = y;
1324 tooltip_timer_reset(tooltip);
1325 return 0;
1326 }
1327
1328 /* the handler might be triggered too fast via input device motion, so
1329 * we need this check here to make sure tooltip is fully initialized */
1330 if (parent->tooltip_count > 1)
1331 return 0;
1332
1333 tooltip = malloc(sizeof *tooltip);
1334 if (!tooltip)
1335 return -1;
1336
1337 parent->tooltip = tooltip;
1338 tooltip->parent = parent;
1339 tooltip->widget = NULL;
1340 tooltip->window = NULL;
1341 tooltip->x = x;
1342 tooltip->y = y;
1343 tooltip->entry = strdup(entry);
1344 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1345 if (tooltip->tooltip_fd < 0) {
1346 fprintf(stderr, "could not create timerfd\n: %m");
1347 return -1;
1348 }
1349
1350 tooltip->tooltip_task.run = tooltip_func;
1351 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1352 EPOLLIN, &tooltip->tooltip_task);
1353 tooltip_timer_reset(tooltip);
1354
1355 return 0;
1356}
1357
1358static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001359workspace_manager_state(void *data,
1360 struct workspace_manager *workspace_manager,
1361 uint32_t current,
1362 uint32_t count)
1363{
1364 struct display *display = data;
1365
1366 display->workspace = current;
1367 display->workspace_count = count;
1368}
1369
1370static const struct workspace_manager_listener workspace_manager_listener = {
1371 workspace_manager_state
1372};
1373
1374static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375frame_resize_handler(struct widget *widget,
1376 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001377{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001378 struct frame *frame = data;
1379 struct widget *child = frame->child;
1380 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001381 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001382 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001383 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001384 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001386 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001388 switch (widget->window->type) {
1389 case TYPE_FULLSCREEN:
1390 decoration_width = 0;
1391 decoration_height = 0;
1392
1393 allocation.x = 0;
1394 allocation.y = 0;
1395 allocation.width = width;
1396 allocation.height = height;
1397 opaque_margin = 0;
1398
1399 wl_list_for_each(button, &frame->buttons_list, link)
1400 button->widget->opaque = 1;
1401 break;
1402 case TYPE_MAXIMIZED:
1403 decoration_width = t->width * 2;
1404 decoration_height = t->width + t->titlebar_height;
1405
1406 allocation.x = t->width;
1407 allocation.y = t->titlebar_height;
1408 allocation.width = width - decoration_width;
1409 allocation.height = height - decoration_height;
1410
1411 opaque_margin = 0;
1412
1413 wl_list_for_each(button, &frame->buttons_list, link)
1414 button->widget->opaque = 0;
1415 break;
1416 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001417 decoration_width = (t->width + t->margin) * 2;
1418 decoration_height = t->width +
1419 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001420
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001421 allocation.x = t->width + t->margin;
1422 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001423 allocation.width = width - decoration_width;
1424 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001425
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001426 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001427
1428 wl_list_for_each(button, &frame->buttons_list, link)
1429 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001430 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001431 }
1432
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001433 widget_set_allocation(child, allocation.x, allocation.y,
1434 allocation.width, allocation.height);
1435
1436 if (child->resize_handler)
1437 child->resize_handler(child,
1438 allocation.width,
1439 allocation.height,
1440 child->user_data);
1441
Scott Moreauf7e498c2012-05-14 11:39:29 -06001442 width = child->allocation.width + decoration_width;
1443 height = child->allocation.height + decoration_height;
1444
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001445 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1446
Kristian Høgsberg023be102012-07-31 11:59:12 -04001447 if (widget->window->type != TYPE_FULLSCREEN) {
1448 widget->window->input_region =
1449 wl_compositor_create_region(display->compositor);
1450 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001451 shadow_margin, shadow_margin,
1452 width - 2 * shadow_margin,
1453 height - 2 * shadow_margin);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001454 }
1455
Scott Moreauf7e498c2012-05-14 11:39:29 -06001456 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001457
1458 if (child->opaque) {
1459 widget->window->opaque_region =
1460 wl_compositor_create_region(display->compositor);
1461 wl_region_add(widget->window->opaque_region,
1462 opaque_margin, opaque_margin,
1463 widget->allocation.width - 2 * opaque_margin,
1464 widget->allocation.height - 2 * opaque_margin);
1465 }
Martin Minarik1998b152012-05-10 02:04:35 +02001466
1467 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001468 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1469 x_l = t->width + shadow_margin;
1470 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001471 wl_list_for_each(button, &frame->buttons_list, link) {
1472 const int button_padding = 4;
1473 w = cairo_image_surface_get_width(button->icon);
1474 h = cairo_image_surface_get_height(button->icon);
1475
1476 if (button->decoration == FRAME_BUTTON_FANCY)
1477 w += 10;
1478
1479 if (button->align == FRAME_BUTTON_LEFT) {
1480 widget_set_allocation(button->widget,
1481 x_l, y , w + 1, h + 1);
1482 x_l += w;
1483 x_l += button_padding;
1484 } else {
1485 x_r -= w;
1486 widget_set_allocation(button->widget,
1487 x_r, y , w + 1, h + 1);
1488 x_r -= button_padding;
1489 }
1490 }
1491}
1492
1493static int
1494frame_button_enter_handler(struct widget *widget,
1495 struct input *input, float x, float y, void *data)
1496{
1497 struct frame_button *frame_button = data;
1498
1499 widget_schedule_redraw(frame_button->widget);
1500 frame_button->state = FRAME_BUTTON_OVER;
1501
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001502 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001503}
1504
1505static void
1506frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1507{
1508 struct frame_button *frame_button = data;
1509
1510 widget_schedule_redraw(frame_button->widget);
1511 frame_button->state = FRAME_BUTTON_DEFAULT;
1512}
1513
1514static void
1515frame_button_button_handler(struct widget *widget,
1516 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001517 uint32_t button,
1518 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001519{
1520 struct frame_button *frame_button = data;
1521 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001522 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001523
1524 if (button != BTN_LEFT)
1525 return;
1526
1527 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001528 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001529 frame_button->state = FRAME_BUTTON_ACTIVE;
1530 widget_schedule_redraw(frame_button->widget);
1531
1532 if (frame_button->type == FRAME_BUTTON_ICON)
1533 window_show_frame_menu(window, input, time);
1534 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001535 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001536 frame_button->state = FRAME_BUTTON_DEFAULT;
1537 widget_schedule_redraw(frame_button->widget);
1538 break;
1539 }
1540
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001541 if (!was_pressed)
1542 return;
1543
Martin Minarik1998b152012-05-10 02:04:35 +02001544 switch (frame_button->type) {
1545 case FRAME_BUTTON_CLOSE:
1546 if (window->close_handler)
1547 window->close_handler(window->parent,
1548 window->user_data);
1549 else
1550 display_exit(window->display);
1551 break;
1552 case FRAME_BUTTON_MINIMIZE:
1553 fprintf(stderr,"Minimize stub\n");
1554 break;
1555 case FRAME_BUTTON_MAXIMIZE:
1556 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1557 break;
1558 default:
1559 /* Unknown operation */
1560 break;
1561 }
1562}
1563
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001564static int
1565frame_button_motion_handler(struct widget *widget,
1566 struct input *input, uint32_t time,
1567 float x, float y, void *data)
1568{
1569 struct frame_button *frame_button = data;
1570 enum frame_button_pointer previous_button_state = frame_button->state;
1571
1572 /* only track state for a pressed button */
1573 if (input->grab != widget)
1574 return CURSOR_LEFT_PTR;
1575
1576 if (x > widget->allocation.x &&
1577 x < (widget->allocation.x + widget->allocation.width) &&
1578 y > widget->allocation.y &&
1579 y < (widget->allocation.y + widget->allocation.height)) {
1580 frame_button->state = FRAME_BUTTON_ACTIVE;
1581 } else {
1582 frame_button->state = FRAME_BUTTON_DEFAULT;
1583 }
1584
1585 if (frame_button->state != previous_button_state)
1586 widget_schedule_redraw(frame_button->widget);
1587
1588 return CURSOR_LEFT_PTR;
1589}
1590
Martin Minarik1998b152012-05-10 02:04:35 +02001591static void
1592frame_button_redraw_handler(struct widget *widget, void *data)
1593{
1594 struct frame_button *frame_button = data;
1595 cairo_t *cr;
1596 int width, height, x, y;
1597 struct window *window = widget->window;
1598
1599 x = widget->allocation.x;
1600 y = widget->allocation.y;
1601 width = widget->allocation.width;
1602 height = widget->allocation.height;
1603
1604 if (!width)
1605 return;
1606 if (!height)
1607 return;
1608 if (widget->opaque)
1609 return;
1610
1611 cr = cairo_create(window->cairo_surface);
1612
1613 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1614 cairo_set_line_width(cr, 1);
1615
1616 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1617 cairo_rectangle (cr, x, y, 25, 16);
1618
1619 cairo_stroke_preserve(cr);
1620
1621 switch (frame_button->state) {
1622 case FRAME_BUTTON_DEFAULT:
1623 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1624 break;
1625 case FRAME_BUTTON_OVER:
1626 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1627 break;
1628 case FRAME_BUTTON_ACTIVE:
1629 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1630 break;
1631 }
1632
1633 cairo_fill (cr);
1634
1635 x += 4;
1636 }
1637
1638 cairo_set_source_surface(cr, frame_button->icon, x, y);
1639 cairo_paint(cr);
1640
1641 cairo_destroy(cr);
1642}
1643
1644static struct widget *
1645frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1646 enum frame_button_align align, enum frame_button_decoration style)
1647{
1648 struct frame_button *frame_button;
1649 const char *icon = data;
1650
1651 frame_button = malloc (sizeof *frame_button);
1652 memset(frame_button, 0, sizeof *frame_button);
1653
1654 frame_button->icon = cairo_image_surface_create_from_png(icon);
1655 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1656 frame_button->frame = frame;
1657 frame_button->type = type;
1658 frame_button->align = align;
1659 frame_button->decoration = style;
1660
1661 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1662
1663 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1664 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1665 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1666 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001667 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001668 return frame_button->widget;
1669}
1670
1671static void
1672frame_button_destroy(struct frame_button *frame_button)
1673{
1674 widget_destroy(frame_button->widget);
1675 wl_list_remove(&frame_button->link);
1676 cairo_surface_destroy(frame_button->icon);
1677 free(frame_button);
1678
1679 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001680}
1681
1682static void
1683frame_redraw_handler(struct widget *widget, void *data)
1684{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001685 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001686 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001687 struct theme *t = window->display->theme;
1688 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001689
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001690 if (window->type == TYPE_FULLSCREEN)
1691 return;
1692
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001693 cr = cairo_create(window->cairo_surface);
1694
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001695 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001696 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001697 if (window->type == TYPE_MAXIMIZED)
1698 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001699 theme_render_frame(t, cr, widget->allocation.width,
1700 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701
1702 cairo_destroy(cr);
1703}
1704
1705static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001706frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001707{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001708 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001709 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001710 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001711
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001712 location = theme_get_location(t, input->sx, input->sy,
1713 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001714 frame->widget->allocation.height,
1715 window->type == TYPE_MAXIMIZED ?
1716 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001717
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001718 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001719 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001720 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001721 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001722 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001723 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001724 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001725 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001726 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001727 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001728 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001729 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001730 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001731 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001732 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001733 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001734 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001735 case THEME_LOCATION_EXTERIOR:
1736 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001737 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001738 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001739 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001740}
1741
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001742static void
1743frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001744{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001745 struct display *display;
1746
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001747 switch (index) {
1748 case 0: /* close */
1749 if (window->close_handler)
1750 window->close_handler(window->parent,
1751 window->user_data);
1752 else
1753 display_exit(window->display);
1754 break;
1755 case 1: /* fullscreen */
1756 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001757 if (window->fullscreen_handler)
1758 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001759 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001760 case 2: /* move to workspace above */
1761 display = window->display;
1762 if (display->workspace > 0)
1763 workspace_manager_move_surface(display->workspace_manager,
1764 window->surface,
1765 display->workspace - 1);
1766 break;
1767 case 3: /* move to workspace below */
1768 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001769 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001770 workspace_manager_move_surface(display->workspace_manager,
1771 window->surface,
1772 display->workspace + 1);
1773 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001774 }
1775}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001776
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001777void
1778window_show_frame_menu(struct window *window,
1779 struct input *input, uint32_t time)
1780{
1781 int32_t x, y;
1782
1783 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001784 "Close", "Fullscreen",
1785 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001786 };
1787
1788 input_get_position(input, &x, &y);
1789 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001790 x - 10, y - 10, frame_menu_func, entries,
1791 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001792}
1793
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001794static int
1795frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001796 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001797{
1798 return frame_get_pointer_image_for_location(data, input);
1799}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001800
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001801static int
1802frame_motion_handler(struct widget *widget,
1803 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001804 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001805{
1806 return frame_get_pointer_image_for_location(data, input);
1807}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001808
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001810frame_button_handler(struct widget *widget,
1811 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001812 uint32_t button, enum wl_pointer_button_state state,
1813 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001814
1815{
1816 struct frame *frame = data;
1817 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001818 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001819 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001820
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001821 location = theme_get_location(display->theme, input->sx, input->sy,
1822 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001823 frame->widget->allocation.height,
1824 window->type == TYPE_MAXIMIZED ?
1825 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001826
Daniel Stone4dbadb12012-05-30 16:31:51 +01001827 if (window->display->shell && button == BTN_LEFT &&
1828 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001829 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001830 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001831 if (!window->shell_surface)
1832 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001833 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001834 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001835 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001836 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001837 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001838 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001839 case THEME_LOCATION_RESIZING_TOP:
1840 case THEME_LOCATION_RESIZING_BOTTOM:
1841 case THEME_LOCATION_RESIZING_LEFT:
1842 case THEME_LOCATION_RESIZING_RIGHT:
1843 case THEME_LOCATION_RESIZING_TOP_LEFT:
1844 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1845 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1846 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001847 if (!window->shell_surface)
1848 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001849 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001850
1851 if (!display->dpy) {
1852 /* If we're using shm, allocate a big
1853 pool to create buffers out of while
1854 we resize. We should probably base
1855 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001856 window->pool =
1857 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001858 }
1859
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001860 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001861 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001862 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001863 break;
1864 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001865 } else if (button == BTN_RIGHT &&
1866 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001867 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001868 }
1869}
1870
1871struct widget *
1872frame_create(struct window *window, void *data)
1873{
1874 struct frame *frame;
1875
1876 frame = malloc(sizeof *frame);
1877 memset(frame, 0, sizeof *frame);
1878
1879 frame->widget = window_add_widget(window, frame);
1880 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001881
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001882 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1883 widget_set_resize_handler(frame->widget, frame_resize_handler);
1884 widget_set_enter_handler(frame->widget, frame_enter_handler);
1885 widget_set_motion_handler(frame->widget, frame_motion_handler);
1886 widget_set_button_handler(frame->widget, frame_button_handler);
1887
Martin Minarik1998b152012-05-10 02:04:35 +02001888 /* Create empty list for frame buttons */
1889 wl_list_init(&frame->buttons_list);
1890
1891 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1892 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1893
1894 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1895 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1896
1897 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1898 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1899
1900 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1901 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1902
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001903 window->frame = frame;
1904
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001905 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001906}
1907
Kristian Høgsberga1627922012-06-20 17:30:03 -04001908void
1909frame_set_child_size(struct widget *widget, int child_width, int child_height)
1910{
1911 struct display *display = widget->window->display;
1912 struct theme *t = display->theme;
1913 int decoration_width, decoration_height;
1914 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001915 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001916
1917 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001918 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001919 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001920 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001921
1922 width = child_width + decoration_width;
1923 height = child_height + decoration_height;
1924 } else {
1925 width = child_width;
1926 height = child_height;
1927 }
1928
1929 window_schedule_resize(widget->window, width, height);
1930}
1931
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001932static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001933frame_destroy(struct frame *frame)
1934{
Martin Minarik1998b152012-05-10 02:04:35 +02001935 struct frame_button *button, *tmp;
1936
1937 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1938 frame_button_destroy(button);
1939
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001940 /* frame->child must be destroyed by the application */
1941 widget_destroy(frame->widget);
1942 free(frame);
1943}
1944
1945static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001946input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001947 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001948{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001949 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001950 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001951
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001952 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001953 return;
1954
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001955 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001956 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001957 widget = old;
1958 if (input->grab)
1959 widget = input->grab;
1960 if (widget->leave_handler)
1961 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001962 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001963 }
1964
1965 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001966 widget = focus;
1967 if (input->grab)
1968 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001969 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001970 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001971 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001972 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001973
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001974 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001975 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001976}
1977
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001978void
1979input_grab(struct input *input, struct widget *widget, uint32_t button)
1980{
1981 input->grab = widget;
1982 input->grab_button = button;
1983}
1984
1985void
1986input_ungrab(struct input *input)
1987{
1988 struct widget *widget;
1989
1990 input->grab = NULL;
1991 if (input->pointer_focus) {
1992 widget = widget_find_widget(input->pointer_focus->widget,
1993 input->sx, input->sy);
1994 input_set_focus_widget(input, widget, input->sx, input->sy);
1995 }
1996}
1997
1998static void
1999input_remove_pointer_focus(struct input *input)
2000{
2001 struct window *window = input->pointer_focus;
2002
2003 if (!window)
2004 return;
2005
2006 input_set_focus_widget(input, NULL, 0, 0);
2007
2008 input->pointer_focus = NULL;
2009 input->current_cursor = CURSOR_UNSET;
2010}
2011
2012static void
2013pointer_handle_enter(void *data, struct wl_pointer *pointer,
2014 uint32_t serial, struct wl_surface *surface,
2015 wl_fixed_t sx_w, wl_fixed_t sy_w)
2016{
2017 struct input *input = data;
2018 struct window *window;
2019 struct widget *widget;
2020 float sx = wl_fixed_to_double(sx_w);
2021 float sy = wl_fixed_to_double(sy_w);
2022
2023 if (!surface) {
2024 /* enter event for a window we've just destroyed */
2025 return;
2026 }
2027
2028 input->display->serial = serial;
2029 input->pointer_enter_serial = serial;
2030 input->pointer_focus = wl_surface_get_user_data(surface);
2031 window = input->pointer_focus;
2032
2033 if (window->pool) {
2034 shm_pool_destroy(window->pool);
2035 window->pool = NULL;
2036 /* Schedule a redraw to free the pool */
2037 window_schedule_redraw(window);
2038 }
2039
2040 input->sx = sx;
2041 input->sy = sy;
2042
2043 widget = widget_find_widget(window->widget, sx, sy);
2044 input_set_focus_widget(input, widget, sx, sy);
2045}
2046
2047static void
2048pointer_handle_leave(void *data, struct wl_pointer *pointer,
2049 uint32_t serial, struct wl_surface *surface)
2050{
2051 struct input *input = data;
2052
2053 input->display->serial = serial;
2054 input_remove_pointer_focus(input);
2055}
2056
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002057static void
Daniel Stone37816df2012-05-16 18:45:18 +01002058pointer_handle_motion(void *data, struct wl_pointer *pointer,
2059 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002060{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002061 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002062 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002063 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002064 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002065 float sx = wl_fixed_to_double(sx_w);
2066 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002067
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002068 input->sx = sx;
2069 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002070
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002071 if (!window)
2072 return;
2073
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002074 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002075 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002076 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002077 }
2078
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002079 if (input->grab)
2080 widget = input->grab;
2081 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002082 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002083 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002084 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002085 input, time, sx, sy,
2086 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002087
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002088 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002089}
2090
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002091static void
Daniel Stone37816df2012-05-16 18:45:18 +01002092pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002093 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002094{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002095 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002096 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002097 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002098
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002099 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002100 if (input->focus_widget && input->grab == NULL &&
2101 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002102 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002103
Neil Roberts6b28aad2012-01-23 19:11:18 +00002104 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002105 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002106 (*widget->button_handler)(widget,
2107 input, time,
2108 button, state,
2109 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002110
Daniel Stone4dbadb12012-05-30 16:31:51 +01002111 if (input->grab && input->grab_button == button &&
2112 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002113 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002114}
2115
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002116static void
Daniel Stone37816df2012-05-16 18:45:18 +01002117pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002118 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002119{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002120 struct input *input = data;
2121 struct widget *widget;
2122
2123 widget = input->focus_widget;
2124 if (input->grab)
2125 widget = input->grab;
2126 if (widget && widget->axis_handler)
2127 (*widget->axis_handler)(widget,
2128 input, time,
2129 axis, value,
2130 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002131}
2132
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002133static const struct wl_pointer_listener pointer_listener = {
2134 pointer_handle_enter,
2135 pointer_handle_leave,
2136 pointer_handle_motion,
2137 pointer_handle_button,
2138 pointer_handle_axis,
2139};
2140
2141static void
2142input_remove_keyboard_focus(struct input *input)
2143{
2144 struct window *window = input->keyboard_focus;
2145 struct itimerspec its;
2146
2147 its.it_interval.tv_sec = 0;
2148 its.it_interval.tv_nsec = 0;
2149 its.it_value.tv_sec = 0;
2150 its.it_value.tv_nsec = 0;
2151 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2152
2153 if (!window)
2154 return;
2155
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002156 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002157 if (window->keyboard_focus_handler)
2158 (*window->keyboard_focus_handler)(window, NULL,
2159 window->user_data);
2160
2161 input->keyboard_focus = NULL;
2162}
2163
2164static void
2165keyboard_repeat_func(struct task *task, uint32_t events)
2166{
2167 struct input *input =
2168 container_of(task, struct input, repeat_task);
2169 struct window *window = input->keyboard_focus;
2170 uint64_t exp;
2171
2172 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2173 /* If we change the timer between the fd becoming
2174 * readable and getting here, there'll be nothing to
2175 * read and we get EAGAIN. */
2176 return;
2177
2178 if (window && window->key_handler) {
2179 (*window->key_handler)(window, input, input->repeat_time,
2180 input->repeat_key, input->repeat_sym,
2181 WL_KEYBOARD_KEY_STATE_PRESSED,
2182 window->user_data);
2183 }
2184}
2185
2186static void
2187keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2188 uint32_t format, int fd, uint32_t size)
2189{
2190 struct input *input = data;
2191 char *map_str;
2192
2193 if (!data) {
2194 close(fd);
2195 return;
2196 }
2197
2198 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2199 close(fd);
2200 return;
2201 }
2202
2203 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2204 if (map_str == MAP_FAILED) {
2205 close(fd);
2206 return;
2207 }
2208
2209 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2210 map_str,
2211 XKB_KEYMAP_FORMAT_TEXT_V1,
2212 0);
2213 munmap(map_str, size);
2214 close(fd);
2215
2216 if (!input->xkb.keymap) {
2217 fprintf(stderr, "failed to compile keymap\n");
2218 return;
2219 }
2220
2221 input->xkb.state = xkb_state_new(input->xkb.keymap);
2222 if (!input->xkb.state) {
2223 fprintf(stderr, "failed to create XKB state\n");
2224 xkb_map_unref(input->xkb.keymap);
2225 input->xkb.keymap = NULL;
2226 return;
2227 }
2228
2229 input->xkb.control_mask =
2230 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2231 input->xkb.alt_mask =
2232 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2233 input->xkb.shift_mask =
2234 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2235}
2236
2237static void
2238keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2239 uint32_t serial, struct wl_surface *surface,
2240 struct wl_array *keys)
2241{
2242 struct input *input = data;
2243 struct window *window;
2244
2245 input->display->serial = serial;
2246 input->keyboard_focus = wl_surface_get_user_data(surface);
2247
2248 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002249 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002250 if (window->keyboard_focus_handler)
2251 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002252 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002253}
2254
2255static void
2256keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2257 uint32_t serial, struct wl_surface *surface)
2258{
2259 struct input *input = data;
2260
2261 input->display->serial = serial;
2262 input_remove_keyboard_focus(input);
2263}
2264
Scott Moreau210d0792012-03-22 10:47:01 -06002265static void
Daniel Stone37816df2012-05-16 18:45:18 +01002266keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002267 uint32_t serial, uint32_t time, uint32_t key,
2268 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002269{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002270 struct input *input = data;
2271 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002272 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002273 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002274 const xkb_keysym_t *syms;
2275 xkb_keysym_t sym;
2276 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002277 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002278
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002279 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002280 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002281 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002282 return;
2283
Daniel Stone97f68542012-05-30 16:32:01 +01002284 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002285
Daniel Stone97f68542012-05-30 16:32:01 +01002286 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002287 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002288 XKB_STATE_LATCHED);
2289 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002290 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002291 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002292 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002293 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002294 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002295 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002296
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002297 sym = XKB_KEY_NoSymbol;
2298 if (num_syms == 1)
2299 sym = syms[0];
2300
2301 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002302 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002303 window_set_maximized(window,
2304 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002305 } else if (sym == XKB_KEY_F11 &&
2306 window->fullscreen_handler &&
2307 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2308 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002309 } else if (sym == XKB_KEY_F4 &&
2310 input->modifiers == MOD_ALT_MASK &&
2311 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2312 if (window->close_handler)
2313 window->close_handler(window->parent,
2314 window->user_data);
2315 else
2316 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002317 } else if (window->key_handler) {
2318 (*window->key_handler)(window, input, time, key,
2319 sym, state, window->user_data);
2320 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002321
2322 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2323 key == input->repeat_key) {
2324 its.it_interval.tv_sec = 0;
2325 its.it_interval.tv_nsec = 0;
2326 its.it_value.tv_sec = 0;
2327 its.it_value.tv_nsec = 0;
2328 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2329 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2330 input->repeat_sym = sym;
2331 input->repeat_key = key;
2332 input->repeat_time = time;
2333 its.it_interval.tv_sec = 0;
2334 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2335 its.it_value.tv_sec = 0;
2336 its.it_value.tv_nsec = 400 * 1000 * 1000;
2337 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2338 }
2339}
2340
2341static void
Daniel Stone351eb612012-05-31 15:27:47 -04002342keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2343 uint32_t serial, uint32_t mods_depressed,
2344 uint32_t mods_latched, uint32_t mods_locked,
2345 uint32_t group)
2346{
2347 struct input *input = data;
2348
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002349 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2350 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002351}
2352
Daniel Stone37816df2012-05-16 18:45:18 +01002353static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002354 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002355 keyboard_handle_enter,
2356 keyboard_handle_leave,
2357 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002358 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002359};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002360
2361static void
Daniel Stone37816df2012-05-16 18:45:18 +01002362seat_handle_capabilities(void *data, struct wl_seat *seat,
2363 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002364{
Daniel Stone37816df2012-05-16 18:45:18 +01002365 struct input *input = data;
2366
2367 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2368 input->pointer = wl_seat_get_pointer(seat);
2369 wl_pointer_set_user_data(input->pointer, input);
2370 wl_pointer_add_listener(input->pointer, &pointer_listener,
2371 input);
2372 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2373 wl_pointer_destroy(input->pointer);
2374 input->pointer = NULL;
2375 }
2376
2377 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2378 input->keyboard = wl_seat_get_keyboard(seat);
2379 wl_keyboard_set_user_data(input->keyboard, input);
2380 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2381 input);
2382 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2383 wl_keyboard_destroy(input->keyboard);
2384 input->keyboard = NULL;
2385 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002386}
2387
Daniel Stone37816df2012-05-16 18:45:18 +01002388static const struct wl_seat_listener seat_listener = {
2389 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002390};
2391
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002392void
2393input_get_position(struct input *input, int32_t *x, int32_t *y)
2394{
2395 *x = input->sx;
2396 *y = input->sy;
2397}
2398
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002399struct display *
2400input_get_display(struct input *input)
2401{
2402 return input->display;
2403}
2404
Daniel Stone37816df2012-05-16 18:45:18 +01002405struct wl_seat *
2406input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002407{
Daniel Stone37816df2012-05-16 18:45:18 +01002408 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002409}
2410
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002411uint32_t
2412input_get_modifiers(struct input *input)
2413{
2414 return input->modifiers;
2415}
2416
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002417struct widget *
2418input_get_focus_widget(struct input *input)
2419{
2420 return input->focus_widget;
2421}
2422
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002423struct data_offer {
2424 struct wl_data_offer *offer;
2425 struct input *input;
2426 struct wl_array types;
2427 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002428
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002429 struct task io_task;
2430 int fd;
2431 data_func_t func;
2432 int32_t x, y;
2433 void *user_data;
2434};
2435
2436static void
2437data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2438{
2439 struct data_offer *offer = data;
2440 char **p;
2441
2442 p = wl_array_add(&offer->types, sizeof *p);
2443 *p = strdup(type);
2444}
2445
2446static const struct wl_data_offer_listener data_offer_listener = {
2447 data_offer_offer,
2448};
2449
2450static void
2451data_offer_destroy(struct data_offer *offer)
2452{
2453 char **p;
2454
2455 offer->refcount--;
2456 if (offer->refcount == 0) {
2457 wl_data_offer_destroy(offer->offer);
2458 for (p = offer->types.data; *p; p++)
2459 free(*p);
2460 wl_array_release(&offer->types);
2461 free(offer);
2462 }
2463}
2464
2465static void
2466data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002467 struct wl_data_device *data_device,
2468 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002469{
2470 struct data_offer *offer;
2471
2472 offer = malloc(sizeof *offer);
2473
2474 wl_array_init(&offer->types);
2475 offer->refcount = 1;
2476 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002477 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002478 wl_data_offer_add_listener(offer->offer,
2479 &data_offer_listener, offer);
2480}
2481
2482static void
2483data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002484 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002485 wl_fixed_t x_w, wl_fixed_t y_w,
2486 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002487{
2488 struct input *input = data;
2489 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002490 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002491 float x = wl_fixed_to_double(x_w);
2492 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002493 char **p;
2494
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002495 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002496 window = wl_surface_get_user_data(surface);
2497 input->pointer_focus = window;
2498
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002499 if (offer) {
2500 input->drag_offer = wl_data_offer_get_user_data(offer);
2501
2502 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2503 *p = NULL;
2504
2505 types_data = input->drag_offer->types.data;
2506 } else {
2507 input->drag_offer = NULL;
2508 types_data = NULL;
2509 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002510
2511 window = input->pointer_focus;
2512 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002513 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002514 window->user_data);
2515}
2516
2517static void
2518data_device_leave(void *data, struct wl_data_device *data_device)
2519{
2520 struct input *input = data;
2521
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002522 if (input->drag_offer) {
2523 data_offer_destroy(input->drag_offer);
2524 input->drag_offer = NULL;
2525 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002526}
2527
2528static void
2529data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002530 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002531{
2532 struct input *input = data;
2533 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002534 float x = wl_fixed_to_double(x_w);
2535 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002536 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002537
2538 input->sx = x;
2539 input->sy = y;
2540
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002541 if (input->drag_offer)
2542 types_data = input->drag_offer->types.data;
2543 else
2544 types_data = NULL;
2545
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002546 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002547 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002548 window->user_data);
2549}
2550
2551static void
2552data_device_drop(void *data, struct wl_data_device *data_device)
2553{
2554 struct input *input = data;
2555 struct window *window = input->pointer_focus;
2556
2557 if (window->drop_handler)
2558 window->drop_handler(window, input,
2559 input->sx, input->sy, window->user_data);
2560}
2561
2562static void
2563data_device_selection(void *data,
2564 struct wl_data_device *wl_data_device,
2565 struct wl_data_offer *offer)
2566{
2567 struct input *input = data;
2568 char **p;
2569
2570 if (input->selection_offer)
2571 data_offer_destroy(input->selection_offer);
2572
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002573 if (offer) {
2574 input->selection_offer = wl_data_offer_get_user_data(offer);
2575 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2576 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002577 } else {
2578 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002579 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002580}
2581
2582static const struct wl_data_device_listener data_device_listener = {
2583 data_device_data_offer,
2584 data_device_enter,
2585 data_device_leave,
2586 data_device_motion,
2587 data_device_drop,
2588 data_device_selection
2589};
2590
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002591static void
2592input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002593{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002594 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002595 struct wl_cursor *cursor;
2596 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002597
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002598 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002599 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002600 return;
2601
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002602 if (index >= (int) cursor->image_count) {
2603 fprintf(stderr, "cursor index out of range\n");
2604 return;
2605 }
2606
2607 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002608 buffer = wl_cursor_image_get_buffer(image);
2609 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002610 return;
2611
Kristian Høgsbergae277372012-08-01 09:41:08 -04002612 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002613 input->pointer_surface,
2614 image->hotspot_x, image->hotspot_y);
2615 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2616 wl_surface_damage(input->pointer_surface, 0, 0,
2617 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002618}
2619
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002620static const struct wl_callback_listener pointer_surface_listener;
2621
2622static void
2623pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2624 uint32_t time)
2625{
2626 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002627 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002628 int i;
2629
2630 if (callback) {
2631 assert(callback == input->cursor_frame_cb);
2632 wl_callback_destroy(callback);
2633 input->cursor_frame_cb = NULL;
2634 }
2635
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002636 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002637 wl_pointer_set_cursor(input->pointer,
2638 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002639 NULL, 0, 0);
2640 return;
2641 }
2642
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002643 if (input->current_cursor == CURSOR_UNSET)
2644 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002645 cursor = input->display->cursors[input->current_cursor];
2646 if (!cursor)
2647 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002648
2649 /* FIXME We don't have the current time on the first call so we set
2650 * the animation start to the time of the first frame callback. */
2651 if (time == 0)
2652 input->cursor_anim_start = 0;
2653 else if (input->cursor_anim_start == 0)
2654 input->cursor_anim_start = time;
2655
2656 if (time == 0 || input->cursor_anim_start == 0)
2657 i = 0;
2658 else
2659 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2660
2661 input_set_pointer_image_index(input, i);
2662
2663 if (cursor->image_count == 1)
2664 return;
2665
2666 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2667 wl_callback_add_listener(input->cursor_frame_cb,
2668 &pointer_surface_listener, input);
2669}
2670
2671static const struct wl_callback_listener pointer_surface_listener = {
2672 pointer_surface_frame_callback
2673};
2674
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002675void
2676input_set_pointer_image(struct input *input, int pointer)
2677{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002678 int force = 0;
2679
2680 if (input->pointer_enter_serial > input->cursor_serial)
2681 force = 1;
2682
2683 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002684 return;
2685
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002686 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002687 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002688 if (!input->cursor_frame_cb)
2689 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002690 else if (force) {
2691 /* The current frame callback may be stuck if, for instance,
2692 * the set cursor request was processed by the server after
2693 * this client lost the focus. In this case the cursor surface
2694 * might not be mapped and the frame callback wouldn't ever
2695 * complete. Send a set_cursor and attach to try to map the
2696 * cursor surface again so that the callback will finish */
2697 input_set_pointer_image_index(input, 0);
2698 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002699}
2700
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002701struct wl_data_device *
2702input_get_data_device(struct input *input)
2703{
2704 return input->data_device;
2705}
2706
2707void
2708input_set_selection(struct input *input,
2709 struct wl_data_source *source, uint32_t time)
2710{
2711 wl_data_device_set_selection(input->data_device, source, time);
2712}
2713
2714void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002715input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002716{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002717 wl_data_offer_accept(input->drag_offer->offer,
2718 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002719}
2720
2721static void
2722offer_io_func(struct task *task, uint32_t events)
2723{
2724 struct data_offer *offer =
2725 container_of(task, struct data_offer, io_task);
2726 unsigned int len;
2727 char buffer[4096];
2728
2729 len = read(offer->fd, buffer, sizeof buffer);
2730 offer->func(buffer, len,
2731 offer->x, offer->y, offer->user_data);
2732
2733 if (len == 0) {
2734 close(offer->fd);
2735 data_offer_destroy(offer);
2736 }
2737}
2738
2739static void
2740data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2741 data_func_t func, void *user_data)
2742{
2743 int p[2];
2744
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002745 if (pipe2(p, O_CLOEXEC) == -1)
2746 return;
2747
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002748 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2749 close(p[1]);
2750
2751 offer->io_task.run = offer_io_func;
2752 offer->fd = p[0];
2753 offer->func = func;
2754 offer->refcount++;
2755 offer->user_data = user_data;
2756
2757 display_watch_fd(offer->input->display,
2758 offer->fd, EPOLLIN, &offer->io_task);
2759}
2760
2761void
2762input_receive_drag_data(struct input *input, const char *mime_type,
2763 data_func_t func, void *data)
2764{
2765 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2766 input->drag_offer->x = input->sx;
2767 input->drag_offer->y = input->sy;
2768}
2769
2770int
2771input_receive_selection_data(struct input *input, const char *mime_type,
2772 data_func_t func, void *data)
2773{
2774 char **p;
2775
2776 if (input->selection_offer == NULL)
2777 return -1;
2778
2779 for (p = input->selection_offer->types.data; *p; p++)
2780 if (strcmp(mime_type, *p) == 0)
2781 break;
2782
2783 if (*p == NULL)
2784 return -1;
2785
2786 data_offer_receive_data(input->selection_offer,
2787 mime_type, func, data);
2788 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002789}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002790
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002791int
2792input_receive_selection_data_to_fd(struct input *input,
2793 const char *mime_type, int fd)
2794{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002795 if (input->selection_offer)
2796 wl_data_offer_receive(input->selection_offer->offer,
2797 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002798
2799 return 0;
2800}
2801
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002802void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002804{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002805 if (!window->shell_surface)
2806 return;
2807
Daniel Stone37816df2012-05-16 18:45:18 +01002808 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002809}
2810
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002811static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002812idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002813{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002814 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002815
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002816 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002817 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002818 widget_set_allocation(widget,
2819 window->pending_allocation.x,
2820 window->pending_allocation.y,
2821 window->pending_allocation.width,
2822 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002823
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002824 if (window->input_region) {
2825 wl_region_destroy(window->input_region);
2826 window->input_region = NULL;
2827 }
2828
2829 if (window->opaque_region) {
2830 wl_region_destroy(window->opaque_region);
2831 window->opaque_region = NULL;
2832 }
2833
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002834 if (widget->resize_handler)
2835 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002836 widget->allocation.width,
2837 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002838 widget->user_data);
2839
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002840 if (window->allocation.width != widget->allocation.width ||
2841 window->allocation.height != widget->allocation.height) {
2842 window->allocation = widget->allocation;
2843 window_schedule_redraw(window);
2844 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002845}
2846
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002847void
2848window_schedule_resize(struct window *window, int width, int height)
2849{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002850 window->pending_allocation.x = 0;
2851 window->pending_allocation.y = 0;
2852 window->pending_allocation.width = width;
2853 window->pending_allocation.height = height;
2854
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002855 if (window->min_allocation.width == 0)
2856 window->min_allocation = window->pending_allocation;
2857 if (window->pending_allocation.width < window->min_allocation.width)
2858 window->pending_allocation.width = window->min_allocation.width;
2859 if (window->pending_allocation.height < window->min_allocation.height)
2860 window->pending_allocation.height = window->min_allocation.height;
2861
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002862 window->resize_needed = 1;
2863 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002864}
2865
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002866void
2867widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2868{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002869 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002870}
2871
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002872static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002873handle_ping(void *data, struct wl_shell_surface *shell_surface,
2874 uint32_t serial)
2875{
2876 wl_shell_surface_pong(shell_surface, serial);
2877}
2878
2879static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002880handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002881 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002882{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002883 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002884
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002885 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002886 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002887}
2888
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002889static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002890menu_destroy(struct menu *menu)
2891{
2892 widget_destroy(menu->widget);
2893 window_destroy(menu->window);
2894 free(menu);
2895}
2896
2897static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002898handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2899{
2900 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002901 struct menu *menu = window->widget->user_data;
2902
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002903 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002904 * device. Or just use wl_callback. And this really needs to
2905 * be a window vfunc that the menu can set. And we need the
2906 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002907
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002908 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002909 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002910 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002911}
2912
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002913static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002914 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002915 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002917};
2918
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002919void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002920window_get_allocation(struct window *window,
2921 struct rectangle *allocation)
2922{
2923 *allocation = window->allocation;
2924}
2925
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002926static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002927widget_redraw(struct widget *widget)
2928{
2929 struct widget *child;
2930
2931 if (widget->redraw_handler)
2932 widget->redraw_handler(widget, widget->user_data);
2933 wl_list_for_each(child, &widget->child_list, link)
2934 widget_redraw(child);
2935}
2936
2937static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002938frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2939{
2940 struct window *window = data;
2941
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002942 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002943 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002944 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002945 window->redraw_scheduled = 0;
2946 if (window->redraw_needed)
2947 window_schedule_redraw(window);
2948}
2949
2950static const struct wl_callback_listener listener = {
2951 frame_callback
2952};
2953
2954static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002955idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002956{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002957 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002958
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002959 if (window->resize_needed)
2960 idle_resize(window);
2961
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002962 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002963 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002964 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002965 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002966 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002967
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002968 window->frame_cb = wl_surface_frame(window->surface);
2969 wl_callback_add_listener(window->frame_cb, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002970}
2971
2972void
2973window_schedule_redraw(struct window *window)
2974{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002975 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002976 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002977 window->redraw_task.run = idle_redraw;
2978 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002979 window->redraw_scheduled = 1;
2980 }
2981}
2982
Kristian Høgsberg1671e112012-10-10 11:36:24 -04002983int
2984window_is_fullscreen(struct window *window)
2985{
2986 return window->type == TYPE_FULLSCREEN;
2987}
2988
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002989void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002990window_set_fullscreen(struct window *window, int fullscreen)
2991{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002992 if (!window->display->shell)
2993 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002994
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002995 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002996 return;
2997
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002998 if (fullscreen) {
2999 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003000 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003001 wl_shell_surface_set_fullscreen(window->shell_surface,
3002 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3003 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003004 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003005 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003006 wl_shell_surface_set_toplevel(window->shell_surface);
3007 window_schedule_resize(window,
3008 window->saved_allocation.width,
3009 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003010 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003011}
3012
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003013int
3014window_is_maximized(struct window *window)
3015{
3016 return window->type == TYPE_MAXIMIZED;
3017}
3018
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003019void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003020window_set_maximized(struct window *window, int maximized)
3021{
3022 if (!window->display->shell)
3023 return;
3024
3025 if ((window->type == TYPE_MAXIMIZED) == maximized)
3026 return;
3027
3028 if (window->type == TYPE_TOPLEVEL) {
3029 window->saved_allocation = window->allocation;
3030 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3031 window->type = TYPE_MAXIMIZED;
3032 } else {
3033 wl_shell_surface_set_toplevel(window->shell_surface);
3034 window->type = TYPE_TOPLEVEL;
3035 window_schedule_resize(window,
3036 window->saved_allocation.width,
3037 window->saved_allocation.height);
3038 }
3039}
3040
3041void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003042window_set_user_data(struct window *window, void *data)
3043{
3044 window->user_data = data;
3045}
3046
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003047void *
3048window_get_user_data(struct window *window)
3049{
3050 return window->user_data;
3051}
3052
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003053void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003054window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003055 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003056{
3057 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003058}
3059
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003060void
3061window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003062 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003063{
3064 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003065}
3066
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003067void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003068window_set_data_handler(struct window *window, window_data_handler_t handler)
3069{
3070 window->data_handler = handler;
3071}
3072
3073void
3074window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3075{
3076 window->drop_handler = handler;
3077}
3078
3079void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003080window_set_close_handler(struct window *window,
3081 window_close_handler_t handler)
3082{
3083 window->close_handler = handler;
3084}
3085
3086void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003087window_set_fullscreen_handler(struct window *window,
3088 window_fullscreen_handler_t handler)
3089{
3090 window->fullscreen_handler = handler;
3091}
3092
3093void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003094window_set_title(struct window *window, const char *title)
3095{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003096 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003097 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003098 if (window->shell_surface)
3099 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003100}
3101
3102const char *
3103window_get_title(struct window *window)
3104{
3105 return window->title;
3106}
3107
3108void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003109window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3110{
3111 struct text_cursor_position *text_cursor_position =
3112 window->display->text_cursor_position;
3113
Scott Moreau9295ce02012-06-01 12:46:10 -06003114 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003115 return;
3116
3117 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003118 window->surface,
3119 wl_fixed_from_int(x),
3120 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003121}
3122
3123void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003124window_damage(struct window *window, int32_t x, int32_t y,
3125 int32_t width, int32_t height)
3126{
3127 wl_surface_damage(window->surface, x, y, width, height);
3128}
3129
Casey Dahlin9074db52012-04-19 22:50:09 -04003130static void
3131surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003132 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003133{
Rob Bradford7507b572012-05-15 17:55:34 +01003134 struct window *window = data;
3135 struct output *output;
3136 struct output *output_found = NULL;
3137 struct window_output *window_output;
3138
3139 wl_list_for_each(output, &window->display->output_list, link) {
3140 if (output->output == wl_output) {
3141 output_found = output;
3142 break;
3143 }
3144 }
3145
3146 if (!output_found)
3147 return;
3148
3149 window_output = malloc (sizeof *window_output);
3150 window_output->output = output_found;
3151
3152 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003153}
3154
3155static void
3156surface_leave(void *data,
3157 struct wl_surface *wl_surface, struct wl_output *output)
3158{
Rob Bradford7507b572012-05-15 17:55:34 +01003159 struct window *window = data;
3160 struct window_output *window_output;
3161 struct window_output *window_output_found = NULL;
3162
3163 wl_list_for_each(window_output, &window->window_output_list, link) {
3164 if (window_output->output->output == output) {
3165 window_output_found = window_output;
3166 break;
3167 }
3168 }
3169
3170 if (window_output_found) {
3171 wl_list_remove(&window_output_found->link);
3172 free(window_output_found);
3173 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003174}
3175
3176static const struct wl_surface_listener surface_listener = {
3177 surface_enter,
3178 surface_leave
3179};
3180
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003181static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003182window_create_internal(struct display *display,
3183 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003184{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003185 struct window *window;
3186
3187 window = malloc(sizeof *window);
3188 if (window == NULL)
3189 return NULL;
3190
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003191 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003192 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003193 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003194 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003195 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003196 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003197 window->shell_surface =
3198 wl_shell_get_shell_surface(display->shell,
3199 window->surface);
3200 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003201 window->allocation.x = 0;
3202 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003203 window->allocation.width = 0;
3204 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003205 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003206 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003207 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003208 window->input_region = NULL;
3209 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003210
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003211 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003212#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003213 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003214#else
3215 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3216#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003217 else
3218 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003219
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003220 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003221 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003222 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003223
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003224 if (window->shell_surface) {
3225 wl_shell_surface_set_user_data(window->shell_surface, window);
3226 wl_shell_surface_add_listener(window->shell_surface,
3227 &shell_surface_listener, window);
3228 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003229
Rob Bradford7507b572012-05-15 17:55:34 +01003230 wl_list_init (&window->window_output_list);
3231
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003232 return window;
3233}
3234
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003235struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003236window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003237{
3238 struct window *window;
3239
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003240 window = window_create_internal(display, NULL, TYPE_NONE);
3241 if (!window)
3242 return NULL;
3243
3244 return window;
3245}
3246
3247struct window *
3248window_create_custom(struct display *display)
3249{
3250 struct window *window;
3251
3252 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003253 if (!window)
3254 return NULL;
3255
3256 return window;
3257}
3258
3259struct window *
3260window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003261 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003262{
3263 struct window *window;
3264
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003265 window = window_create_internal(parent->display,
3266 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003267 if (!window)
3268 return NULL;
3269
3270 window->x = x;
3271 window->y = y;
3272
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003273 if (display->shell)
3274 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003275 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003276 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003277
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003278 return window;
3279}
3280
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003281static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003282menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003283{
3284 int next;
3285
3286 next = (sy - 8) / 20;
3287 if (menu->current != next) {
3288 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003289 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003290 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003291}
3292
3293static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003294menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003295 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003296 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003297{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003298 struct menu *menu = data;
3299
3300 if (widget == menu->widget)
3301 menu_set_item(data, y);
3302
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003303 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003304}
3305
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003306static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003307menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003308 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003309{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003310 struct menu *menu = data;
3311
3312 if (widget == menu->widget)
3313 menu_set_item(data, y);
3314
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003315 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003316}
3317
3318static void
3319menu_leave_handler(struct widget *widget, struct input *input, void *data)
3320{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003321 struct menu *menu = data;
3322
3323 if (widget == menu->widget)
3324 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003325}
3326
3327static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003328menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003329 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003330 uint32_t button, enum wl_pointer_button_state state,
3331 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003332
3333{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003334 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003335
Daniel Stone4dbadb12012-05-30 16:31:51 +01003336 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3337 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003338 /* Either relase after press-drag-release or
3339 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003340 menu->func(menu->window->parent,
3341 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003342 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003343 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003344 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003345}
3346
3347static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003348menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003349{
3350 cairo_t *cr;
3351 const int32_t r = 3, margin = 3;
3352 struct menu *menu = data;
3353 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003354 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003355
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003356 cr = cairo_create(window->cairo_surface);
3357 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3358 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3359 cairo_paint(cr);
3360
3361 width = window->allocation.width;
3362 height = window->allocation.height;
3363 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003364
3365 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003366 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3367 cairo_fill(cr);
3368
3369 for (i = 0; i < menu->count; i++) {
3370 if (i == menu->current) {
3371 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3372 cairo_rectangle(cr, margin, i * 20 + margin,
3373 width - 2 * margin, 20);
3374 cairo_fill(cr);
3375 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3376 cairo_move_to(cr, 10, i * 20 + 16);
3377 cairo_show_text(cr, menu->entries[i]);
3378 } else {
3379 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3380 cairo_move_to(cr, 10, i * 20 + 16);
3381 cairo_show_text(cr, menu->entries[i]);
3382 }
3383 }
3384
3385 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003386}
3387
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003388void
3389window_show_menu(struct display *display,
3390 struct input *input, uint32_t time, struct window *parent,
3391 int32_t x, int32_t y,
3392 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003393{
3394 struct window *window;
3395 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003396 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003397
3398 menu = malloc(sizeof *menu);
3399 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003400 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003401
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003402 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003403 if (!window) {
3404 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003405 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003406 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003407
3408 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003409 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003410 menu->entries = entries;
3411 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003412 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003413 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003414 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003415 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003416 window->type = TYPE_MENU;
3417 window->x = x;
3418 window->y = y;
3419
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003420 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003421 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003422 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003423 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003424 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003425
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003426 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003427 widget_set_enter_handler(menu->widget, menu_enter_handler);
3428 widget_set_leave_handler(menu->widget, menu_leave_handler);
3429 widget_set_motion_handler(menu->widget, menu_motion_handler);
3430 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003431
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003432 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003433 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003434}
3435
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003436void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003437window_set_buffer_type(struct window *window, enum window_buffer_type type)
3438{
3439 window->buffer_type = type;
3440}
3441
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003442
3443static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003444display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003445 struct wl_output *wl_output,
3446 int x, int y,
3447 int physical_width,
3448 int physical_height,
3449 int subpixel,
3450 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003451 const char *model,
3452 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003453{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003454 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003455
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003456 output->allocation.x = x;
3457 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06003458 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003459}
3460
3461static void
3462display_handle_mode(void *data,
3463 struct wl_output *wl_output,
3464 uint32_t flags,
3465 int width,
3466 int height,
3467 int refresh)
3468{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003469 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003470 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003471
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003472 if (flags & WL_OUTPUT_MODE_CURRENT) {
3473 output->allocation.width = width;
3474 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003475 if (display->output_configure_handler)
3476 (*display->output_configure_handler)(
3477 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003478 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003479}
3480
3481static const struct wl_output_listener output_listener = {
3482 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003483 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003484};
3485
3486static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003487display_add_output(struct display *d, uint32_t id)
3488{
3489 struct output *output;
3490
3491 output = malloc(sizeof *output);
3492 if (output == NULL)
3493 return;
3494
3495 memset(output, 0, sizeof *output);
3496 output->display = d;
3497 output->output =
3498 wl_display_bind(d->display, id, &wl_output_interface);
3499 wl_list_insert(d->output_list.prev, &output->link);
3500
3501 wl_output_add_listener(output->output, &output_listener, output);
3502}
3503
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003504static void
3505output_destroy(struct output *output)
3506{
3507 if (output->destroy_handler)
3508 (*output->destroy_handler)(output, output->user_data);
3509
3510 wl_output_destroy(output->output);
3511 wl_list_remove(&output->link);
3512 free(output);
3513}
3514
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003515void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003516display_set_output_configure_handler(struct display *display,
3517 display_output_handler_t handler)
3518{
3519 struct output *output;
3520
3521 display->output_configure_handler = handler;
3522 if (!handler)
3523 return;
3524
3525 wl_list_for_each(output, &display->output_list, link)
3526 (*display->output_configure_handler)(output,
3527 display->user_data);
3528}
3529
3530void
3531output_set_user_data(struct output *output, void *data)
3532{
3533 output->user_data = data;
3534}
3535
3536void *
3537output_get_user_data(struct output *output)
3538{
3539 return output->user_data;
3540}
3541
3542void
3543output_set_destroy_handler(struct output *output,
3544 display_output_handler_t handler)
3545{
3546 output->destroy_handler = handler;
3547 /* FIXME: implement this, once we have way to remove outputs */
3548}
3549
3550void
Scott Moreau4e072362012-09-29 02:03:11 -06003551output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003552{
Scott Moreau4e072362012-09-29 02:03:11 -06003553 struct rectangle allocation = output->allocation;
3554
3555 switch (output->transform) {
3556 case WL_OUTPUT_TRANSFORM_90:
3557 case WL_OUTPUT_TRANSFORM_270:
3558 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3559 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3560 /* Swap width and height */
3561 allocation.width = output->allocation.height;
3562 allocation.height = output->allocation.width;
3563 break;
3564 }
3565
3566 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003567}
3568
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003569struct wl_output *
3570output_get_wl_output(struct output *output)
3571{
3572 return output->output;
3573}
3574
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003575static void
Daniel Stone97f68542012-05-30 16:32:01 +01003576fini_xkb(struct input *input)
3577{
3578 xkb_state_unref(input->xkb.state);
3579 xkb_map_unref(input->xkb.keymap);
3580}
3581
3582static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003583display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003584{
3585 struct input *input;
3586
3587 input = malloc(sizeof *input);
3588 if (input == NULL)
3589 return;
3590
3591 memset(input, 0, sizeof *input);
3592 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003593 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003594 input->pointer_focus = NULL;
3595 input->keyboard_focus = NULL;
3596 wl_list_insert(d->input_list.prev, &input->link);
3597
Daniel Stone37816df2012-05-16 18:45:18 +01003598 wl_seat_add_listener(input->seat, &seat_listener, input);
3599 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003600
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601 input->data_device =
3602 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003603 input->seat);
3604 wl_data_device_add_listener(input->data_device, &data_device_listener,
3605 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003606
3607 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003608
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003609 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3610 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003611 input->repeat_task.run = keyboard_repeat_func;
3612 display_watch_fd(d, input->repeat_timer_fd,
3613 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003614}
3615
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003616static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003617input_destroy(struct input *input)
3618{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003619 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003620 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003621
3622 if (input->drag_offer)
3623 data_offer_destroy(input->drag_offer);
3624
3625 if (input->selection_offer)
3626 data_offer_destroy(input->selection_offer);
3627
3628 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003629 fini_xkb(input);
3630
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003631 wl_surface_destroy(input->pointer_surface);
3632
Pekka Paalanene1207c72011-12-16 12:02:09 +02003633 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003634 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003635 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003636 free(input);
3637}
3638
3639static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003640init_workspace_manager(struct display *d, uint32_t id)
3641{
3642 d->workspace_manager =
3643 wl_display_bind(d->display, id, &workspace_manager_interface);
3644 if (d->workspace_manager != NULL)
3645 workspace_manager_add_listener(d->workspace_manager,
3646 &workspace_manager_listener,
3647 d);
3648}
3649
3650static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003651display_handle_global(struct wl_display *display, uint32_t id,
3652 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003653{
3654 struct display *d = data;
3655
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003656 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003657 d->compositor =
3658 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003659 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003660 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003661 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003662 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003663 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003664 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003665 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003666 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003667 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3668 d->data_device_manager =
3669 wl_display_bind(display, id,
3670 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003671 } else if (strcmp(interface, "text_cursor_position") == 0) {
3672 d->text_cursor_position =
3673 wl_display_bind(display, id,
3674 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003675 } else if (strcmp(interface, "workspace_manager") == 0) {
3676 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003677 }
3678}
3679
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003680#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003681static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003682init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003683{
3684 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003685 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003686
Rob Clark6396ed32012-03-11 19:48:41 -05003687#ifdef USE_CAIRO_GLESV2
3688# define GL_BIT EGL_OPENGL_ES2_BIT
3689#else
3690# define GL_BIT EGL_OPENGL_BIT
3691#endif
3692
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003693 static const EGLint argb_cfg_attribs[] = {
3694 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003695 EGL_RED_SIZE, 1,
3696 EGL_GREEN_SIZE, 1,
3697 EGL_BLUE_SIZE, 1,
3698 EGL_ALPHA_SIZE, 1,
3699 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003700 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003701 EGL_NONE
3702 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003703
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003704#ifdef USE_CAIRO_GLESV2
3705 static const EGLint context_attribs[] = {
3706 EGL_CONTEXT_CLIENT_VERSION, 2,
3707 EGL_NONE
3708 };
3709 EGLint api = EGL_OPENGL_ES_API;
3710#else
3711 EGLint *context_attribs = NULL;
3712 EGLint api = EGL_OPENGL_API;
3713#endif
3714
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003715 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003716 if (!eglInitialize(d->dpy, &major, &minor)) {
3717 fprintf(stderr, "failed to initialize display\n");
3718 return -1;
3719 }
3720
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003721 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003722 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3723 return -1;
3724 }
3725
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003726 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3727 &d->argb_config, 1, &n) || n != 1) {
3728 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003729 return -1;
3730 }
3731
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003732 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003733 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003734 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003735 fprintf(stderr, "failed to create context\n");
3736 return -1;
3737 }
3738
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003739 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003740 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003741 return -1;
3742 }
3743
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003744#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003745 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3746 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3747 fprintf(stderr, "failed to get cairo egl argb device\n");
3748 return -1;
3749 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003750#endif
3751
3752 return 0;
3753}
3754
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003755static void
3756fini_egl(struct display *display)
3757{
3758#ifdef HAVE_CAIRO_EGL
3759 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003760#endif
3761
3762 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3763 EGL_NO_CONTEXT);
3764
3765 eglTerminate(display->dpy);
3766 eglReleaseThread();
3767}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003768#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003769
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003770static int
3771event_mask_update(uint32_t mask, void *data)
3772{
3773 struct display *d = data;
3774
3775 d->mask = mask;
3776
3777 return 0;
3778}
3779
3780static void
3781handle_display_data(struct task *task, uint32_t events)
3782{
3783 struct display *display =
3784 container_of(task, struct display, display_task);
U. Artie Eoff44874d92012-10-02 21:12:35 -07003785
3786 display->display_fd_events = events;
3787
3788 if (events & EPOLLERR || events & EPOLLHUP) {
3789 display_exit(display);
3790 return;
3791 }
3792
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003793 wl_display_iterate(display->display, display->mask);
3794}
3795
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003796struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003797display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003798{
3799 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003800
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003801 d = malloc(sizeof *d);
3802 if (d == NULL)
3803 return NULL;
3804
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003805 memset(d, 0, sizeof *d);
3806
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003807 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003808 if (d->display == NULL) {
3809 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003810 return NULL;
3811 }
3812
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003813 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003814 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3815 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003816 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3817 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003818
3819 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003820 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003821 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003822
Daniel Stone97f68542012-05-30 16:32:01 +01003823 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003824 if (d->xkb_context == NULL) {
3825 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003826 return NULL;
3827 }
3828
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003829 d->workspace = 0;
3830 d->workspace_count = 1;
3831
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003832 /* Set up listener so we'll catch all events. */
3833 wl_display_add_global_listener(d->display,
3834 display_handle_global, d);
3835
3836 /* Process connection events. */
3837 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003838#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003839 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003840 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003841#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003842
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003843 d->image_target_texture_2d =
3844 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3845 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3846 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3847
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003848 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003849
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003850 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003851
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003852 wl_list_init(&d->window_list);
3853
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003854 return d;
3855}
3856
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003857static void
3858display_destroy_outputs(struct display *display)
3859{
3860 struct output *tmp;
3861 struct output *output;
3862
3863 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3864 output_destroy(output);
3865}
3866
Pekka Paalanene1207c72011-12-16 12:02:09 +02003867static void
3868display_destroy_inputs(struct display *display)
3869{
3870 struct input *tmp;
3871 struct input *input;
3872
3873 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3874 input_destroy(input);
3875}
3876
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003877void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003878display_destroy(struct display *display)
3879{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003880 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003881 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3882 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003883
3884 if (!wl_list_empty(&display->deferred_list))
3885 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3886
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003887 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003888 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003889
Daniel Stone97f68542012-05-30 16:32:01 +01003890 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003891
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003892 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003893 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003894
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003895#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003896 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003897#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003898
Pekka Paalanenc2052982011-12-16 11:41:32 +02003899 if (display->shell)
3900 wl_shell_destroy(display->shell);
3901
3902 if (display->shm)
3903 wl_shm_destroy(display->shm);
3904
3905 if (display->data_device_manager)
3906 wl_data_device_manager_destroy(display->data_device_manager);
3907
3908 wl_compositor_destroy(display->compositor);
3909
3910 close(display->epoll_fd);
3911
U. Artie Eoff44874d92012-10-02 21:12:35 -07003912 if (!(display->display_fd_events & EPOLLERR) &&
3913 !(display->display_fd_events & EPOLLHUP))
3914 wl_display_flush(display->display);
3915
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003916 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003917 free(display);
3918}
3919
3920void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003921display_set_user_data(struct display *display, void *data)
3922{
3923 display->user_data = data;
3924}
3925
3926void *
3927display_get_user_data(struct display *display)
3928{
3929 return display->user_data;
3930}
3931
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003932struct wl_display *
3933display_get_display(struct display *display)
3934{
3935 return display->display;
3936}
3937
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003938struct output *
3939display_get_output(struct display *display)
3940{
3941 return container_of(display->output_list.next, struct output, link);
3942}
3943
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003944struct wl_compositor *
3945display_get_compositor(struct display *display)
3946{
3947 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003948}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003949
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003950uint32_t
3951display_get_serial(struct display *display)
3952{
3953 return display->serial;
3954}
3955
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003956EGLDisplay
3957display_get_egl_display(struct display *d)
3958{
3959 return d->dpy;
3960}
3961
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003962struct wl_data_source *
3963display_create_data_source(struct display *display)
3964{
3965 return wl_data_device_manager_create_data_source(display->data_device_manager);
3966}
3967
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003968EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003969display_get_argb_egl_config(struct display *d)
3970{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003971 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003972}
3973
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003974struct wl_shell *
3975display_get_shell(struct display *display)
3976{
3977 return display->shell;
3978}
3979
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003980int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003981display_acquire_window_surface(struct display *display,
3982 struct window *window,
3983 EGLContext ctx)
3984{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003985#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003986 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003987 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003988
3989 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003990 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003991 device = cairo_surface_get_device(window->cairo_surface);
3992 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003993 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003994
Benjamin Franzke0c991632011-09-27 21:57:31 +02003995 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003996 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003997 ctx = display->argb_ctx;
3998 else
3999 assert(0);
4000 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004001
4002 data = cairo_surface_get_user_data(window->cairo_surface,
4003 &surface_data_key);
4004
Pekka Paalanen9015ead2011-12-19 13:57:59 +02004005 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004006 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004007 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4008 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02004009
4010 return 0;
4011#else
4012 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004013#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004014}
4015
4016void
Benjamin Franzke0c991632011-09-27 21:57:31 +02004017display_release_window_surface(struct display *display,
4018 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004019{
Benjamin Franzke0c991632011-09-27 21:57:31 +02004020#ifdef HAVE_CAIRO_EGL
4021 cairo_device_t *device;
4022
4023 device = cairo_surface_get_device(window->cairo_surface);
4024 if (!device)
4025 return;
4026
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004027 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004028 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004029 cairo_device_release(device);
4030#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004031}
4032
4033void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004034display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004035{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004036 wl_list_insert(&display->deferred_list, &task->link);
4037}
4038
4039void
4040display_watch_fd(struct display *display,
4041 int fd, uint32_t events, struct task *task)
4042{
4043 struct epoll_event ep;
4044
4045 ep.events = events;
4046 ep.data.ptr = task;
4047 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4048}
4049
4050void
4051display_run(struct display *display)
4052{
4053 struct task *task;
4054 struct epoll_event ep[16];
4055 int i, count;
4056
Pekka Paalanen826d7952011-12-15 10:14:07 +02004057 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004058 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004059 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004060
4061 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004062 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004063 struct task, link);
4064 wl_list_remove(&task->link);
4065 task->run(task, 0);
4066 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004067
4068 if (!display->running)
4069 break;
4070
4071 wl_display_flush(display->display);
4072
4073 count = epoll_wait(display->epoll_fd,
4074 ep, ARRAY_LENGTH(ep), -1);
4075 for (i = 0; i < count; i++) {
4076 task = ep[i].data.ptr;
4077 task->run(task, ep[i].events);
4078 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004079 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004080}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004081
4082void
4083display_exit(struct display *display)
4084{
4085 display->running = 0;
4086}