blob: 4df771d744b2d75811608af4534efb2b4c733dbf [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;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200241
242 display_output_handler_t destroy_handler;
243 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500244};
245
Martin Minarik1998b152012-05-10 02:04:35 +0200246enum frame_button_action {
247 FRAME_BUTTON_NULL = 0,
248 FRAME_BUTTON_ICON = 1,
249 FRAME_BUTTON_CLOSE = 2,
250 FRAME_BUTTON_MINIMIZE = 3,
251 FRAME_BUTTON_MAXIMIZE = 4,
252};
253
254enum frame_button_pointer {
255 FRAME_BUTTON_DEFAULT = 0,
256 FRAME_BUTTON_OVER = 1,
257 FRAME_BUTTON_ACTIVE = 2,
258};
259
260enum frame_button_align {
261 FRAME_BUTTON_RIGHT = 0,
262 FRAME_BUTTON_LEFT = 1,
263};
264
265enum frame_button_decoration {
266 FRAME_BUTTON_NONE = 0,
267 FRAME_BUTTON_FANCY = 1,
268};
269
270struct frame_button {
271 struct widget *widget;
272 struct frame *frame;
273 cairo_surface_t *icon;
274 enum frame_button_action type;
275 enum frame_button_pointer state;
276 struct wl_list link; /* buttons_list */
277 enum frame_button_align align;
278 enum frame_button_decoration decoration;
279};
280
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500281struct frame {
282 struct widget *widget;
283 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200284 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500285};
286
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500287struct menu {
288 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500289 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500290 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500291 const char **entries;
292 uint32_t time;
293 int current;
294 int count;
295 menu_func_t func;
296};
297
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300298struct tooltip {
299 struct widget *parent;
300 struct window *window;
301 struct widget *widget;
302 char *entry;
303 struct task tooltip_task;
304 int tooltip_fd;
305 float x, y;
306};
307
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700308struct shm_pool {
309 struct wl_shm_pool *pool;
310 size_t size;
311 size_t used;
312 void *data;
313};
314
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400315enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300316 CURSOR_DEFAULT = 100,
317 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400318};
319
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500320enum window_location {
321 WINDOW_INTERIOR = 0,
322 WINDOW_RESIZING_TOP = 1,
323 WINDOW_RESIZING_BOTTOM = 2,
324 WINDOW_RESIZING_LEFT = 4,
325 WINDOW_RESIZING_TOP_LEFT = 5,
326 WINDOW_RESIZING_BOTTOM_LEFT = 6,
327 WINDOW_RESIZING_RIGHT = 8,
328 WINDOW_RESIZING_TOP_RIGHT = 9,
329 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
330 WINDOW_RESIZING_MASK = 15,
331 WINDOW_EXTERIOR = 16,
332 WINDOW_TITLEBAR = 17,
333 WINDOW_CLIENT_AREA = 18,
334};
335
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336static const cairo_user_data_key_t surface_data_key;
337struct surface_data {
338 struct wl_buffer *buffer;
339};
340
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500341#define MULT(_d,c,a,t) \
342 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
343
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500344#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100346struct egl_window_surface_data {
347 struct display *display;
348 struct wl_surface *surface;
349 struct wl_egl_window *window;
350 EGLSurface surf;
351};
352
353static void
354egl_window_surface_data_destroy(void *p)
355{
356 struct egl_window_surface_data *data = p;
357 struct display *d = data->display;
358
359 eglDestroySurface(d->dpy, data->surf);
360 wl_egl_window_destroy(data->window);
361 data->surface = NULL;
362
363 free(p);
364}
365
366static cairo_surface_t *
367display_create_egl_window_surface(struct display *display,
368 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100370 struct rectangle *rectangle)
371{
372 cairo_surface_t *cairo_surface;
373 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376
377 data = malloc(sizeof *data);
378 if (data == NULL)
379 return NULL;
380
381 data->display = display;
382 data->surface = surface;
383
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500384 config = display->argb_config;
385 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400386
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400387 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100388 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400389 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100390
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400391 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500392 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100393
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100395 data->surf,
396 rectangle->width,
397 rectangle->height);
398
399 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
400 data, egl_window_surface_data_destroy);
401
402 return cairo_surface;
403}
404
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405#endif
406
407struct wl_buffer *
408display_get_buffer_for_surface(struct display *display,
409 cairo_surface_t *surface)
410{
411 struct surface_data *data;
412
413 data = cairo_surface_get_user_data (surface, &surface_data_key);
414
415 return data->buffer;
416}
417
418struct shm_surface_data {
419 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700420 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421};
422
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500423static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700424shm_pool_destroy(struct shm_pool *pool);
425
426static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427shm_surface_data_destroy(void *p)
428{
429 struct shm_surface_data *data = p;
430
431 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700432 if (data->pool)
433 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300434
435 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436}
437
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438static struct wl_shm_pool *
439make_shm_pool(struct display *display, int size, void **data)
440{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400441 struct wl_shm_pool *pool;
442 int fd;
443
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300444 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400445 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300446 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
447 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400448 return NULL;
449 }
450
451 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400452 if (*data == MAP_FAILED) {
453 fprintf(stderr, "mmap failed: %m\n");
454 close(fd);
455 return NULL;
456 }
457
458 pool = wl_shm_create_pool(display->shm, fd, size);
459
460 close(fd);
461
462 return pool;
463}
464
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700465static struct shm_pool *
466shm_pool_create(struct display *display, size_t size)
467{
468 struct shm_pool *pool = malloc(sizeof *pool);
469
470 if (!pool)
471 return NULL;
472
473 pool->pool = make_shm_pool(display, size, &pool->data);
474 if (!pool->pool) {
475 free(pool);
476 return NULL;
477 }
478
479 pool->size = size;
480 pool->used = 0;
481
482 return pool;
483}
484
485static void *
486shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
487{
488 if (pool->used + size > pool->size)
489 return NULL;
490
491 *offset = pool->used;
492 pool->used += size;
493
494 return (char *) pool->data + *offset;
495}
496
497/* destroy the pool. this does not unmap the memory though */
498static void
499shm_pool_destroy(struct shm_pool *pool)
500{
501 munmap(pool->data, pool->size);
502 wl_shm_pool_destroy(pool->pool);
503 free(pool);
504}
505
506/* Start allocating from the beginning of the pool again */
507static void
508shm_pool_reset(struct shm_pool *pool)
509{
510 pool->used = 0;
511}
512
513static int
514data_length_for_shm_surface(struct rectangle *rect)
515{
516 int stride;
517
518 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
519 rect->width);
520 return stride * rect->height;
521}
522
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500523static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524display_create_shm_surface_from_pool(struct display *display,
525 struct rectangle *rectangle,
526 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527{
528 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400529 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400530 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400532 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533
534 data = malloc(sizeof *data);
535 if (data == NULL)
536 return NULL;
537
538 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
539 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700540 length = stride * rectangle->height;
541 data->pool = NULL;
542 map = shm_pool_allocate(pool, length, &offset);
543
544 if (!map) {
545 free(data);
546 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400547 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400549 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 CAIRO_FORMAT_ARGB32,
551 rectangle->width,
552 rectangle->height,
553 stride);
554
555 cairo_surface_set_user_data (surface, &surface_data_key,
556 data, shm_surface_data_destroy);
557
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500559 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400560 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500561 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400562
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700563 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400564 rectangle->width,
565 rectangle->height,
566 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700568 return surface;
569}
570
571static cairo_surface_t *
572display_create_shm_surface(struct display *display,
573 struct rectangle *rectangle, uint32_t flags,
574 struct window *window)
575{
576 struct shm_surface_data *data;
577 struct shm_pool *pool;
578 cairo_surface_t *surface;
579
580 if (window && window->pool) {
581 shm_pool_reset(window->pool);
582 surface = display_create_shm_surface_from_pool(display,
583 rectangle,
584 flags,
585 window->pool);
586 if (surface)
587 return surface;
588 }
589
590 pool = shm_pool_create(display,
591 data_length_for_shm_surface(rectangle));
592 if (!pool)
593 return NULL;
594
595 surface =
596 display_create_shm_surface_from_pool(display, rectangle,
597 flags, pool);
598
599 if (!surface) {
600 shm_pool_destroy(pool);
601 return NULL;
602 }
603
604 /* make sure we destroy the pool when the surface is destroyed */
605 data = cairo_surface_get_user_data(surface, &surface_data_key);
606 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607
608 return surface;
609}
610
nobled7b87cb02011-02-01 18:51:47 +0000611static int
612check_size(struct rectangle *rect)
613{
614 if (rect->width && rect->height)
615 return 0;
616
617 fprintf(stderr, "tried to create surface of "
618 "width: %d, height: %d\n", rect->width, rect->height);
619 return -1;
620}
621
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622cairo_surface_t *
623display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400625 struct rectangle *rectangle,
626 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627{
nobled7b87cb02011-02-01 18:51:47 +0000628 if (check_size(rectangle) < 0)
629 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500630#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300631 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400632 return display_create_egl_window_surface(display,
633 surface,
634 flags,
635 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400637 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638}
639
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200640/*
641 * The following correspondences between file names and cursors was copied
642 * from: https://bugs.kde.org/attachment.cgi?id=67313
643 */
644
645static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300646 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200647 "sw-resize"
648};
649
650static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300651 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200652 "se-resize"
653};
654
655static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300656 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200657 "s-resize"
658};
659
660static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300661 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200662 "closedhand",
663 "208530c400c041818281048008011002"
664};
665
666static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300667 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200668 "default",
669 "top_left_arrow",
670 "left-arrow"
671};
672
673static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200675 "w-resize"
676};
677
678static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300679 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200680 "e-resize"
681};
682
683static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300684 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200685 "nw-resize"
686};
687
688static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300689 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200690 "ne-resize"
691};
692
693static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300694 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200695 "n-resize"
696};
697
698static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300699 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200700 "ibeam",
701 "text"
702};
703
704static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300705 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200706 "pointer",
707 "pointing_hand",
708 "e29285e634086352946a0e7090d73106"
709};
710
711static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400712 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200713 "wait",
714 "0426c94ea35c87780ff01dc239897213"
715};
716
717struct cursor_alternatives {
718 const char **names;
719 size_t count;
720};
721
722static const struct cursor_alternatives cursors[] = {
723 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
724 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
725 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
726 {grabbings, ARRAY_LENGTH(grabbings)},
727 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
728 {left_sides, ARRAY_LENGTH(left_sides)},
729 {right_sides, ARRAY_LENGTH(right_sides)},
730 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
731 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
732 {top_sides, ARRAY_LENGTH(top_sides)},
733 {xterms, ARRAY_LENGTH(xterms)},
734 {hand1s, ARRAY_LENGTH(hand1s)},
735 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300736};
737
738static void
739create_cursors(struct display *display)
740{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100741 char *config_file;
742 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200743 unsigned int i, j;
744 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100745 struct config_key shell_keys[] = {
746 { "cursor-theme", CONFIG_KEY_STRING, &theme },
747 };
748 struct config_section cs[] = {
749 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
750 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300751
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100752 config_file = config_file_path("weston.ini");
753 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
754 free(config_file);
755
756 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300757 display->cursors =
758 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
759
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300760 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200761 cursor = NULL;
762 for (j = 0; !cursor && j < cursors[i].count; ++j)
763 cursor = wl_cursor_theme_get_cursor(
764 display->cursor_theme, cursors[i].names[j]);
765
766 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300767 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200768 cursors[i].names[0]);
769
770 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300771 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300772}
773
774static void
775destroy_cursors(struct display *display)
776{
777 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800778 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300779}
780
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300781struct wl_cursor_image *
782display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400783{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200784 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400785
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300786 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400787}
788
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400789static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200790window_get_resize_dx_dy(struct window *window, int *x, int *y)
791{
792 if (window->resize_edges & WINDOW_RESIZING_LEFT)
793 *x = window->server_allocation.width - window->allocation.width;
794 else
795 *x = 0;
796
797 if (window->resize_edges & WINDOW_RESIZING_TOP)
798 *y = window->server_allocation.height -
799 window->allocation.height;
800 else
801 *y = 0;
802
803 window->resize_edges = 0;
804}
805
806static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500807window_attach_surface(struct window *window)
808{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400809 struct display *display = window->display;
810 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000811#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500814 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400816 if (window->type == TYPE_NONE) {
817 window->type = TYPE_TOPLEVEL;
818 if (display->shell)
819 wl_shell_surface_set_toplevel(window->shell_surface);
820 }
821
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
825 data = cairo_surface_get_user_data(window->cairo_surface,
826 &surface_data_key);
827
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 cairo_gl_surface_swapbuffers(window->cairo_surface);
829 wl_egl_window_get_attached_size(data->window,
830 &window->server_allocation.width,
831 &window->server_allocation.height);
832 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 buffer =
836 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400837 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200839 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400841 wl_surface_damage(window->surface, 0, 0,
842 window->allocation.width,
843 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400845 cairo_surface_destroy(window->cairo_surface);
846 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000848 default:
849 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100850 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500851
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500852 if (window->input_region) {
853 wl_surface_set_input_region(window->surface,
854 window->input_region);
855 wl_region_destroy(window->input_region);
856 window->input_region = NULL;
857 }
858
859 if (window->opaque_region) {
860 wl_surface_set_opaque_region(window->surface,
861 window->opaque_region);
862 wl_region_destroy(window->opaque_region);
863 window->opaque_region = NULL;
864 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500865}
866
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400867int
868window_has_focus(struct window *window)
869{
870 return window->focus_count > 0;
871}
872
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500873void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400874window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500875{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400876 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100877 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500878}
879
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400880void
881window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500883 cairo_surface_reference(surface);
884
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400885 if (window->cairo_surface != NULL)
886 cairo_surface_destroy(window->cairo_surface);
887
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500888 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889}
890
Benjamin Franzke22d54812011-07-16 19:50:32 +0000891#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892static void
893window_resize_cairo_window_surface(struct window *window)
894{
895 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200896 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897
898 data = cairo_surface_get_user_data(window->cairo_surface,
899 &surface_data_key);
900
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200901 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902 wl_egl_window_resize(data->window,
903 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 window->allocation.height,
905 x,y);
906
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907 cairo_gl_surface_set_size(window->cairo_surface,
908 window->allocation.width,
909 window->allocation.height);
910}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000911#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400913struct display *
914window_get_display(struct window *window)
915{
916 return window->display;
917}
918
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400919void
920window_create_surface(struct window *window)
921{
922 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400923 uint32_t flags = 0;
924
925 if (!window->transparent)
926 flags = SURFACE_OPAQUE;
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500929#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
931 if (window->cairo_surface) {
932 window_resize_cairo_window_surface(window);
933 return;
934 }
935 surface = display_create_surface(window->display,
936 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400937 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400939#endif
940 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400941 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400942 &window->allocation,
943 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800945 default:
946 surface = NULL;
947 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400948 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949
950 window_set_surface(window, surface);
951 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952}
953
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200954static void frame_destroy(struct frame *frame);
955
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400956void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500957window_destroy(struct window *window)
958{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200959 struct display *display = window->display;
960 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100961 struct window_output *window_output;
962 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200963
964 if (window->redraw_scheduled)
965 wl_list_remove(&window->redraw_task.link);
966
967 wl_list_for_each(input, &display->input_list, link) {
968 if (input->pointer_focus == window)
969 input->pointer_focus = NULL;
970 if (input->keyboard_focus == window)
971 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500972 if (input->focus_widget &&
973 input->focus_widget->window == window)
974 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200975 }
976
Rob Bradford7507b572012-05-15 17:55:34 +0100977 wl_list_for_each_safe(window_output, window_output_tmp,
978 &window->window_output_list, link) {
979 free (window_output);
980 }
981
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500982 if (window->input_region)
983 wl_region_destroy(window->input_region);
984 if (window->opaque_region)
985 wl_region_destroy(window->opaque_region);
986
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200987 if (window->frame)
988 frame_destroy(window->frame);
989
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200990 if (window->shell_surface)
991 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500992 wl_surface_destroy(window->surface);
993 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200994
995 if (window->cairo_surface != NULL)
996 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200997
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300998 if (window->frame_cb)
999 wl_callback_destroy(window->frame_cb);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001000 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001001 free(window);
1002}
1003
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001004static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001007 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001009 wl_list_for_each(child, &widget->child_list, link) {
1010 target = widget_find_widget(child, x, y);
1011 if (target)
1012 return target;
1013 }
1014
1015 if (widget->allocation.x <= x &&
1016 x < widget->allocation.x + widget->allocation.width &&
1017 widget->allocation.y <= y &&
1018 y < widget->allocation.y + widget->allocation.height) {
1019 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020 }
1021
1022 return NULL;
1023}
1024
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001025static struct widget *
1026widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001029
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001030 widget = malloc(sizeof *widget);
1031 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001032 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001033 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001034 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001035 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001036 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001037 widget->tooltip = NULL;
1038 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001039
1040 return widget;
1041}
1042
1043struct widget *
1044window_add_widget(struct window *window, void *data)
1045{
1046 window->widget = widget_create(window, data);
1047 wl_list_init(&window->widget->link);
1048
1049 return window->widget;
1050}
1051
1052struct widget *
1053widget_add_widget(struct widget *parent, void *data)
1054{
1055 struct widget *widget;
1056
1057 widget = widget_create(parent->window, data);
1058 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061}
1062
1063void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001064widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001066 struct display *display = widget->window->display;
1067 struct input *input;
1068
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001069 if (widget->tooltip) {
1070 free(widget->tooltip);
1071 widget->tooltip = NULL;
1072 }
1073
Pekka Paalanene156fb62012-01-19 13:51:38 +02001074 wl_list_for_each(input, &display->input_list, link) {
1075 if (input->focus_widget == widget)
1076 input->focus_widget = NULL;
1077 }
1078
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001079 wl_list_remove(&widget->link);
1080 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081}
1082
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001083void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
1089void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001090widget_set_size(struct widget *widget, int32_t width, int32_t height)
1091{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001092 widget->allocation.width = width;
1093 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001094}
1095
1096void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001097widget_set_allocation(struct widget *widget,
1098 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100 widget->allocation.x = x;
1101 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001102 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103}
1104
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001105void
1106widget_set_transparent(struct widget *widget, int transparent)
1107{
1108 widget->opaque = !transparent;
1109}
1110
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001111void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001112widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001113{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001114 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001115}
1116
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001117void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001118widget_set_resize_handler(struct widget *widget,
1119 widget_resize_handler_t handler)
1120{
1121 widget->resize_handler = handler;
1122}
1123
1124void
1125widget_set_redraw_handler(struct widget *widget,
1126 widget_redraw_handler_t handler)
1127{
1128 widget->redraw_handler = handler;
1129}
1130
1131void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001132widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001133{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001134 widget->enter_handler = handler;
1135}
1136
1137void
1138widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1139{
1140 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001141}
1142
1143void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001144widget_set_motion_handler(struct widget *widget,
1145 widget_motion_handler_t handler)
1146{
1147 widget->motion_handler = handler;
1148}
1149
1150void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001151widget_set_button_handler(struct widget *widget,
1152 widget_button_handler_t handler)
1153{
1154 widget->button_handler = handler;
1155}
1156
1157void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001158widget_set_axis_handler(struct widget *widget,
1159 widget_axis_handler_t handler)
1160{
1161 widget->axis_handler = handler;
1162}
1163
1164void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001165widget_schedule_redraw(struct widget *widget)
1166{
1167 window_schedule_redraw(widget->window);
1168}
1169
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001170cairo_surface_t *
1171window_get_surface(struct window *window)
1172{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001173 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001174}
1175
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001176struct wl_surface *
1177window_get_wl_surface(struct window *window)
1178{
1179 return window->surface;
1180}
1181
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001182struct wl_shell_surface *
1183window_get_wl_shell_surface(struct window *window)
1184{
1185 return window->shell_surface;
1186}
1187
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001188static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001189tooltip_redraw_handler(struct widget *widget, void *data)
1190{
1191 cairo_t *cr;
1192 const int32_t r = 3;
1193 struct tooltip *tooltip = data;
1194 int32_t width, height;
1195 struct window *window = widget->window;
1196
1197 cr = cairo_create(window->cairo_surface);
1198 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1199 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1200 cairo_paint(cr);
1201
1202 width = window->allocation.width;
1203 height = window->allocation.height;
1204 rounded_rect(cr, 0, 0, width, height, r);
1205
1206 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1207 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1208 cairo_fill(cr);
1209
1210 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1211 cairo_move_to(cr, 10, 16);
1212 cairo_show_text(cr, tooltip->entry);
1213 cairo_destroy(cr);
1214}
1215
1216static cairo_text_extents_t
1217get_text_extents(struct tooltip *tooltip)
1218{
1219 struct window *window;
1220 cairo_t *cr;
1221 cairo_text_extents_t extents;
1222
1223 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1224 * created yet */
1225 window = tooltip->widget->window->parent;
1226 cr = cairo_create(window->cairo_surface);
1227 cairo_text_extents(cr, tooltip->entry, &extents);
1228 cairo_destroy(cr);
1229
1230 return extents;
1231}
1232
1233static int
1234window_create_tooltip(struct tooltip *tooltip)
1235{
1236 struct widget *parent = tooltip->parent;
1237 struct display *display = parent->window->display;
1238 struct window *window;
1239 const int offset_y = 27;
1240 const int margin = 3;
1241 cairo_text_extents_t extents;
1242
1243 if (tooltip->widget)
1244 return 0;
1245
1246 window = window_create_transient(display, parent->window, tooltip->x,
1247 tooltip->y + offset_y,
1248 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1249 if (!window)
1250 return -1;
1251
1252 tooltip->window = window;
1253 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1254
1255 extents = get_text_extents(tooltip);
1256 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1257 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1258
1259 return 0;
1260}
1261
1262void
1263widget_destroy_tooltip(struct widget *parent)
1264{
1265 struct tooltip *tooltip = parent->tooltip;
1266
1267 parent->tooltip_count = 0;
1268 if (!tooltip)
1269 return;
1270
1271 if (tooltip->widget) {
1272 widget_destroy(tooltip->widget);
1273 window_destroy(tooltip->window);
1274 tooltip->widget = NULL;
1275 tooltip->window = NULL;
1276 }
1277
1278 close(tooltip->tooltip_fd);
1279 free(tooltip->entry);
1280 free(tooltip);
1281 parent->tooltip = NULL;
1282}
1283
1284static void
1285tooltip_func(struct task *task, uint32_t events)
1286{
1287 struct tooltip *tooltip =
1288 container_of(task, struct tooltip, tooltip_task);
1289 uint64_t exp;
1290
Martin Olsson8df662a2012-07-08 03:03:47 +02001291 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1292 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001293 window_create_tooltip(tooltip);
1294}
1295
1296#define TOOLTIP_TIMEOUT 500
1297static int
1298tooltip_timer_reset(struct tooltip *tooltip)
1299{
1300 struct itimerspec its;
1301
1302 its.it_interval.tv_sec = 0;
1303 its.it_interval.tv_nsec = 0;
1304 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1305 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1306 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1307 fprintf(stderr, "could not set timerfd\n: %m");
1308 return -1;
1309 }
1310
1311 return 0;
1312}
1313
1314int
1315widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1316{
1317 struct tooltip *tooltip = parent->tooltip;
1318
1319 parent->tooltip_count++;
1320 if (tooltip) {
1321 tooltip->x = x;
1322 tooltip->y = y;
1323 tooltip_timer_reset(tooltip);
1324 return 0;
1325 }
1326
1327 /* the handler might be triggered too fast via input device motion, so
1328 * we need this check here to make sure tooltip is fully initialized */
1329 if (parent->tooltip_count > 1)
1330 return 0;
1331
1332 tooltip = malloc(sizeof *tooltip);
1333 if (!tooltip)
1334 return -1;
1335
1336 parent->tooltip = tooltip;
1337 tooltip->parent = parent;
1338 tooltip->widget = NULL;
1339 tooltip->window = NULL;
1340 tooltip->x = x;
1341 tooltip->y = y;
1342 tooltip->entry = strdup(entry);
1343 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1344 if (tooltip->tooltip_fd < 0) {
1345 fprintf(stderr, "could not create timerfd\n: %m");
1346 return -1;
1347 }
1348
1349 tooltip->tooltip_task.run = tooltip_func;
1350 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1351 EPOLLIN, &tooltip->tooltip_task);
1352 tooltip_timer_reset(tooltip);
1353
1354 return 0;
1355}
1356
1357static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001358workspace_manager_state(void *data,
1359 struct workspace_manager *workspace_manager,
1360 uint32_t current,
1361 uint32_t count)
1362{
1363 struct display *display = data;
1364
1365 display->workspace = current;
1366 display->workspace_count = count;
1367}
1368
1369static const struct workspace_manager_listener workspace_manager_listener = {
1370 workspace_manager_state
1371};
1372
1373static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001374frame_resize_handler(struct widget *widget,
1375 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001376{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001377 struct frame *frame = data;
1378 struct widget *child = frame->child;
1379 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001380 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001381 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001382 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001383 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001384 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001385 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001387 switch (widget->window->type) {
1388 case TYPE_FULLSCREEN:
1389 decoration_width = 0;
1390 decoration_height = 0;
1391
1392 allocation.x = 0;
1393 allocation.y = 0;
1394 allocation.width = width;
1395 allocation.height = height;
1396 opaque_margin = 0;
1397
1398 wl_list_for_each(button, &frame->buttons_list, link)
1399 button->widget->opaque = 1;
1400 break;
1401 case TYPE_MAXIMIZED:
1402 decoration_width = t->width * 2;
1403 decoration_height = t->width + t->titlebar_height;
1404
1405 allocation.x = t->width;
1406 allocation.y = t->titlebar_height;
1407 allocation.width = width - decoration_width;
1408 allocation.height = height - decoration_height;
1409
1410 opaque_margin = 0;
1411
1412 wl_list_for_each(button, &frame->buttons_list, link)
1413 button->widget->opaque = 0;
1414 break;
1415 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001416 decoration_width = (t->width + t->margin) * 2;
1417 decoration_height = t->width +
1418 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001419
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001420 allocation.x = t->width + t->margin;
1421 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001422 allocation.width = width - decoration_width;
1423 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001424
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001425 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001426
1427 wl_list_for_each(button, &frame->buttons_list, link)
1428 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001429 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001430 }
1431
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001432 widget_set_allocation(child, allocation.x, allocation.y,
1433 allocation.width, allocation.height);
1434
1435 if (child->resize_handler)
1436 child->resize_handler(child,
1437 allocation.width,
1438 allocation.height,
1439 child->user_data);
1440
Scott Moreauf7e498c2012-05-14 11:39:29 -06001441 width = child->allocation.width + decoration_width;
1442 height = child->allocation.height + decoration_height;
1443
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001444 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1445
Kristian Høgsberg023be102012-07-31 11:59:12 -04001446 if (widget->window->type != TYPE_FULLSCREEN) {
1447 widget->window->input_region =
1448 wl_compositor_create_region(display->compositor);
1449 wl_region_add(widget->window->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001450 shadow_margin, shadow_margin,
1451 width - 2 * shadow_margin,
1452 height - 2 * shadow_margin);
Kristian Høgsberg023be102012-07-31 11:59:12 -04001453 }
1454
Scott Moreauf7e498c2012-05-14 11:39:29 -06001455 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001456
1457 if (child->opaque) {
1458 widget->window->opaque_region =
1459 wl_compositor_create_region(display->compositor);
1460 wl_region_add(widget->window->opaque_region,
1461 opaque_margin, opaque_margin,
1462 widget->allocation.width - 2 * opaque_margin,
1463 widget->allocation.height - 2 * opaque_margin);
1464 }
Martin Minarik1998b152012-05-10 02:04:35 +02001465
1466 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001467 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1468 x_l = t->width + shadow_margin;
1469 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001470 wl_list_for_each(button, &frame->buttons_list, link) {
1471 const int button_padding = 4;
1472 w = cairo_image_surface_get_width(button->icon);
1473 h = cairo_image_surface_get_height(button->icon);
1474
1475 if (button->decoration == FRAME_BUTTON_FANCY)
1476 w += 10;
1477
1478 if (button->align == FRAME_BUTTON_LEFT) {
1479 widget_set_allocation(button->widget,
1480 x_l, y , w + 1, h + 1);
1481 x_l += w;
1482 x_l += button_padding;
1483 } else {
1484 x_r -= w;
1485 widget_set_allocation(button->widget,
1486 x_r, y , w + 1, h + 1);
1487 x_r -= button_padding;
1488 }
1489 }
1490}
1491
1492static int
1493frame_button_enter_handler(struct widget *widget,
1494 struct input *input, float x, float y, void *data)
1495{
1496 struct frame_button *frame_button = data;
1497
1498 widget_schedule_redraw(frame_button->widget);
1499 frame_button->state = FRAME_BUTTON_OVER;
1500
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001501 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001502}
1503
1504static void
1505frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1506{
1507 struct frame_button *frame_button = data;
1508
1509 widget_schedule_redraw(frame_button->widget);
1510 frame_button->state = FRAME_BUTTON_DEFAULT;
1511}
1512
1513static void
1514frame_button_button_handler(struct widget *widget,
1515 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001516 uint32_t button,
1517 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001518{
1519 struct frame_button *frame_button = data;
1520 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001521 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001522
1523 if (button != BTN_LEFT)
1524 return;
1525
1526 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001527 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001528 frame_button->state = FRAME_BUTTON_ACTIVE;
1529 widget_schedule_redraw(frame_button->widget);
1530
1531 if (frame_button->type == FRAME_BUTTON_ICON)
1532 window_show_frame_menu(window, input, time);
1533 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001534 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001535 frame_button->state = FRAME_BUTTON_DEFAULT;
1536 widget_schedule_redraw(frame_button->widget);
1537 break;
1538 }
1539
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001540 if (!was_pressed)
1541 return;
1542
Martin Minarik1998b152012-05-10 02:04:35 +02001543 switch (frame_button->type) {
1544 case FRAME_BUTTON_CLOSE:
1545 if (window->close_handler)
1546 window->close_handler(window->parent,
1547 window->user_data);
1548 else
1549 display_exit(window->display);
1550 break;
1551 case FRAME_BUTTON_MINIMIZE:
1552 fprintf(stderr,"Minimize stub\n");
1553 break;
1554 case FRAME_BUTTON_MAXIMIZE:
1555 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1556 break;
1557 default:
1558 /* Unknown operation */
1559 break;
1560 }
1561}
1562
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001563static int
1564frame_button_motion_handler(struct widget *widget,
1565 struct input *input, uint32_t time,
1566 float x, float y, void *data)
1567{
1568 struct frame_button *frame_button = data;
1569 enum frame_button_pointer previous_button_state = frame_button->state;
1570
1571 /* only track state for a pressed button */
1572 if (input->grab != widget)
1573 return CURSOR_LEFT_PTR;
1574
1575 if (x > widget->allocation.x &&
1576 x < (widget->allocation.x + widget->allocation.width) &&
1577 y > widget->allocation.y &&
1578 y < (widget->allocation.y + widget->allocation.height)) {
1579 frame_button->state = FRAME_BUTTON_ACTIVE;
1580 } else {
1581 frame_button->state = FRAME_BUTTON_DEFAULT;
1582 }
1583
1584 if (frame_button->state != previous_button_state)
1585 widget_schedule_redraw(frame_button->widget);
1586
1587 return CURSOR_LEFT_PTR;
1588}
1589
Martin Minarik1998b152012-05-10 02:04:35 +02001590static void
1591frame_button_redraw_handler(struct widget *widget, void *data)
1592{
1593 struct frame_button *frame_button = data;
1594 cairo_t *cr;
1595 int width, height, x, y;
1596 struct window *window = widget->window;
1597
1598 x = widget->allocation.x;
1599 y = widget->allocation.y;
1600 width = widget->allocation.width;
1601 height = widget->allocation.height;
1602
1603 if (!width)
1604 return;
1605 if (!height)
1606 return;
1607 if (widget->opaque)
1608 return;
1609
1610 cr = cairo_create(window->cairo_surface);
1611
1612 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1613 cairo_set_line_width(cr, 1);
1614
1615 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1616 cairo_rectangle (cr, x, y, 25, 16);
1617
1618 cairo_stroke_preserve(cr);
1619
1620 switch (frame_button->state) {
1621 case FRAME_BUTTON_DEFAULT:
1622 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1623 break;
1624 case FRAME_BUTTON_OVER:
1625 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1626 break;
1627 case FRAME_BUTTON_ACTIVE:
1628 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1629 break;
1630 }
1631
1632 cairo_fill (cr);
1633
1634 x += 4;
1635 }
1636
1637 cairo_set_source_surface(cr, frame_button->icon, x, y);
1638 cairo_paint(cr);
1639
1640 cairo_destroy(cr);
1641}
1642
1643static struct widget *
1644frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1645 enum frame_button_align align, enum frame_button_decoration style)
1646{
1647 struct frame_button *frame_button;
1648 const char *icon = data;
1649
1650 frame_button = malloc (sizeof *frame_button);
1651 memset(frame_button, 0, sizeof *frame_button);
1652
1653 frame_button->icon = cairo_image_surface_create_from_png(icon);
1654 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1655 frame_button->frame = frame;
1656 frame_button->type = type;
1657 frame_button->align = align;
1658 frame_button->decoration = style;
1659
1660 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1661
1662 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1663 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1664 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1665 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001666 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001667 return frame_button->widget;
1668}
1669
1670static void
1671frame_button_destroy(struct frame_button *frame_button)
1672{
1673 widget_destroy(frame_button->widget);
1674 wl_list_remove(&frame_button->link);
1675 cairo_surface_destroy(frame_button->icon);
1676 free(frame_button);
1677
1678 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001679}
1680
1681static void
1682frame_redraw_handler(struct widget *widget, void *data)
1683{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001685 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001686 struct theme *t = window->display->theme;
1687 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001688
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001689 if (window->type == TYPE_FULLSCREEN)
1690 return;
1691
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001692 cr = cairo_create(window->cairo_surface);
1693
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001694 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001695 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001696 if (window->type == TYPE_MAXIMIZED)
1697 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001698 theme_render_frame(t, cr, widget->allocation.width,
1699 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001700
1701 cairo_destroy(cr);
1702}
1703
1704static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001706{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001707 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001708 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001709 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001710
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001711 location = theme_get_location(t, input->sx, input->sy,
1712 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001713 frame->widget->allocation.height,
1714 window->type == TYPE_MAXIMIZED ?
1715 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001716
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001717 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001718 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001719 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001720 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001721 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001722 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001723 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001724 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001725 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001726 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001727 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001728 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001729 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001730 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001731 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001732 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001733 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001734 case THEME_LOCATION_EXTERIOR:
1735 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001736 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001738 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001739}
1740
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001741static void
1742frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001743{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001744 struct display *display;
1745
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001746 switch (index) {
1747 case 0: /* close */
1748 if (window->close_handler)
1749 window->close_handler(window->parent,
1750 window->user_data);
1751 else
1752 display_exit(window->display);
1753 break;
1754 case 1: /* fullscreen */
1755 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001756 if (window->fullscreen_handler)
1757 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001758 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001759 case 2: /* move to workspace above */
1760 display = window->display;
1761 if (display->workspace > 0)
1762 workspace_manager_move_surface(display->workspace_manager,
1763 window->surface,
1764 display->workspace - 1);
1765 break;
1766 case 3: /* move to workspace below */
1767 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001768 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001769 workspace_manager_move_surface(display->workspace_manager,
1770 window->surface,
1771 display->workspace + 1);
1772 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001773 }
1774}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001775
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001776void
1777window_show_frame_menu(struct window *window,
1778 struct input *input, uint32_t time)
1779{
1780 int32_t x, y;
1781
1782 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001783 "Close", "Fullscreen",
1784 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001785 };
1786
1787 input_get_position(input, &x, &y);
1788 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001789 x - 10, y - 10, frame_menu_func, entries,
1790 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001791}
1792
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001793static int
1794frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001795 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001796{
1797 return frame_get_pointer_image_for_location(data, input);
1798}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001799
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001800static int
1801frame_motion_handler(struct widget *widget,
1802 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001803 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804{
1805 return frame_get_pointer_image_for_location(data, input);
1806}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001807
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001809frame_button_handler(struct widget *widget,
1810 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001811 uint32_t button, enum wl_pointer_button_state state,
1812 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001813
1814{
1815 struct frame *frame = data;
1816 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001817 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001818 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001819
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001820 location = theme_get_location(display->theme, input->sx, input->sy,
1821 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001822 frame->widget->allocation.height,
1823 window->type == TYPE_MAXIMIZED ?
1824 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001825
Daniel Stone4dbadb12012-05-30 16:31:51 +01001826 if (window->display->shell && button == BTN_LEFT &&
1827 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001828 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001829 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001830 if (!window->shell_surface)
1831 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001832 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001833 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001834 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001835 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001836 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001837 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001838 case THEME_LOCATION_RESIZING_TOP:
1839 case THEME_LOCATION_RESIZING_BOTTOM:
1840 case THEME_LOCATION_RESIZING_LEFT:
1841 case THEME_LOCATION_RESIZING_RIGHT:
1842 case THEME_LOCATION_RESIZING_TOP_LEFT:
1843 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1844 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1845 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001846 if (!window->shell_surface)
1847 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001848 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001849
1850 if (!display->dpy) {
1851 /* If we're using shm, allocate a big
1852 pool to create buffers out of while
1853 we resize. We should probably base
1854 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001855 window->pool =
1856 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001857 }
1858
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001859 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001860 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001861 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001862 break;
1863 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001864 } else if (button == BTN_RIGHT &&
1865 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001866 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001867 }
1868}
1869
1870struct widget *
1871frame_create(struct window *window, void *data)
1872{
1873 struct frame *frame;
1874
1875 frame = malloc(sizeof *frame);
1876 memset(frame, 0, sizeof *frame);
1877
1878 frame->widget = window_add_widget(window, frame);
1879 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001880
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001881 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1882 widget_set_resize_handler(frame->widget, frame_resize_handler);
1883 widget_set_enter_handler(frame->widget, frame_enter_handler);
1884 widget_set_motion_handler(frame->widget, frame_motion_handler);
1885 widget_set_button_handler(frame->widget, frame_button_handler);
1886
Martin Minarik1998b152012-05-10 02:04:35 +02001887 /* Create empty list for frame buttons */
1888 wl_list_init(&frame->buttons_list);
1889
1890 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1891 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1892
1893 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1894 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1895
1896 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1897 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1898
1899 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1900 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1901
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001902 window->frame = frame;
1903
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001904 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001905}
1906
Kristian Høgsberga1627922012-06-20 17:30:03 -04001907void
1908frame_set_child_size(struct widget *widget, int child_width, int child_height)
1909{
1910 struct display *display = widget->window->display;
1911 struct theme *t = display->theme;
1912 int decoration_width, decoration_height;
1913 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001914 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001915
1916 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001917 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001918 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001919 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04001920
1921 width = child_width + decoration_width;
1922 height = child_height + decoration_height;
1923 } else {
1924 width = child_width;
1925 height = child_height;
1926 }
1927
1928 window_schedule_resize(widget->window, width, height);
1929}
1930
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001931static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001932frame_destroy(struct frame *frame)
1933{
Martin Minarik1998b152012-05-10 02:04:35 +02001934 struct frame_button *button, *tmp;
1935
1936 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1937 frame_button_destroy(button);
1938
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001939 /* frame->child must be destroyed by the application */
1940 widget_destroy(frame->widget);
1941 free(frame);
1942}
1943
1944static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001945input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001946 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001947{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001948 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001949 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001950
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001951 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001952 return;
1953
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001954 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001955 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001956 widget = old;
1957 if (input->grab)
1958 widget = input->grab;
1959 if (widget->leave_handler)
1960 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001961 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001962 }
1963
1964 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001965 widget = focus;
1966 if (input->grab)
1967 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001968 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001969 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001970 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001971 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001972
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001973 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001974 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001975}
1976
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001977void
1978input_grab(struct input *input, struct widget *widget, uint32_t button)
1979{
1980 input->grab = widget;
1981 input->grab_button = button;
1982}
1983
1984void
1985input_ungrab(struct input *input)
1986{
1987 struct widget *widget;
1988
1989 input->grab = NULL;
1990 if (input->pointer_focus) {
1991 widget = widget_find_widget(input->pointer_focus->widget,
1992 input->sx, input->sy);
1993 input_set_focus_widget(input, widget, input->sx, input->sy);
1994 }
1995}
1996
1997static void
1998input_remove_pointer_focus(struct input *input)
1999{
2000 struct window *window = input->pointer_focus;
2001
2002 if (!window)
2003 return;
2004
2005 input_set_focus_widget(input, NULL, 0, 0);
2006
2007 input->pointer_focus = NULL;
2008 input->current_cursor = CURSOR_UNSET;
2009}
2010
2011static void
2012pointer_handle_enter(void *data, struct wl_pointer *pointer,
2013 uint32_t serial, struct wl_surface *surface,
2014 wl_fixed_t sx_w, wl_fixed_t sy_w)
2015{
2016 struct input *input = data;
2017 struct window *window;
2018 struct widget *widget;
2019 float sx = wl_fixed_to_double(sx_w);
2020 float sy = wl_fixed_to_double(sy_w);
2021
2022 if (!surface) {
2023 /* enter event for a window we've just destroyed */
2024 return;
2025 }
2026
2027 input->display->serial = serial;
2028 input->pointer_enter_serial = serial;
2029 input->pointer_focus = wl_surface_get_user_data(surface);
2030 window = input->pointer_focus;
2031
2032 if (window->pool) {
2033 shm_pool_destroy(window->pool);
2034 window->pool = NULL;
2035 /* Schedule a redraw to free the pool */
2036 window_schedule_redraw(window);
2037 }
2038
2039 input->sx = sx;
2040 input->sy = sy;
2041
2042 widget = widget_find_widget(window->widget, sx, sy);
2043 input_set_focus_widget(input, widget, sx, sy);
2044}
2045
2046static void
2047pointer_handle_leave(void *data, struct wl_pointer *pointer,
2048 uint32_t serial, struct wl_surface *surface)
2049{
2050 struct input *input = data;
2051
2052 input->display->serial = serial;
2053 input_remove_pointer_focus(input);
2054}
2055
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002056static void
Daniel Stone37816df2012-05-16 18:45:18 +01002057pointer_handle_motion(void *data, struct wl_pointer *pointer,
2058 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002059{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002060 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002061 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002062 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002063 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002064 float sx = wl_fixed_to_double(sx_w);
2065 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002066
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002067 input->sx = sx;
2068 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002069
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002070 if (!window)
2071 return;
2072
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002073 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002074 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002075 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002076 }
2077
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002078 if (input->grab)
2079 widget = input->grab;
2080 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002081 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002082 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002083 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002084 input, time, sx, sy,
2085 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002086
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002087 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002088}
2089
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002090static void
Daniel Stone37816df2012-05-16 18:45:18 +01002091pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002092 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002093{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002094 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002095 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002096 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002097
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002098 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002099 if (input->focus_widget && input->grab == NULL &&
2100 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002101 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002102
Neil Roberts6b28aad2012-01-23 19:11:18 +00002103 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002104 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002105 (*widget->button_handler)(widget,
2106 input, time,
2107 button, state,
2108 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002109
Daniel Stone4dbadb12012-05-30 16:31:51 +01002110 if (input->grab && input->grab_button == button &&
2111 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002112 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002113}
2114
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002115static void
Daniel Stone37816df2012-05-16 18:45:18 +01002116pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002117 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002118{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002119 struct input *input = data;
2120 struct widget *widget;
2121
2122 widget = input->focus_widget;
2123 if (input->grab)
2124 widget = input->grab;
2125 if (widget && widget->axis_handler)
2126 (*widget->axis_handler)(widget,
2127 input, time,
2128 axis, value,
2129 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002130}
2131
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002132static const struct wl_pointer_listener pointer_listener = {
2133 pointer_handle_enter,
2134 pointer_handle_leave,
2135 pointer_handle_motion,
2136 pointer_handle_button,
2137 pointer_handle_axis,
2138};
2139
2140static void
2141input_remove_keyboard_focus(struct input *input)
2142{
2143 struct window *window = input->keyboard_focus;
2144 struct itimerspec its;
2145
2146 its.it_interval.tv_sec = 0;
2147 its.it_interval.tv_nsec = 0;
2148 its.it_value.tv_sec = 0;
2149 its.it_value.tv_nsec = 0;
2150 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2151
2152 if (!window)
2153 return;
2154
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002155 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002156 if (window->keyboard_focus_handler)
2157 (*window->keyboard_focus_handler)(window, NULL,
2158 window->user_data);
2159
2160 input->keyboard_focus = NULL;
2161}
2162
2163static void
2164keyboard_repeat_func(struct task *task, uint32_t events)
2165{
2166 struct input *input =
2167 container_of(task, struct input, repeat_task);
2168 struct window *window = input->keyboard_focus;
2169 uint64_t exp;
2170
2171 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2172 /* If we change the timer between the fd becoming
2173 * readable and getting here, there'll be nothing to
2174 * read and we get EAGAIN. */
2175 return;
2176
2177 if (window && window->key_handler) {
2178 (*window->key_handler)(window, input, input->repeat_time,
2179 input->repeat_key, input->repeat_sym,
2180 WL_KEYBOARD_KEY_STATE_PRESSED,
2181 window->user_data);
2182 }
2183}
2184
2185static void
2186keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2187 uint32_t format, int fd, uint32_t size)
2188{
2189 struct input *input = data;
2190 char *map_str;
2191
2192 if (!data) {
2193 close(fd);
2194 return;
2195 }
2196
2197 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2198 close(fd);
2199 return;
2200 }
2201
2202 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2203 if (map_str == MAP_FAILED) {
2204 close(fd);
2205 return;
2206 }
2207
2208 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2209 map_str,
2210 XKB_KEYMAP_FORMAT_TEXT_V1,
2211 0);
2212 munmap(map_str, size);
2213 close(fd);
2214
2215 if (!input->xkb.keymap) {
2216 fprintf(stderr, "failed to compile keymap\n");
2217 return;
2218 }
2219
2220 input->xkb.state = xkb_state_new(input->xkb.keymap);
2221 if (!input->xkb.state) {
2222 fprintf(stderr, "failed to create XKB state\n");
2223 xkb_map_unref(input->xkb.keymap);
2224 input->xkb.keymap = NULL;
2225 return;
2226 }
2227
2228 input->xkb.control_mask =
2229 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2230 input->xkb.alt_mask =
2231 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2232 input->xkb.shift_mask =
2233 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2234}
2235
2236static void
2237keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2238 uint32_t serial, struct wl_surface *surface,
2239 struct wl_array *keys)
2240{
2241 struct input *input = data;
2242 struct window *window;
2243
2244 input->display->serial = serial;
2245 input->keyboard_focus = wl_surface_get_user_data(surface);
2246
2247 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002248 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002249 if (window->keyboard_focus_handler)
2250 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002251 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002252}
2253
2254static void
2255keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2256 uint32_t serial, struct wl_surface *surface)
2257{
2258 struct input *input = data;
2259
2260 input->display->serial = serial;
2261 input_remove_keyboard_focus(input);
2262}
2263
Scott Moreau210d0792012-03-22 10:47:01 -06002264static void
Daniel Stone37816df2012-05-16 18:45:18 +01002265keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002266 uint32_t serial, uint32_t time, uint32_t key,
2267 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002268{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002269 struct input *input = data;
2270 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002271 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002272 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002273 const xkb_keysym_t *syms;
2274 xkb_keysym_t sym;
2275 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002276 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002277
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002278 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002279 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002280 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002281 return;
2282
Daniel Stone97f68542012-05-30 16:32:01 +01002283 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002284
Daniel Stone97f68542012-05-30 16:32:01 +01002285 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002286 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002287 XKB_STATE_LATCHED);
2288 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002289 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002290 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002291 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002292 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002293 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002294 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002295
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002296 sym = XKB_KEY_NoSymbol;
2297 if (num_syms == 1)
2298 sym = syms[0];
2299
2300 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002301 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002302 window_set_maximized(window,
2303 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002304 } else if (sym == XKB_KEY_F11 &&
2305 window->fullscreen_handler &&
2306 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2307 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002308 } else if (sym == XKB_KEY_F4 &&
2309 input->modifiers == MOD_ALT_MASK &&
2310 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2311 if (window->close_handler)
2312 window->close_handler(window->parent,
2313 window->user_data);
2314 else
2315 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002316 } else if (window->key_handler) {
2317 (*window->key_handler)(window, input, time, key,
2318 sym, state, window->user_data);
2319 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002320
2321 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2322 key == input->repeat_key) {
2323 its.it_interval.tv_sec = 0;
2324 its.it_interval.tv_nsec = 0;
2325 its.it_value.tv_sec = 0;
2326 its.it_value.tv_nsec = 0;
2327 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2328 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2329 input->repeat_sym = sym;
2330 input->repeat_key = key;
2331 input->repeat_time = time;
2332 its.it_interval.tv_sec = 0;
2333 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2334 its.it_value.tv_sec = 0;
2335 its.it_value.tv_nsec = 400 * 1000 * 1000;
2336 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2337 }
2338}
2339
2340static void
Daniel Stone351eb612012-05-31 15:27:47 -04002341keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2342 uint32_t serial, uint32_t mods_depressed,
2343 uint32_t mods_latched, uint32_t mods_locked,
2344 uint32_t group)
2345{
2346 struct input *input = data;
2347
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002348 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2349 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002350}
2351
Daniel Stone37816df2012-05-16 18:45:18 +01002352static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002353 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002354 keyboard_handle_enter,
2355 keyboard_handle_leave,
2356 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002357 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002358};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002359
2360static void
Daniel Stone37816df2012-05-16 18:45:18 +01002361seat_handle_capabilities(void *data, struct wl_seat *seat,
2362 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002363{
Daniel Stone37816df2012-05-16 18:45:18 +01002364 struct input *input = data;
2365
2366 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2367 input->pointer = wl_seat_get_pointer(seat);
2368 wl_pointer_set_user_data(input->pointer, input);
2369 wl_pointer_add_listener(input->pointer, &pointer_listener,
2370 input);
2371 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2372 wl_pointer_destroy(input->pointer);
2373 input->pointer = NULL;
2374 }
2375
2376 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2377 input->keyboard = wl_seat_get_keyboard(seat);
2378 wl_keyboard_set_user_data(input->keyboard, input);
2379 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2380 input);
2381 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2382 wl_keyboard_destroy(input->keyboard);
2383 input->keyboard = NULL;
2384 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002385}
2386
Daniel Stone37816df2012-05-16 18:45:18 +01002387static const struct wl_seat_listener seat_listener = {
2388 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002389};
2390
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002391void
2392input_get_position(struct input *input, int32_t *x, int32_t *y)
2393{
2394 *x = input->sx;
2395 *y = input->sy;
2396}
2397
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002398struct display *
2399input_get_display(struct input *input)
2400{
2401 return input->display;
2402}
2403
Daniel Stone37816df2012-05-16 18:45:18 +01002404struct wl_seat *
2405input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002406{
Daniel Stone37816df2012-05-16 18:45:18 +01002407 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002408}
2409
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002410uint32_t
2411input_get_modifiers(struct input *input)
2412{
2413 return input->modifiers;
2414}
2415
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002416struct widget *
2417input_get_focus_widget(struct input *input)
2418{
2419 return input->focus_widget;
2420}
2421
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002422struct data_offer {
2423 struct wl_data_offer *offer;
2424 struct input *input;
2425 struct wl_array types;
2426 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002427
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002428 struct task io_task;
2429 int fd;
2430 data_func_t func;
2431 int32_t x, y;
2432 void *user_data;
2433};
2434
2435static void
2436data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2437{
2438 struct data_offer *offer = data;
2439 char **p;
2440
2441 p = wl_array_add(&offer->types, sizeof *p);
2442 *p = strdup(type);
2443}
2444
2445static const struct wl_data_offer_listener data_offer_listener = {
2446 data_offer_offer,
2447};
2448
2449static void
2450data_offer_destroy(struct data_offer *offer)
2451{
2452 char **p;
2453
2454 offer->refcount--;
2455 if (offer->refcount == 0) {
2456 wl_data_offer_destroy(offer->offer);
2457 for (p = offer->types.data; *p; p++)
2458 free(*p);
2459 wl_array_release(&offer->types);
2460 free(offer);
2461 }
2462}
2463
2464static void
2465data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002466 struct wl_data_device *data_device,
2467 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002468{
2469 struct data_offer *offer;
2470
2471 offer = malloc(sizeof *offer);
2472
2473 wl_array_init(&offer->types);
2474 offer->refcount = 1;
2475 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002476 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002477 wl_data_offer_add_listener(offer->offer,
2478 &data_offer_listener, offer);
2479}
2480
2481static void
2482data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002483 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002484 wl_fixed_t x_w, wl_fixed_t y_w,
2485 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002486{
2487 struct input *input = data;
2488 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002489 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002490 float x = wl_fixed_to_double(x_w);
2491 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002492 char **p;
2493
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002494 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002495 window = wl_surface_get_user_data(surface);
2496 input->pointer_focus = window;
2497
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002498 if (offer) {
2499 input->drag_offer = wl_data_offer_get_user_data(offer);
2500
2501 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2502 *p = NULL;
2503
2504 types_data = input->drag_offer->types.data;
2505 } else {
2506 input->drag_offer = NULL;
2507 types_data = NULL;
2508 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002509
2510 window = input->pointer_focus;
2511 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002512 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002513 window->user_data);
2514}
2515
2516static void
2517data_device_leave(void *data, struct wl_data_device *data_device)
2518{
2519 struct input *input = data;
2520
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002521 if (input->drag_offer) {
2522 data_offer_destroy(input->drag_offer);
2523 input->drag_offer = NULL;
2524 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002525}
2526
2527static void
2528data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002529 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002530{
2531 struct input *input = data;
2532 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002533 float x = wl_fixed_to_double(x_w);
2534 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002535 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002536
2537 input->sx = x;
2538 input->sy = y;
2539
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002540 if (input->drag_offer)
2541 types_data = input->drag_offer->types.data;
2542 else
2543 types_data = NULL;
2544
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002545 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002546 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002547 window->user_data);
2548}
2549
2550static void
2551data_device_drop(void *data, struct wl_data_device *data_device)
2552{
2553 struct input *input = data;
2554 struct window *window = input->pointer_focus;
2555
2556 if (window->drop_handler)
2557 window->drop_handler(window, input,
2558 input->sx, input->sy, window->user_data);
2559}
2560
2561static void
2562data_device_selection(void *data,
2563 struct wl_data_device *wl_data_device,
2564 struct wl_data_offer *offer)
2565{
2566 struct input *input = data;
2567 char **p;
2568
2569 if (input->selection_offer)
2570 data_offer_destroy(input->selection_offer);
2571
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002572 if (offer) {
2573 input->selection_offer = wl_data_offer_get_user_data(offer);
2574 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2575 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002576 } else {
2577 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002578 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002579}
2580
2581static const struct wl_data_device_listener data_device_listener = {
2582 data_device_data_offer,
2583 data_device_enter,
2584 data_device_leave,
2585 data_device_motion,
2586 data_device_drop,
2587 data_device_selection
2588};
2589
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002590static void
2591input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002592{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002593 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002594 struct wl_cursor *cursor;
2595 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002596
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002597 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002598 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002599 return;
2600
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002601 if (index >= (int) cursor->image_count) {
2602 fprintf(stderr, "cursor index out of range\n");
2603 return;
2604 }
2605
2606 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002607 buffer = wl_cursor_image_get_buffer(image);
2608 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002609 return;
2610
Kristian Høgsbergae277372012-08-01 09:41:08 -04002611 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002612 input->pointer_surface,
2613 image->hotspot_x, image->hotspot_y);
2614 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2615 wl_surface_damage(input->pointer_surface, 0, 0,
2616 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002617}
2618
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002619static const struct wl_callback_listener pointer_surface_listener;
2620
2621static void
2622pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2623 uint32_t time)
2624{
2625 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002626 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002627 int i;
2628
2629 if (callback) {
2630 assert(callback == input->cursor_frame_cb);
2631 wl_callback_destroy(callback);
2632 input->cursor_frame_cb = NULL;
2633 }
2634
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002635 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002636 wl_pointer_set_cursor(input->pointer,
2637 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002638 NULL, 0, 0);
2639 return;
2640 }
2641
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002642 if (input->current_cursor == CURSOR_UNSET)
2643 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002644 cursor = input->display->cursors[input->current_cursor];
2645 if (!cursor)
2646 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002647
2648 /* FIXME We don't have the current time on the first call so we set
2649 * the animation start to the time of the first frame callback. */
2650 if (time == 0)
2651 input->cursor_anim_start = 0;
2652 else if (input->cursor_anim_start == 0)
2653 input->cursor_anim_start = time;
2654
2655 if (time == 0 || input->cursor_anim_start == 0)
2656 i = 0;
2657 else
2658 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2659
2660 input_set_pointer_image_index(input, i);
2661
2662 if (cursor->image_count == 1)
2663 return;
2664
2665 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2666 wl_callback_add_listener(input->cursor_frame_cb,
2667 &pointer_surface_listener, input);
2668}
2669
2670static const struct wl_callback_listener pointer_surface_listener = {
2671 pointer_surface_frame_callback
2672};
2673
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002674void
2675input_set_pointer_image(struct input *input, int pointer)
2676{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002677 int force = 0;
2678
2679 if (input->pointer_enter_serial > input->cursor_serial)
2680 force = 1;
2681
2682 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002683 return;
2684
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002685 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002686 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002687 if (!input->cursor_frame_cb)
2688 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002689 else if (force) {
2690 /* The current frame callback may be stuck if, for instance,
2691 * the set cursor request was processed by the server after
2692 * this client lost the focus. In this case the cursor surface
2693 * might not be mapped and the frame callback wouldn't ever
2694 * complete. Send a set_cursor and attach to try to map the
2695 * cursor surface again so that the callback will finish */
2696 input_set_pointer_image_index(input, 0);
2697 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002698}
2699
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002700struct wl_data_device *
2701input_get_data_device(struct input *input)
2702{
2703 return input->data_device;
2704}
2705
2706void
2707input_set_selection(struct input *input,
2708 struct wl_data_source *source, uint32_t time)
2709{
2710 wl_data_device_set_selection(input->data_device, source, time);
2711}
2712
2713void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002715{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002716 wl_data_offer_accept(input->drag_offer->offer,
2717 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002718}
2719
2720static void
2721offer_io_func(struct task *task, uint32_t events)
2722{
2723 struct data_offer *offer =
2724 container_of(task, struct data_offer, io_task);
2725 unsigned int len;
2726 char buffer[4096];
2727
2728 len = read(offer->fd, buffer, sizeof buffer);
2729 offer->func(buffer, len,
2730 offer->x, offer->y, offer->user_data);
2731
2732 if (len == 0) {
2733 close(offer->fd);
2734 data_offer_destroy(offer);
2735 }
2736}
2737
2738static void
2739data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2740 data_func_t func, void *user_data)
2741{
2742 int p[2];
2743
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002744 if (pipe2(p, O_CLOEXEC) == -1)
2745 return;
2746
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002747 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2748 close(p[1]);
2749
2750 offer->io_task.run = offer_io_func;
2751 offer->fd = p[0];
2752 offer->func = func;
2753 offer->refcount++;
2754 offer->user_data = user_data;
2755
2756 display_watch_fd(offer->input->display,
2757 offer->fd, EPOLLIN, &offer->io_task);
2758}
2759
2760void
2761input_receive_drag_data(struct input *input, const char *mime_type,
2762 data_func_t func, void *data)
2763{
2764 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2765 input->drag_offer->x = input->sx;
2766 input->drag_offer->y = input->sy;
2767}
2768
2769int
2770input_receive_selection_data(struct input *input, const char *mime_type,
2771 data_func_t func, void *data)
2772{
2773 char **p;
2774
2775 if (input->selection_offer == NULL)
2776 return -1;
2777
2778 for (p = input->selection_offer->types.data; *p; p++)
2779 if (strcmp(mime_type, *p) == 0)
2780 break;
2781
2782 if (*p == NULL)
2783 return -1;
2784
2785 data_offer_receive_data(input->selection_offer,
2786 mime_type, func, data);
2787 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002788}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002789
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002790int
2791input_receive_selection_data_to_fd(struct input *input,
2792 const char *mime_type, int fd)
2793{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002794 if (input->selection_offer)
2795 wl_data_offer_receive(input->selection_offer->offer,
2796 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002797
2798 return 0;
2799}
2800
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002801void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002802window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002803{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002804 if (!window->shell_surface)
2805 return;
2806
Daniel Stone37816df2012-05-16 18:45:18 +01002807 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002808}
2809
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002810static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002811idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002812{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002813 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002814
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002815 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002816 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002817 widget_set_allocation(widget,
2818 window->pending_allocation.x,
2819 window->pending_allocation.y,
2820 window->pending_allocation.width,
2821 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002822
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002823 if (window->input_region) {
2824 wl_region_destroy(window->input_region);
2825 window->input_region = NULL;
2826 }
2827
2828 if (window->opaque_region) {
2829 wl_region_destroy(window->opaque_region);
2830 window->opaque_region = NULL;
2831 }
2832
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002833 if (widget->resize_handler)
2834 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002835 widget->allocation.width,
2836 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002837 widget->user_data);
2838
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002839 if (window->allocation.width != widget->allocation.width ||
2840 window->allocation.height != widget->allocation.height) {
2841 window->allocation = widget->allocation;
2842 window_schedule_redraw(window);
2843 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002844}
2845
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002846void
2847window_schedule_resize(struct window *window, int width, int height)
2848{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002849 window->pending_allocation.x = 0;
2850 window->pending_allocation.y = 0;
2851 window->pending_allocation.width = width;
2852 window->pending_allocation.height = height;
2853
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002854 if (window->min_allocation.width == 0)
2855 window->min_allocation = window->pending_allocation;
2856 if (window->pending_allocation.width < window->min_allocation.width)
2857 window->pending_allocation.width = window->min_allocation.width;
2858 if (window->pending_allocation.height < window->min_allocation.height)
2859 window->pending_allocation.height = window->min_allocation.height;
2860
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002861 window->resize_needed = 1;
2862 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002863}
2864
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002865void
2866widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2867{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002868 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002869}
2870
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002871static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002872handle_ping(void *data, struct wl_shell_surface *shell_surface,
2873 uint32_t serial)
2874{
2875 wl_shell_surface_pong(shell_surface, serial);
2876}
2877
2878static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002879handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002880 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002881{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002882 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002883
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002884 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002885 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002886}
2887
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002888static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002889menu_destroy(struct menu *menu)
2890{
2891 widget_destroy(menu->widget);
2892 window_destroy(menu->window);
2893 free(menu);
2894}
2895
2896static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002897handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2898{
2899 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002900 struct menu *menu = window->widget->user_data;
2901
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002902 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002903 * device. Or just use wl_callback. And this really needs to
2904 * be a window vfunc that the menu can set. And we need the
2905 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002906
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002907 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002908 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002909 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002910}
2911
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002912static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002913 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002914 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002915 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002916};
2917
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002918void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002919window_get_allocation(struct window *window,
2920 struct rectangle *allocation)
2921{
2922 *allocation = window->allocation;
2923}
2924
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002925static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002926widget_redraw(struct widget *widget)
2927{
2928 struct widget *child;
2929
2930 if (widget->redraw_handler)
2931 widget->redraw_handler(widget, widget->user_data);
2932 wl_list_for_each(child, &widget->child_list, link)
2933 widget_redraw(child);
2934}
2935
2936static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002937frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2938{
2939 struct window *window = data;
2940
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002941 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002942 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002943 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002944 window->redraw_scheduled = 0;
2945 if (window->redraw_needed)
2946 window_schedule_redraw(window);
2947}
2948
2949static const struct wl_callback_listener listener = {
2950 frame_callback
2951};
2952
2953static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002954idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002955{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002956 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002957
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002958 if (window->resize_needed)
2959 idle_resize(window);
2960
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002961 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002962 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002963 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002964 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002965 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002966
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002967 window->frame_cb = wl_surface_frame(window->surface);
2968 wl_callback_add_listener(window->frame_cb, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002969}
2970
2971void
2972window_schedule_redraw(struct window *window)
2973{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002974 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002975 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002976 window->redraw_task.run = idle_redraw;
2977 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002978 window->redraw_scheduled = 1;
2979 }
2980}
2981
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002982void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002983window_set_fullscreen(struct window *window, int fullscreen)
2984{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002985 if (!window->display->shell)
2986 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002987
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002988 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002989 return;
2990
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002991 if (fullscreen) {
2992 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002993 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002994 wl_shell_surface_set_fullscreen(window->shell_surface,
2995 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2996 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002997 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002998 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002999 wl_shell_surface_set_toplevel(window->shell_surface);
3000 window_schedule_resize(window,
3001 window->saved_allocation.width,
3002 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003003 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003004}
3005
3006void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003007window_set_maximized(struct window *window, int maximized)
3008{
3009 if (!window->display->shell)
3010 return;
3011
3012 if ((window->type == TYPE_MAXIMIZED) == maximized)
3013 return;
3014
3015 if (window->type == TYPE_TOPLEVEL) {
3016 window->saved_allocation = window->allocation;
3017 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3018 window->type = TYPE_MAXIMIZED;
3019 } else {
3020 wl_shell_surface_set_toplevel(window->shell_surface);
3021 window->type = TYPE_TOPLEVEL;
3022 window_schedule_resize(window,
3023 window->saved_allocation.width,
3024 window->saved_allocation.height);
3025 }
3026}
3027
3028void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003029window_set_user_data(struct window *window, void *data)
3030{
3031 window->user_data = data;
3032}
3033
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003034void *
3035window_get_user_data(struct window *window)
3036{
3037 return window->user_data;
3038}
3039
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003040void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003041window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003042 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003043{
3044 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003045}
3046
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003047void
3048window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003049 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003050{
3051 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003052}
3053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003054void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003055window_set_data_handler(struct window *window, window_data_handler_t handler)
3056{
3057 window->data_handler = handler;
3058}
3059
3060void
3061window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3062{
3063 window->drop_handler = handler;
3064}
3065
3066void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003067window_set_close_handler(struct window *window,
3068 window_close_handler_t handler)
3069{
3070 window->close_handler = handler;
3071}
3072
3073void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003074window_set_fullscreen_handler(struct window *window,
3075 window_fullscreen_handler_t handler)
3076{
3077 window->fullscreen_handler = handler;
3078}
3079
3080void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003081window_set_title(struct window *window, const char *title)
3082{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003083 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003084 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003085 if (window->shell_surface)
3086 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003087}
3088
3089const char *
3090window_get_title(struct window *window)
3091{
3092 return window->title;
3093}
3094
3095void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003096window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3097{
3098 struct text_cursor_position *text_cursor_position =
3099 window->display->text_cursor_position;
3100
Scott Moreau9295ce02012-06-01 12:46:10 -06003101 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003102 return;
3103
3104 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003105 window->surface,
3106 wl_fixed_from_int(x),
3107 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003108}
3109
3110void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003111window_damage(struct window *window, int32_t x, int32_t y,
3112 int32_t width, int32_t height)
3113{
3114 wl_surface_damage(window->surface, x, y, width, height);
3115}
3116
Casey Dahlin9074db52012-04-19 22:50:09 -04003117static void
3118surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003119 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003120{
Rob Bradford7507b572012-05-15 17:55:34 +01003121 struct window *window = data;
3122 struct output *output;
3123 struct output *output_found = NULL;
3124 struct window_output *window_output;
3125
3126 wl_list_for_each(output, &window->display->output_list, link) {
3127 if (output->output == wl_output) {
3128 output_found = output;
3129 break;
3130 }
3131 }
3132
3133 if (!output_found)
3134 return;
3135
3136 window_output = malloc (sizeof *window_output);
3137 window_output->output = output_found;
3138
3139 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003140}
3141
3142static void
3143surface_leave(void *data,
3144 struct wl_surface *wl_surface, struct wl_output *output)
3145{
Rob Bradford7507b572012-05-15 17:55:34 +01003146 struct window *window = data;
3147 struct window_output *window_output;
3148 struct window_output *window_output_found = NULL;
3149
3150 wl_list_for_each(window_output, &window->window_output_list, link) {
3151 if (window_output->output->output == output) {
3152 window_output_found = window_output;
3153 break;
3154 }
3155 }
3156
3157 if (window_output_found) {
3158 wl_list_remove(&window_output_found->link);
3159 free(window_output_found);
3160 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003161}
3162
3163static const struct wl_surface_listener surface_listener = {
3164 surface_enter,
3165 surface_leave
3166};
3167
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003168static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003169window_create_internal(struct display *display,
3170 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003171{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003172 struct window *window;
3173
3174 window = malloc(sizeof *window);
3175 if (window == NULL)
3176 return NULL;
3177
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003178 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003179 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003180 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003181 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003182 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003183 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003184 window->shell_surface =
3185 wl_shell_get_shell_surface(display->shell,
3186 window->surface);
3187 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003188 window->allocation.x = 0;
3189 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003190 window->allocation.width = 0;
3191 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003192 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003193 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003194 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003195 window->input_region = NULL;
3196 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003197
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003198 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003199#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003200 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003201#else
3202 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3203#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003204 else
3205 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003206
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003207 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003208 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003209 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003210
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003211 if (window->shell_surface) {
3212 wl_shell_surface_set_user_data(window->shell_surface, window);
3213 wl_shell_surface_add_listener(window->shell_surface,
3214 &shell_surface_listener, window);
3215 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003216
Rob Bradford7507b572012-05-15 17:55:34 +01003217 wl_list_init (&window->window_output_list);
3218
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003219 return window;
3220}
3221
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003222struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003223window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003224{
3225 struct window *window;
3226
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003227 window = window_create_internal(display, NULL, TYPE_NONE);
3228 if (!window)
3229 return NULL;
3230
3231 return window;
3232}
3233
3234struct window *
3235window_create_custom(struct display *display)
3236{
3237 struct window *window;
3238
3239 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003240 if (!window)
3241 return NULL;
3242
3243 return window;
3244}
3245
3246struct window *
3247window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003248 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003249{
3250 struct window *window;
3251
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003252 window = window_create_internal(parent->display,
3253 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003254 if (!window)
3255 return NULL;
3256
3257 window->x = x;
3258 window->y = y;
3259
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003260 if (display->shell)
3261 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003262 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003263 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003264
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003265 return window;
3266}
3267
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003268static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003269menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003270{
3271 int next;
3272
3273 next = (sy - 8) / 20;
3274 if (menu->current != next) {
3275 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003276 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003277 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003278}
3279
3280static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003281menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003282 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003283 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003284{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003285 struct menu *menu = data;
3286
3287 if (widget == menu->widget)
3288 menu_set_item(data, y);
3289
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003290 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003291}
3292
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003293static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003294menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003295 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003296{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003297 struct menu *menu = data;
3298
3299 if (widget == menu->widget)
3300 menu_set_item(data, y);
3301
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003302 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003303}
3304
3305static void
3306menu_leave_handler(struct widget *widget, struct input *input, void *data)
3307{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003308 struct menu *menu = data;
3309
3310 if (widget == menu->widget)
3311 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003312}
3313
3314static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003315menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003316 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003317 uint32_t button, enum wl_pointer_button_state state,
3318 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003319
3320{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003321 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003322
Daniel Stone4dbadb12012-05-30 16:31:51 +01003323 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3324 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003325 /* Either relase after press-drag-release or
3326 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003327 menu->func(menu->window->parent,
3328 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003329 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003330 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003331 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003332}
3333
3334static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003335menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003336{
3337 cairo_t *cr;
3338 const int32_t r = 3, margin = 3;
3339 struct menu *menu = data;
3340 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003341 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003342
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003343 cr = cairo_create(window->cairo_surface);
3344 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3345 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3346 cairo_paint(cr);
3347
3348 width = window->allocation.width;
3349 height = window->allocation.height;
3350 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003351
3352 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3354 cairo_fill(cr);
3355
3356 for (i = 0; i < menu->count; i++) {
3357 if (i == menu->current) {
3358 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3359 cairo_rectangle(cr, margin, i * 20 + margin,
3360 width - 2 * margin, 20);
3361 cairo_fill(cr);
3362 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3363 cairo_move_to(cr, 10, i * 20 + 16);
3364 cairo_show_text(cr, menu->entries[i]);
3365 } else {
3366 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3367 cairo_move_to(cr, 10, i * 20 + 16);
3368 cairo_show_text(cr, menu->entries[i]);
3369 }
3370 }
3371
3372 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003373}
3374
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003375void
3376window_show_menu(struct display *display,
3377 struct input *input, uint32_t time, struct window *parent,
3378 int32_t x, int32_t y,
3379 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003380{
3381 struct window *window;
3382 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003383 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003384
3385 menu = malloc(sizeof *menu);
3386 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003387 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003388
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003389 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003390 if (!window) {
3391 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003392 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003393 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003394
3395 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003396 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003397 menu->entries = entries;
3398 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003399 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003400 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003401 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003402 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003403 window->type = TYPE_MENU;
3404 window->x = x;
3405 window->y = y;
3406
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003407 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003408 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003409 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003410 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003411 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003412
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003413 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003414 widget_set_enter_handler(menu->widget, menu_enter_handler);
3415 widget_set_leave_handler(menu->widget, menu_leave_handler);
3416 widget_set_motion_handler(menu->widget, menu_motion_handler);
3417 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003418
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003419 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003420 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003421}
3422
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003423void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003424window_set_buffer_type(struct window *window, enum window_buffer_type type)
3425{
3426 window->buffer_type = type;
3427}
3428
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003429
3430static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003431display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003432 struct wl_output *wl_output,
3433 int x, int y,
3434 int physical_width,
3435 int physical_height,
3436 int subpixel,
3437 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003438 const char *model,
3439 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003440{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003441 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003442
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003443 output->allocation.x = x;
3444 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003445}
3446
3447static void
3448display_handle_mode(void *data,
3449 struct wl_output *wl_output,
3450 uint32_t flags,
3451 int width,
3452 int height,
3453 int refresh)
3454{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003455 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003456 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003457
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003458 if (flags & WL_OUTPUT_MODE_CURRENT) {
3459 output->allocation.width = width;
3460 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003461 if (display->output_configure_handler)
3462 (*display->output_configure_handler)(
3463 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003464 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003465}
3466
3467static const struct wl_output_listener output_listener = {
3468 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003469 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003470};
3471
3472static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003473display_add_output(struct display *d, uint32_t id)
3474{
3475 struct output *output;
3476
3477 output = malloc(sizeof *output);
3478 if (output == NULL)
3479 return;
3480
3481 memset(output, 0, sizeof *output);
3482 output->display = d;
3483 output->output =
3484 wl_display_bind(d->display, id, &wl_output_interface);
3485 wl_list_insert(d->output_list.prev, &output->link);
3486
3487 wl_output_add_listener(output->output, &output_listener, output);
3488}
3489
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003490static void
3491output_destroy(struct output *output)
3492{
3493 if (output->destroy_handler)
3494 (*output->destroy_handler)(output, output->user_data);
3495
3496 wl_output_destroy(output->output);
3497 wl_list_remove(&output->link);
3498 free(output);
3499}
3500
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003501void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003502display_set_output_configure_handler(struct display *display,
3503 display_output_handler_t handler)
3504{
3505 struct output *output;
3506
3507 display->output_configure_handler = handler;
3508 if (!handler)
3509 return;
3510
3511 wl_list_for_each(output, &display->output_list, link)
3512 (*display->output_configure_handler)(output,
3513 display->user_data);
3514}
3515
3516void
3517output_set_user_data(struct output *output, void *data)
3518{
3519 output->user_data = data;
3520}
3521
3522void *
3523output_get_user_data(struct output *output)
3524{
3525 return output->user_data;
3526}
3527
3528void
3529output_set_destroy_handler(struct output *output,
3530 display_output_handler_t handler)
3531{
3532 output->destroy_handler = handler;
3533 /* FIXME: implement this, once we have way to remove outputs */
3534}
3535
3536void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003537output_get_allocation(struct output *output, struct rectangle *allocation)
3538{
3539 *allocation = output->allocation;
3540}
3541
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003542struct wl_output *
3543output_get_wl_output(struct output *output)
3544{
3545 return output->output;
3546}
3547
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003548static void
Daniel Stone97f68542012-05-30 16:32:01 +01003549fini_xkb(struct input *input)
3550{
3551 xkb_state_unref(input->xkb.state);
3552 xkb_map_unref(input->xkb.keymap);
3553}
3554
3555static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003556display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003557{
3558 struct input *input;
3559
3560 input = malloc(sizeof *input);
3561 if (input == NULL)
3562 return;
3563
3564 memset(input, 0, sizeof *input);
3565 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003566 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003567 input->pointer_focus = NULL;
3568 input->keyboard_focus = NULL;
3569 wl_list_insert(d->input_list.prev, &input->link);
3570
Daniel Stone37816df2012-05-16 18:45:18 +01003571 wl_seat_add_listener(input->seat, &seat_listener, input);
3572 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003573
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003574 input->data_device =
3575 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003576 input->seat);
3577 wl_data_device_add_listener(input->data_device, &data_device_listener,
3578 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003579
3580 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003581
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003582 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3583 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003584 input->repeat_task.run = keyboard_repeat_func;
3585 display_watch_fd(d, input->repeat_timer_fd,
3586 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003587}
3588
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003589static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003590input_destroy(struct input *input)
3591{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003592 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003593 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003594
3595 if (input->drag_offer)
3596 data_offer_destroy(input->drag_offer);
3597
3598 if (input->selection_offer)
3599 data_offer_destroy(input->selection_offer);
3600
3601 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003602 fini_xkb(input);
3603
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003604 wl_surface_destroy(input->pointer_surface);
3605
Pekka Paalanene1207c72011-12-16 12:02:09 +02003606 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003607 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003608 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003609 free(input);
3610}
3611
3612static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003613init_workspace_manager(struct display *d, uint32_t id)
3614{
3615 d->workspace_manager =
3616 wl_display_bind(d->display, id, &workspace_manager_interface);
3617 if (d->workspace_manager != NULL)
3618 workspace_manager_add_listener(d->workspace_manager,
3619 &workspace_manager_listener,
3620 d);
3621}
3622
3623static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003624display_handle_global(struct wl_display *display, uint32_t id,
3625 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003626{
3627 struct display *d = data;
3628
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003629 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003630 d->compositor =
3631 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003632 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003633 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003634 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003635 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003636 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003637 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003638 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003639 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003640 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3641 d->data_device_manager =
3642 wl_display_bind(display, id,
3643 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003644 } else if (strcmp(interface, "text_cursor_position") == 0) {
3645 d->text_cursor_position =
3646 wl_display_bind(display, id,
3647 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003648 } else if (strcmp(interface, "workspace_manager") == 0) {
3649 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003650 }
3651}
3652
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003653#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003654static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003655init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003656{
3657 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003658 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003659
Rob Clark6396ed32012-03-11 19:48:41 -05003660#ifdef USE_CAIRO_GLESV2
3661# define GL_BIT EGL_OPENGL_ES2_BIT
3662#else
3663# define GL_BIT EGL_OPENGL_BIT
3664#endif
3665
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003666 static const EGLint argb_cfg_attribs[] = {
3667 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003668 EGL_RED_SIZE, 1,
3669 EGL_GREEN_SIZE, 1,
3670 EGL_BLUE_SIZE, 1,
3671 EGL_ALPHA_SIZE, 1,
3672 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003673 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003674 EGL_NONE
3675 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003676
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003677#ifdef USE_CAIRO_GLESV2
3678 static const EGLint context_attribs[] = {
3679 EGL_CONTEXT_CLIENT_VERSION, 2,
3680 EGL_NONE
3681 };
3682 EGLint api = EGL_OPENGL_ES_API;
3683#else
3684 EGLint *context_attribs = NULL;
3685 EGLint api = EGL_OPENGL_API;
3686#endif
3687
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003688 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003689 if (!eglInitialize(d->dpy, &major, &minor)) {
3690 fprintf(stderr, "failed to initialize display\n");
3691 return -1;
3692 }
3693
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003694 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003695 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3696 return -1;
3697 }
3698
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003699 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3700 &d->argb_config, 1, &n) || n != 1) {
3701 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003702 return -1;
3703 }
3704
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003705 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003706 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003707 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003708 fprintf(stderr, "failed to create context\n");
3709 return -1;
3710 }
3711
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003712 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003713 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003714 return -1;
3715 }
3716
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003717#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003718 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3719 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3720 fprintf(stderr, "failed to get cairo egl argb device\n");
3721 return -1;
3722 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003723#endif
3724
3725 return 0;
3726}
3727
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003728static void
3729fini_egl(struct display *display)
3730{
3731#ifdef HAVE_CAIRO_EGL
3732 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003733#endif
3734
3735 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3736 EGL_NO_CONTEXT);
3737
3738 eglTerminate(display->dpy);
3739 eglReleaseThread();
3740}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003741#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003742
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003743static int
3744event_mask_update(uint32_t mask, void *data)
3745{
3746 struct display *d = data;
3747
3748 d->mask = mask;
3749
3750 return 0;
3751}
3752
3753static void
3754handle_display_data(struct task *task, uint32_t events)
3755{
3756 struct display *display =
3757 container_of(task, struct display, display_task);
U. Artie Eoff44874d92012-10-02 21:12:35 -07003758
3759 display->display_fd_events = events;
3760
3761 if (events & EPOLLERR || events & EPOLLHUP) {
3762 display_exit(display);
3763 return;
3764 }
3765
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003766 wl_display_iterate(display->display, display->mask);
3767}
3768
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003769struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003770display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003771{
3772 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003773
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003774 d = malloc(sizeof *d);
3775 if (d == NULL)
3776 return NULL;
3777
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003778 memset(d, 0, sizeof *d);
3779
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003780 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003781 if (d->display == NULL) {
3782 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003783 return NULL;
3784 }
3785
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003786 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003787 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3788 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003789 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3790 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003791
3792 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003793 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003794 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003795
Daniel Stone97f68542012-05-30 16:32:01 +01003796 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003797 if (d->xkb_context == NULL) {
3798 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003799 return NULL;
3800 }
3801
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003802 d->workspace = 0;
3803 d->workspace_count = 1;
3804
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003805 /* Set up listener so we'll catch all events. */
3806 wl_display_add_global_listener(d->display,
3807 display_handle_global, d);
3808
3809 /* Process connection events. */
3810 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003811#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003812 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003813 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003814#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003815
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003816 d->image_target_texture_2d =
3817 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3818 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3819 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3820
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003821 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003822
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003823 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003824
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003825 wl_list_init(&d->window_list);
3826
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003827 return d;
3828}
3829
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003830static void
3831display_destroy_outputs(struct display *display)
3832{
3833 struct output *tmp;
3834 struct output *output;
3835
3836 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3837 output_destroy(output);
3838}
3839
Pekka Paalanene1207c72011-12-16 12:02:09 +02003840static void
3841display_destroy_inputs(struct display *display)
3842{
3843 struct input *tmp;
3844 struct input *input;
3845
3846 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3847 input_destroy(input);
3848}
3849
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003850void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003851display_destroy(struct display *display)
3852{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003853 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003854 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3855 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003856
3857 if (!wl_list_empty(&display->deferred_list))
3858 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3859
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003860 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003861 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003862
Daniel Stone97f68542012-05-30 16:32:01 +01003863 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003864
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003865 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003866 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003867
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003868#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003869 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003870#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003871
Pekka Paalanenc2052982011-12-16 11:41:32 +02003872 if (display->shell)
3873 wl_shell_destroy(display->shell);
3874
3875 if (display->shm)
3876 wl_shm_destroy(display->shm);
3877
3878 if (display->data_device_manager)
3879 wl_data_device_manager_destroy(display->data_device_manager);
3880
3881 wl_compositor_destroy(display->compositor);
3882
3883 close(display->epoll_fd);
3884
U. Artie Eoff44874d92012-10-02 21:12:35 -07003885 if (!(display->display_fd_events & EPOLLERR) &&
3886 !(display->display_fd_events & EPOLLHUP))
3887 wl_display_flush(display->display);
3888
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003889 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003890 free(display);
3891}
3892
3893void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003894display_set_user_data(struct display *display, void *data)
3895{
3896 display->user_data = data;
3897}
3898
3899void *
3900display_get_user_data(struct display *display)
3901{
3902 return display->user_data;
3903}
3904
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003905struct wl_display *
3906display_get_display(struct display *display)
3907{
3908 return display->display;
3909}
3910
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003911struct output *
3912display_get_output(struct display *display)
3913{
3914 return container_of(display->output_list.next, struct output, link);
3915}
3916
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003917struct wl_compositor *
3918display_get_compositor(struct display *display)
3919{
3920 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003921}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003922
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003923uint32_t
3924display_get_serial(struct display *display)
3925{
3926 return display->serial;
3927}
3928
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003929EGLDisplay
3930display_get_egl_display(struct display *d)
3931{
3932 return d->dpy;
3933}
3934
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003935struct wl_data_source *
3936display_create_data_source(struct display *display)
3937{
3938 return wl_data_device_manager_create_data_source(display->data_device_manager);
3939}
3940
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003941EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003942display_get_argb_egl_config(struct display *d)
3943{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003944 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003945}
3946
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003947struct wl_shell *
3948display_get_shell(struct display *display)
3949{
3950 return display->shell;
3951}
3952
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003953int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003954display_acquire_window_surface(struct display *display,
3955 struct window *window,
3956 EGLContext ctx)
3957{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003958#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003959 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003960 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003961
3962 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003963 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003964 device = cairo_surface_get_device(window->cairo_surface);
3965 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003966 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003967
Benjamin Franzke0c991632011-09-27 21:57:31 +02003968 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003969 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003970 ctx = display->argb_ctx;
3971 else
3972 assert(0);
3973 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003974
3975 data = cairo_surface_get_user_data(window->cairo_surface,
3976 &surface_data_key);
3977
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003978 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003979 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003980 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3981 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003982
3983 return 0;
3984#else
3985 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003986#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003987}
3988
3989void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003990display_release_window_surface(struct display *display,
3991 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003992{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003993#ifdef HAVE_CAIRO_EGL
3994 cairo_device_t *device;
3995
3996 device = cairo_surface_get_device(window->cairo_surface);
3997 if (!device)
3998 return;
3999
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004000 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004001 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004002 cairo_device_release(device);
4003#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004004}
4005
4006void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004007display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004008{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004009 wl_list_insert(&display->deferred_list, &task->link);
4010}
4011
4012void
4013display_watch_fd(struct display *display,
4014 int fd, uint32_t events, struct task *task)
4015{
4016 struct epoll_event ep;
4017
4018 ep.events = events;
4019 ep.data.ptr = task;
4020 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4021}
4022
4023void
4024display_run(struct display *display)
4025{
4026 struct task *task;
4027 struct epoll_event ep[16];
4028 int i, count;
4029
Pekka Paalanen826d7952011-12-15 10:14:07 +02004030 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004031 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004032 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004033
4034 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004035 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004036 struct task, link);
4037 wl_list_remove(&task->link);
4038 task->run(task, 0);
4039 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004040
4041 if (!display->running)
4042 break;
4043
4044 wl_display_flush(display->display);
4045
4046 count = epoll_wait(display->epoll_fd,
4047 ep, ARRAY_LENGTH(ep), -1);
4048 for (i = 0; i < count; i++) {
4049 task = ep[i].data.ptr;
4050 task->run(task, ep[i].events);
4051 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004052 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004053}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004054
4055void
4056display_exit(struct display *display)
4057{
4058 display->running = 0;
4059}