blob: aba12540fd01956110ce41f4918eed61cc465af7 [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;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001385 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001387 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001388 decoration_width = (t->width + t->margin) * 2;
1389 decoration_height = t->width +
1390 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001391
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001392 allocation.x = t->width + t->margin;
1393 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001394 allocation.width = width - decoration_width;
1395 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001396
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001397 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001398
1399 wl_list_for_each(button, &frame->buttons_list, link)
1400 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001401 } else {
1402 decoration_width = 0;
1403 decoration_height = 0;
1404
1405 allocation.x = 0;
1406 allocation.y = 0;
1407 allocation.width = width;
1408 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001409 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001410
1411 wl_list_for_each(button, &frame->buttons_list, link)
1412 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001413 }
1414
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001415 widget_set_allocation(child, allocation.x, allocation.y,
1416 allocation.width, allocation.height);
1417
1418 if (child->resize_handler)
1419 child->resize_handler(child,
1420 allocation.width,
1421 allocation.height,
1422 child->user_data);
1423
Scott Moreauf7e498c2012-05-14 11:39:29 -06001424 width = child->allocation.width + decoration_width;
1425 height = child->allocation.height + decoration_height;
1426
Kristian Høgsberg023be102012-07-31 11:59:12 -04001427 if (widget->window->type != TYPE_FULLSCREEN) {
1428 widget->window->input_region =
1429 wl_compositor_create_region(display->compositor);
1430 wl_region_add(widget->window->input_region,
1431 t->margin, t->margin,
1432 width - 2 * t->margin,
1433 height - 2 * t->margin);
1434 }
1435
Scott Moreauf7e498c2012-05-14 11:39:29 -06001436 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001437
1438 if (child->opaque) {
1439 widget->window->opaque_region =
1440 wl_compositor_create_region(display->compositor);
1441 wl_region_add(widget->window->opaque_region,
1442 opaque_margin, opaque_margin,
1443 widget->allocation.width - 2 * opaque_margin,
1444 widget->allocation.height - 2 * opaque_margin);
1445 }
Martin Minarik1998b152012-05-10 02:04:35 +02001446
1447 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001448 x_r = frame->widget->allocation.width - t->width - t->margin;
1449 x_l = t->width + t->margin;
1450 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001451 wl_list_for_each(button, &frame->buttons_list, link) {
1452 const int button_padding = 4;
1453 w = cairo_image_surface_get_width(button->icon);
1454 h = cairo_image_surface_get_height(button->icon);
1455
1456 if (button->decoration == FRAME_BUTTON_FANCY)
1457 w += 10;
1458
1459 if (button->align == FRAME_BUTTON_LEFT) {
1460 widget_set_allocation(button->widget,
1461 x_l, y , w + 1, h + 1);
1462 x_l += w;
1463 x_l += button_padding;
1464 } else {
1465 x_r -= w;
1466 widget_set_allocation(button->widget,
1467 x_r, y , w + 1, h + 1);
1468 x_r -= button_padding;
1469 }
1470 }
1471}
1472
1473static int
1474frame_button_enter_handler(struct widget *widget,
1475 struct input *input, float x, float y, void *data)
1476{
1477 struct frame_button *frame_button = data;
1478
1479 widget_schedule_redraw(frame_button->widget);
1480 frame_button->state = FRAME_BUTTON_OVER;
1481
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001482 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001483}
1484
1485static void
1486frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1487{
1488 struct frame_button *frame_button = data;
1489
1490 widget_schedule_redraw(frame_button->widget);
1491 frame_button->state = FRAME_BUTTON_DEFAULT;
1492}
1493
1494static void
1495frame_button_button_handler(struct widget *widget,
1496 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001497 uint32_t button,
1498 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001499{
1500 struct frame_button *frame_button = data;
1501 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001502 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02001503
1504 if (button != BTN_LEFT)
1505 return;
1506
1507 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001508 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001509 frame_button->state = FRAME_BUTTON_ACTIVE;
1510 widget_schedule_redraw(frame_button->widget);
1511
1512 if (frame_button->type == FRAME_BUTTON_ICON)
1513 window_show_frame_menu(window, input, time);
1514 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001515 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001516 frame_button->state = FRAME_BUTTON_DEFAULT;
1517 widget_schedule_redraw(frame_button->widget);
1518 break;
1519 }
1520
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001521 if (!was_pressed)
1522 return;
1523
Martin Minarik1998b152012-05-10 02:04:35 +02001524 switch (frame_button->type) {
1525 case FRAME_BUTTON_CLOSE:
1526 if (window->close_handler)
1527 window->close_handler(window->parent,
1528 window->user_data);
1529 else
1530 display_exit(window->display);
1531 break;
1532 case FRAME_BUTTON_MINIMIZE:
1533 fprintf(stderr,"Minimize stub\n");
1534 break;
1535 case FRAME_BUTTON_MAXIMIZE:
1536 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1537 break;
1538 default:
1539 /* Unknown operation */
1540 break;
1541 }
1542}
1543
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001544static int
1545frame_button_motion_handler(struct widget *widget,
1546 struct input *input, uint32_t time,
1547 float x, float y, void *data)
1548{
1549 struct frame_button *frame_button = data;
1550 enum frame_button_pointer previous_button_state = frame_button->state;
1551
1552 /* only track state for a pressed button */
1553 if (input->grab != widget)
1554 return CURSOR_LEFT_PTR;
1555
1556 if (x > widget->allocation.x &&
1557 x < (widget->allocation.x + widget->allocation.width) &&
1558 y > widget->allocation.y &&
1559 y < (widget->allocation.y + widget->allocation.height)) {
1560 frame_button->state = FRAME_BUTTON_ACTIVE;
1561 } else {
1562 frame_button->state = FRAME_BUTTON_DEFAULT;
1563 }
1564
1565 if (frame_button->state != previous_button_state)
1566 widget_schedule_redraw(frame_button->widget);
1567
1568 return CURSOR_LEFT_PTR;
1569}
1570
Martin Minarik1998b152012-05-10 02:04:35 +02001571static void
1572frame_button_redraw_handler(struct widget *widget, void *data)
1573{
1574 struct frame_button *frame_button = data;
1575 cairo_t *cr;
1576 int width, height, x, y;
1577 struct window *window = widget->window;
1578
1579 x = widget->allocation.x;
1580 y = widget->allocation.y;
1581 width = widget->allocation.width;
1582 height = widget->allocation.height;
1583
1584 if (!width)
1585 return;
1586 if (!height)
1587 return;
1588 if (widget->opaque)
1589 return;
1590
1591 cr = cairo_create(window->cairo_surface);
1592
1593 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1594 cairo_set_line_width(cr, 1);
1595
1596 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1597 cairo_rectangle (cr, x, y, 25, 16);
1598
1599 cairo_stroke_preserve(cr);
1600
1601 switch (frame_button->state) {
1602 case FRAME_BUTTON_DEFAULT:
1603 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1604 break;
1605 case FRAME_BUTTON_OVER:
1606 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1607 break;
1608 case FRAME_BUTTON_ACTIVE:
1609 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1610 break;
1611 }
1612
1613 cairo_fill (cr);
1614
1615 x += 4;
1616 }
1617
1618 cairo_set_source_surface(cr, frame_button->icon, x, y);
1619 cairo_paint(cr);
1620
1621 cairo_destroy(cr);
1622}
1623
1624static struct widget *
1625frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1626 enum frame_button_align align, enum frame_button_decoration style)
1627{
1628 struct frame_button *frame_button;
1629 const char *icon = data;
1630
1631 frame_button = malloc (sizeof *frame_button);
1632 memset(frame_button, 0, sizeof *frame_button);
1633
1634 frame_button->icon = cairo_image_surface_create_from_png(icon);
1635 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1636 frame_button->frame = frame;
1637 frame_button->type = type;
1638 frame_button->align = align;
1639 frame_button->decoration = style;
1640
1641 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1642
1643 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1644 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1645 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1646 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03001647 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02001648 return frame_button->widget;
1649}
1650
1651static void
1652frame_button_destroy(struct frame_button *frame_button)
1653{
1654 widget_destroy(frame_button->widget);
1655 wl_list_remove(&frame_button->link);
1656 cairo_surface_destroy(frame_button->icon);
1657 free(frame_button);
1658
1659 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660}
1661
1662static void
1663frame_redraw_handler(struct widget *widget, void *data)
1664{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001665 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001666 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001667 struct theme *t = window->display->theme;
1668 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001669
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001670 if (window->type == TYPE_FULLSCREEN)
1671 return;
1672
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001673 cr = cairo_create(window->cairo_surface);
1674
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001675 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001676 flags |= THEME_FRAME_ACTIVE;
1677 theme_render_frame(t, cr, widget->allocation.width,
1678 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001679
1680 cairo_destroy(cr);
1681}
1682
1683static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001686 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001687 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001688
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001689 location = theme_get_location(t, input->sx, input->sy,
1690 frame->widget->allocation.width,
1691 frame->widget->allocation.height);
1692
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001693 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001694 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001695 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001696 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001697 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001698 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001699 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001700 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001701 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001702 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001703 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001704 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001705 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001706 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001707 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001708 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001709 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001710 case THEME_LOCATION_EXTERIOR:
1711 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001712 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001713 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001714 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001715}
1716
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001717static void
1718frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001719{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001720 struct display *display;
1721
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001722 switch (index) {
1723 case 0: /* close */
1724 if (window->close_handler)
1725 window->close_handler(window->parent,
1726 window->user_data);
1727 else
1728 display_exit(window->display);
1729 break;
1730 case 1: /* fullscreen */
1731 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001732 if (window->fullscreen_handler)
1733 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001734 break;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001735 case 2: /* move to workspace above */
1736 display = window->display;
1737 if (display->workspace > 0)
1738 workspace_manager_move_surface(display->workspace_manager,
1739 window->surface,
1740 display->workspace - 1);
1741 break;
1742 case 3: /* move to workspace below */
1743 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001744 if (display->workspace < display->workspace_count - 1)
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001745 workspace_manager_move_surface(display->workspace_manager,
1746 window->surface,
1747 display->workspace + 1);
1748 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001749 }
1750}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001751
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001752void
1753window_show_frame_menu(struct window *window,
1754 struct input *input, uint32_t time)
1755{
1756 int32_t x, y;
1757
1758 static const char *entries[] = {
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02001759 "Close", "Fullscreen",
1760 "Move to workspace above", "Move to workspace below"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001761 };
1762
1763 input_get_position(input, &x, &y);
1764 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001765 x - 10, y - 10, frame_menu_func, entries,
1766 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001767}
1768
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001769static int
1770frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001771 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001772{
1773 return frame_get_pointer_image_for_location(data, input);
1774}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001775
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001776static int
1777frame_motion_handler(struct widget *widget,
1778 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001779 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001780{
1781 return frame_get_pointer_image_for_location(data, input);
1782}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001783
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001784static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001785frame_button_handler(struct widget *widget,
1786 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001787 uint32_t button, enum wl_pointer_button_state state,
1788 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001789
1790{
1791 struct frame *frame = data;
1792 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001793 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001794 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001795
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001796 location = theme_get_location(display->theme, input->sx, input->sy,
1797 frame->widget->allocation.width,
1798 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001799
Daniel Stone4dbadb12012-05-30 16:31:51 +01001800 if (window->display->shell && button == BTN_LEFT &&
1801 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001802 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001803 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804 if (!window->shell_surface)
1805 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001806 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001807 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001808 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001809 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001810 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001812 case THEME_LOCATION_RESIZING_TOP:
1813 case THEME_LOCATION_RESIZING_BOTTOM:
1814 case THEME_LOCATION_RESIZING_LEFT:
1815 case THEME_LOCATION_RESIZING_RIGHT:
1816 case THEME_LOCATION_RESIZING_TOP_LEFT:
1817 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1818 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1819 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001820 if (!window->shell_surface)
1821 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001822 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001823
1824 if (!display->dpy) {
1825 /* If we're using shm, allocate a big
1826 pool to create buffers out of while
1827 we resize. We should probably base
1828 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001829 window->pool =
1830 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001831 }
1832
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001833 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001834 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001835 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001836 break;
1837 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001838 } else if (button == BTN_RIGHT &&
1839 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001840 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001841 }
1842}
1843
1844struct widget *
1845frame_create(struct window *window, void *data)
1846{
1847 struct frame *frame;
1848
1849 frame = malloc(sizeof *frame);
1850 memset(frame, 0, sizeof *frame);
1851
1852 frame->widget = window_add_widget(window, frame);
1853 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001854
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001855 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1856 widget_set_resize_handler(frame->widget, frame_resize_handler);
1857 widget_set_enter_handler(frame->widget, frame_enter_handler);
1858 widget_set_motion_handler(frame->widget, frame_motion_handler);
1859 widget_set_button_handler(frame->widget, frame_button_handler);
1860
Martin Minarik1998b152012-05-10 02:04:35 +02001861 /* Create empty list for frame buttons */
1862 wl_list_init(&frame->buttons_list);
1863
1864 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1865 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1866
1867 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1868 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1869
1870 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1871 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1872
1873 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1874 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1875
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001876 window->frame = frame;
1877
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001878 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001879}
1880
Kristian Høgsberga1627922012-06-20 17:30:03 -04001881void
1882frame_set_child_size(struct widget *widget, int child_width, int child_height)
1883{
1884 struct display *display = widget->window->display;
1885 struct theme *t = display->theme;
1886 int decoration_width, decoration_height;
1887 int width, height;
1888
1889 if (widget->window->type != TYPE_FULLSCREEN) {
1890 decoration_width = (t->width + t->margin) * 2;
1891 decoration_height = t->width +
1892 t->titlebar_height + t->margin * 2;
1893
1894 width = child_width + decoration_width;
1895 height = child_height + decoration_height;
1896 } else {
1897 width = child_width;
1898 height = child_height;
1899 }
1900
1901 window_schedule_resize(widget->window, width, height);
1902}
1903
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001904static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001905frame_destroy(struct frame *frame)
1906{
Martin Minarik1998b152012-05-10 02:04:35 +02001907 struct frame_button *button, *tmp;
1908
1909 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1910 frame_button_destroy(button);
1911
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001912 /* frame->child must be destroyed by the application */
1913 widget_destroy(frame->widget);
1914 free(frame);
1915}
1916
1917static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001918input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001919 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001920{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001921 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001922 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001923
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001924 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001925 return;
1926
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001927 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001928 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001929 widget = old;
1930 if (input->grab)
1931 widget = input->grab;
1932 if (widget->leave_handler)
1933 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001934 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001935 }
1936
1937 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001938 widget = focus;
1939 if (input->grab)
1940 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001941 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001942 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001943 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001944 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001945
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001946 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001947 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001948}
1949
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001950void
1951input_grab(struct input *input, struct widget *widget, uint32_t button)
1952{
1953 input->grab = widget;
1954 input->grab_button = button;
1955}
1956
1957void
1958input_ungrab(struct input *input)
1959{
1960 struct widget *widget;
1961
1962 input->grab = NULL;
1963 if (input->pointer_focus) {
1964 widget = widget_find_widget(input->pointer_focus->widget,
1965 input->sx, input->sy);
1966 input_set_focus_widget(input, widget, input->sx, input->sy);
1967 }
1968}
1969
1970static void
1971input_remove_pointer_focus(struct input *input)
1972{
1973 struct window *window = input->pointer_focus;
1974
1975 if (!window)
1976 return;
1977
1978 input_set_focus_widget(input, NULL, 0, 0);
1979
1980 input->pointer_focus = NULL;
1981 input->current_cursor = CURSOR_UNSET;
1982}
1983
1984static void
1985pointer_handle_enter(void *data, struct wl_pointer *pointer,
1986 uint32_t serial, struct wl_surface *surface,
1987 wl_fixed_t sx_w, wl_fixed_t sy_w)
1988{
1989 struct input *input = data;
1990 struct window *window;
1991 struct widget *widget;
1992 float sx = wl_fixed_to_double(sx_w);
1993 float sy = wl_fixed_to_double(sy_w);
1994
1995 if (!surface) {
1996 /* enter event for a window we've just destroyed */
1997 return;
1998 }
1999
2000 input->display->serial = serial;
2001 input->pointer_enter_serial = serial;
2002 input->pointer_focus = wl_surface_get_user_data(surface);
2003 window = input->pointer_focus;
2004
2005 if (window->pool) {
2006 shm_pool_destroy(window->pool);
2007 window->pool = NULL;
2008 /* Schedule a redraw to free the pool */
2009 window_schedule_redraw(window);
2010 }
2011
2012 input->sx = sx;
2013 input->sy = sy;
2014
2015 widget = widget_find_widget(window->widget, sx, sy);
2016 input_set_focus_widget(input, widget, sx, sy);
2017}
2018
2019static void
2020pointer_handle_leave(void *data, struct wl_pointer *pointer,
2021 uint32_t serial, struct wl_surface *surface)
2022{
2023 struct input *input = data;
2024
2025 input->display->serial = serial;
2026 input_remove_pointer_focus(input);
2027}
2028
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002029static void
Daniel Stone37816df2012-05-16 18:45:18 +01002030pointer_handle_motion(void *data, struct wl_pointer *pointer,
2031 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002032{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002033 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002034 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002035 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002036 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002037 float sx = wl_fixed_to_double(sx_w);
2038 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002039
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002040 input->sx = sx;
2041 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002042
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002043 if (!window)
2044 return;
2045
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002046 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002047 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002048 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002049 }
2050
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002051 if (input->grab)
2052 widget = input->grab;
2053 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002054 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002055 if (widget && widget->motion_handler)
Philipp Brüschweiler8c9c8fc2012-09-01 16:21:40 +02002056 cursor = widget->motion_handler(widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002057 input, time, sx, sy,
2058 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002059
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002060 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002061}
2062
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002063static void
Daniel Stone37816df2012-05-16 18:45:18 +01002064pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002065 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002066{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002067 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002068 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002069 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002070
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002071 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002072 if (input->focus_widget && input->grab == NULL &&
2073 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002074 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002075
Neil Roberts6b28aad2012-01-23 19:11:18 +00002076 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002077 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002078 (*widget->button_handler)(widget,
2079 input, time,
2080 button, state,
2081 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002082
Daniel Stone4dbadb12012-05-30 16:31:51 +01002083 if (input->grab && input->grab_button == button &&
2084 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002085 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002086}
2087
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002088static void
Daniel Stone37816df2012-05-16 18:45:18 +01002089pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002090 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002091{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002092 struct input *input = data;
2093 struct widget *widget;
2094
2095 widget = input->focus_widget;
2096 if (input->grab)
2097 widget = input->grab;
2098 if (widget && widget->axis_handler)
2099 (*widget->axis_handler)(widget,
2100 input, time,
2101 axis, value,
2102 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002103}
2104
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002105static const struct wl_pointer_listener pointer_listener = {
2106 pointer_handle_enter,
2107 pointer_handle_leave,
2108 pointer_handle_motion,
2109 pointer_handle_button,
2110 pointer_handle_axis,
2111};
2112
2113static void
2114input_remove_keyboard_focus(struct input *input)
2115{
2116 struct window *window = input->keyboard_focus;
2117 struct itimerspec its;
2118
2119 its.it_interval.tv_sec = 0;
2120 its.it_interval.tv_nsec = 0;
2121 its.it_value.tv_sec = 0;
2122 its.it_value.tv_nsec = 0;
2123 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2124
2125 if (!window)
2126 return;
2127
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002128 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002129 if (window->keyboard_focus_handler)
2130 (*window->keyboard_focus_handler)(window, NULL,
2131 window->user_data);
2132
2133 input->keyboard_focus = NULL;
2134}
2135
2136static void
2137keyboard_repeat_func(struct task *task, uint32_t events)
2138{
2139 struct input *input =
2140 container_of(task, struct input, repeat_task);
2141 struct window *window = input->keyboard_focus;
2142 uint64_t exp;
2143
2144 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2145 /* If we change the timer between the fd becoming
2146 * readable and getting here, there'll be nothing to
2147 * read and we get EAGAIN. */
2148 return;
2149
2150 if (window && window->key_handler) {
2151 (*window->key_handler)(window, input, input->repeat_time,
2152 input->repeat_key, input->repeat_sym,
2153 WL_KEYBOARD_KEY_STATE_PRESSED,
2154 window->user_data);
2155 }
2156}
2157
2158static void
2159keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2160 uint32_t format, int fd, uint32_t size)
2161{
2162 struct input *input = data;
2163 char *map_str;
2164
2165 if (!data) {
2166 close(fd);
2167 return;
2168 }
2169
2170 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2171 close(fd);
2172 return;
2173 }
2174
2175 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2176 if (map_str == MAP_FAILED) {
2177 close(fd);
2178 return;
2179 }
2180
2181 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2182 map_str,
2183 XKB_KEYMAP_FORMAT_TEXT_V1,
2184 0);
2185 munmap(map_str, size);
2186 close(fd);
2187
2188 if (!input->xkb.keymap) {
2189 fprintf(stderr, "failed to compile keymap\n");
2190 return;
2191 }
2192
2193 input->xkb.state = xkb_state_new(input->xkb.keymap);
2194 if (!input->xkb.state) {
2195 fprintf(stderr, "failed to create XKB state\n");
2196 xkb_map_unref(input->xkb.keymap);
2197 input->xkb.keymap = NULL;
2198 return;
2199 }
2200
2201 input->xkb.control_mask =
2202 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2203 input->xkb.alt_mask =
2204 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2205 input->xkb.shift_mask =
2206 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2207}
2208
2209static void
2210keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2211 uint32_t serial, struct wl_surface *surface,
2212 struct wl_array *keys)
2213{
2214 struct input *input = data;
2215 struct window *window;
2216
2217 input->display->serial = serial;
2218 input->keyboard_focus = wl_surface_get_user_data(surface);
2219
2220 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002221 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002222 if (window->keyboard_focus_handler)
2223 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002224 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002225}
2226
2227static void
2228keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2229 uint32_t serial, struct wl_surface *surface)
2230{
2231 struct input *input = data;
2232
2233 input->display->serial = serial;
2234 input_remove_keyboard_focus(input);
2235}
2236
Scott Moreau210d0792012-03-22 10:47:01 -06002237static void
Daniel Stone37816df2012-05-16 18:45:18 +01002238keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002239 uint32_t serial, uint32_t time, uint32_t key,
2240 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002241{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002242 struct input *input = data;
2243 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002244 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002245 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002246 const xkb_keysym_t *syms;
2247 xkb_keysym_t sym;
2248 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002249 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002250
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002251 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002252 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002253 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002254 return;
2255
Daniel Stone97f68542012-05-30 16:32:01 +01002256 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002257
Daniel Stone97f68542012-05-30 16:32:01 +01002258 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002259 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002260 XKB_STATE_LATCHED);
2261 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002262 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002263 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002264 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002265 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002266 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002267 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002268
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002269 sym = XKB_KEY_NoSymbol;
2270 if (num_syms == 1)
2271 sym = syms[0];
2272
2273 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002274 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002275 window_set_maximized(window,
2276 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002277 } else if (sym == XKB_KEY_F11 &&
2278 window->fullscreen_handler &&
2279 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2280 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002281 } else if (sym == XKB_KEY_F4 &&
2282 input->modifiers == MOD_ALT_MASK &&
2283 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2284 if (window->close_handler)
2285 window->close_handler(window->parent,
2286 window->user_data);
2287 else
2288 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002289 } else if (window->key_handler) {
2290 (*window->key_handler)(window, input, time, key,
2291 sym, state, window->user_data);
2292 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002293
2294 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2295 key == input->repeat_key) {
2296 its.it_interval.tv_sec = 0;
2297 its.it_interval.tv_nsec = 0;
2298 its.it_value.tv_sec = 0;
2299 its.it_value.tv_nsec = 0;
2300 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2301 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2302 input->repeat_sym = sym;
2303 input->repeat_key = key;
2304 input->repeat_time = time;
2305 its.it_interval.tv_sec = 0;
2306 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2307 its.it_value.tv_sec = 0;
2308 its.it_value.tv_nsec = 400 * 1000 * 1000;
2309 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2310 }
2311}
2312
2313static void
Daniel Stone351eb612012-05-31 15:27:47 -04002314keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2315 uint32_t serial, uint32_t mods_depressed,
2316 uint32_t mods_latched, uint32_t mods_locked,
2317 uint32_t group)
2318{
2319 struct input *input = data;
2320
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002321 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2322 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002323}
2324
Daniel Stone37816df2012-05-16 18:45:18 +01002325static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002326 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002327 keyboard_handle_enter,
2328 keyboard_handle_leave,
2329 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002330 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002331};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002332
2333static void
Daniel Stone37816df2012-05-16 18:45:18 +01002334seat_handle_capabilities(void *data, struct wl_seat *seat,
2335 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002336{
Daniel Stone37816df2012-05-16 18:45:18 +01002337 struct input *input = data;
2338
2339 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2340 input->pointer = wl_seat_get_pointer(seat);
2341 wl_pointer_set_user_data(input->pointer, input);
2342 wl_pointer_add_listener(input->pointer, &pointer_listener,
2343 input);
2344 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2345 wl_pointer_destroy(input->pointer);
2346 input->pointer = NULL;
2347 }
2348
2349 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2350 input->keyboard = wl_seat_get_keyboard(seat);
2351 wl_keyboard_set_user_data(input->keyboard, input);
2352 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2353 input);
2354 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2355 wl_keyboard_destroy(input->keyboard);
2356 input->keyboard = NULL;
2357 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002358}
2359
Daniel Stone37816df2012-05-16 18:45:18 +01002360static const struct wl_seat_listener seat_listener = {
2361 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002362};
2363
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002364void
2365input_get_position(struct input *input, int32_t *x, int32_t *y)
2366{
2367 *x = input->sx;
2368 *y = input->sy;
2369}
2370
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002371struct display *
2372input_get_display(struct input *input)
2373{
2374 return input->display;
2375}
2376
Daniel Stone37816df2012-05-16 18:45:18 +01002377struct wl_seat *
2378input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002379{
Daniel Stone37816df2012-05-16 18:45:18 +01002380 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002381}
2382
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002383uint32_t
2384input_get_modifiers(struct input *input)
2385{
2386 return input->modifiers;
2387}
2388
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002389struct widget *
2390input_get_focus_widget(struct input *input)
2391{
2392 return input->focus_widget;
2393}
2394
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002395struct data_offer {
2396 struct wl_data_offer *offer;
2397 struct input *input;
2398 struct wl_array types;
2399 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002400
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002401 struct task io_task;
2402 int fd;
2403 data_func_t func;
2404 int32_t x, y;
2405 void *user_data;
2406};
2407
2408static void
2409data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2410{
2411 struct data_offer *offer = data;
2412 char **p;
2413
2414 p = wl_array_add(&offer->types, sizeof *p);
2415 *p = strdup(type);
2416}
2417
2418static const struct wl_data_offer_listener data_offer_listener = {
2419 data_offer_offer,
2420};
2421
2422static void
2423data_offer_destroy(struct data_offer *offer)
2424{
2425 char **p;
2426
2427 offer->refcount--;
2428 if (offer->refcount == 0) {
2429 wl_data_offer_destroy(offer->offer);
2430 for (p = offer->types.data; *p; p++)
2431 free(*p);
2432 wl_array_release(&offer->types);
2433 free(offer);
2434 }
2435}
2436
2437static void
2438data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002439 struct wl_data_device *data_device,
2440 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002441{
2442 struct data_offer *offer;
2443
2444 offer = malloc(sizeof *offer);
2445
2446 wl_array_init(&offer->types);
2447 offer->refcount = 1;
2448 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002449 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002450 wl_data_offer_add_listener(offer->offer,
2451 &data_offer_listener, offer);
2452}
2453
2454static void
2455data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002456 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002457 wl_fixed_t x_w, wl_fixed_t y_w,
2458 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002459{
2460 struct input *input = data;
2461 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002462 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002463 float x = wl_fixed_to_double(x_w);
2464 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002465 char **p;
2466
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002467 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002468 window = wl_surface_get_user_data(surface);
2469 input->pointer_focus = window;
2470
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002471 if (offer) {
2472 input->drag_offer = wl_data_offer_get_user_data(offer);
2473
2474 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2475 *p = NULL;
2476
2477 types_data = input->drag_offer->types.data;
2478 } else {
2479 input->drag_offer = NULL;
2480 types_data = NULL;
2481 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002482
2483 window = input->pointer_focus;
2484 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002485 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002486 window->user_data);
2487}
2488
2489static void
2490data_device_leave(void *data, struct wl_data_device *data_device)
2491{
2492 struct input *input = data;
2493
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002494 if (input->drag_offer) {
2495 data_offer_destroy(input->drag_offer);
2496 input->drag_offer = NULL;
2497 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002498}
2499
2500static void
2501data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002502 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002503{
2504 struct input *input = data;
2505 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002506 float x = wl_fixed_to_double(x_w);
2507 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002508 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002509
2510 input->sx = x;
2511 input->sy = y;
2512
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002513 if (input->drag_offer)
2514 types_data = input->drag_offer->types.data;
2515 else
2516 types_data = NULL;
2517
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002518 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002519 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002520 window->user_data);
2521}
2522
2523static void
2524data_device_drop(void *data, struct wl_data_device *data_device)
2525{
2526 struct input *input = data;
2527 struct window *window = input->pointer_focus;
2528
2529 if (window->drop_handler)
2530 window->drop_handler(window, input,
2531 input->sx, input->sy, window->user_data);
2532}
2533
2534static void
2535data_device_selection(void *data,
2536 struct wl_data_device *wl_data_device,
2537 struct wl_data_offer *offer)
2538{
2539 struct input *input = data;
2540 char **p;
2541
2542 if (input->selection_offer)
2543 data_offer_destroy(input->selection_offer);
2544
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002545 if (offer) {
2546 input->selection_offer = wl_data_offer_get_user_data(offer);
2547 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2548 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002549 } else {
2550 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002551 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002552}
2553
2554static const struct wl_data_device_listener data_device_listener = {
2555 data_device_data_offer,
2556 data_device_enter,
2557 data_device_leave,
2558 data_device_motion,
2559 data_device_drop,
2560 data_device_selection
2561};
2562
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002563static void
2564input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002566 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002567 struct wl_cursor *cursor;
2568 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002569
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002570 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002571 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002572 return;
2573
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002574 if (index >= (int) cursor->image_count) {
2575 fprintf(stderr, "cursor index out of range\n");
2576 return;
2577 }
2578
2579 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002580 buffer = wl_cursor_image_get_buffer(image);
2581 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002582 return;
2583
Kristian Høgsbergae277372012-08-01 09:41:08 -04002584 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002585 input->pointer_surface,
2586 image->hotspot_x, image->hotspot_y);
2587 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2588 wl_surface_damage(input->pointer_surface, 0, 0,
2589 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002590}
2591
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002592static const struct wl_callback_listener pointer_surface_listener;
2593
2594static void
2595pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2596 uint32_t time)
2597{
2598 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002599 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002600 int i;
2601
2602 if (callback) {
2603 assert(callback == input->cursor_frame_cb);
2604 wl_callback_destroy(callback);
2605 input->cursor_frame_cb = NULL;
2606 }
2607
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002608 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002609 wl_pointer_set_cursor(input->pointer,
2610 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002611 NULL, 0, 0);
2612 return;
2613 }
2614
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002615 if (input->current_cursor == CURSOR_UNSET)
2616 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002617 cursor = input->display->cursors[input->current_cursor];
2618 if (!cursor)
2619 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002620
2621 /* FIXME We don't have the current time on the first call so we set
2622 * the animation start to the time of the first frame callback. */
2623 if (time == 0)
2624 input->cursor_anim_start = 0;
2625 else if (input->cursor_anim_start == 0)
2626 input->cursor_anim_start = time;
2627
2628 if (time == 0 || input->cursor_anim_start == 0)
2629 i = 0;
2630 else
2631 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2632
2633 input_set_pointer_image_index(input, i);
2634
2635 if (cursor->image_count == 1)
2636 return;
2637
2638 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2639 wl_callback_add_listener(input->cursor_frame_cb,
2640 &pointer_surface_listener, input);
2641}
2642
2643static const struct wl_callback_listener pointer_surface_listener = {
2644 pointer_surface_frame_callback
2645};
2646
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002647void
2648input_set_pointer_image(struct input *input, int pointer)
2649{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002650 int force = 0;
2651
2652 if (input->pointer_enter_serial > input->cursor_serial)
2653 force = 1;
2654
2655 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002656 return;
2657
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002658 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002659 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002660 if (!input->cursor_frame_cb)
2661 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002662 else if (force) {
2663 /* The current frame callback may be stuck if, for instance,
2664 * the set cursor request was processed by the server after
2665 * this client lost the focus. In this case the cursor surface
2666 * might not be mapped and the frame callback wouldn't ever
2667 * complete. Send a set_cursor and attach to try to map the
2668 * cursor surface again so that the callback will finish */
2669 input_set_pointer_image_index(input, 0);
2670 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002671}
2672
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002673struct wl_data_device *
2674input_get_data_device(struct input *input)
2675{
2676 return input->data_device;
2677}
2678
2679void
2680input_set_selection(struct input *input,
2681 struct wl_data_source *source, uint32_t time)
2682{
2683 wl_data_device_set_selection(input->data_device, source, time);
2684}
2685
2686void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002687input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002688{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002689 wl_data_offer_accept(input->drag_offer->offer,
2690 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002691}
2692
2693static void
2694offer_io_func(struct task *task, uint32_t events)
2695{
2696 struct data_offer *offer =
2697 container_of(task, struct data_offer, io_task);
2698 unsigned int len;
2699 char buffer[4096];
2700
2701 len = read(offer->fd, buffer, sizeof buffer);
2702 offer->func(buffer, len,
2703 offer->x, offer->y, offer->user_data);
2704
2705 if (len == 0) {
2706 close(offer->fd);
2707 data_offer_destroy(offer);
2708 }
2709}
2710
2711static void
2712data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2713 data_func_t func, void *user_data)
2714{
2715 int p[2];
2716
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002717 if (pipe2(p, O_CLOEXEC) == -1)
2718 return;
2719
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002720 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2721 close(p[1]);
2722
2723 offer->io_task.run = offer_io_func;
2724 offer->fd = p[0];
2725 offer->func = func;
2726 offer->refcount++;
2727 offer->user_data = user_data;
2728
2729 display_watch_fd(offer->input->display,
2730 offer->fd, EPOLLIN, &offer->io_task);
2731}
2732
2733void
2734input_receive_drag_data(struct input *input, const char *mime_type,
2735 data_func_t func, void *data)
2736{
2737 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2738 input->drag_offer->x = input->sx;
2739 input->drag_offer->y = input->sy;
2740}
2741
2742int
2743input_receive_selection_data(struct input *input, const char *mime_type,
2744 data_func_t func, void *data)
2745{
2746 char **p;
2747
2748 if (input->selection_offer == NULL)
2749 return -1;
2750
2751 for (p = input->selection_offer->types.data; *p; p++)
2752 if (strcmp(mime_type, *p) == 0)
2753 break;
2754
2755 if (*p == NULL)
2756 return -1;
2757
2758 data_offer_receive_data(input->selection_offer,
2759 mime_type, func, data);
2760 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002761}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002762
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002763int
2764input_receive_selection_data_to_fd(struct input *input,
2765 const char *mime_type, int fd)
2766{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002767 if (input->selection_offer)
2768 wl_data_offer_receive(input->selection_offer->offer,
2769 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002770
2771 return 0;
2772}
2773
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002774void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002775window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002776{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002777 if (!window->shell_surface)
2778 return;
2779
Daniel Stone37816df2012-05-16 18:45:18 +01002780 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002781}
2782
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002783static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002784idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002785{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002786 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002787
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002788 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002789 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002790 widget_set_allocation(widget,
2791 window->pending_allocation.x,
2792 window->pending_allocation.y,
2793 window->pending_allocation.width,
2794 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002795
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002796 if (window->input_region) {
2797 wl_region_destroy(window->input_region);
2798 window->input_region = NULL;
2799 }
2800
2801 if (window->opaque_region) {
2802 wl_region_destroy(window->opaque_region);
2803 window->opaque_region = NULL;
2804 }
2805
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002806 if (widget->resize_handler)
2807 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002808 widget->allocation.width,
2809 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002810 widget->user_data);
2811
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002812 if (window->allocation.width != widget->allocation.width ||
2813 window->allocation.height != widget->allocation.height) {
2814 window->allocation = widget->allocation;
2815 window_schedule_redraw(window);
2816 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002817}
2818
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002819void
2820window_schedule_resize(struct window *window, int width, int height)
2821{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002822 window->pending_allocation.x = 0;
2823 window->pending_allocation.y = 0;
2824 window->pending_allocation.width = width;
2825 window->pending_allocation.height = height;
2826
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002827 if (window->min_allocation.width == 0)
2828 window->min_allocation = window->pending_allocation;
2829 if (window->pending_allocation.width < window->min_allocation.width)
2830 window->pending_allocation.width = window->min_allocation.width;
2831 if (window->pending_allocation.height < window->min_allocation.height)
2832 window->pending_allocation.height = window->min_allocation.height;
2833
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002834 window->resize_needed = 1;
2835 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002836}
2837
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002838void
2839widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2840{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002841 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002842}
2843
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002844static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002845handle_ping(void *data, struct wl_shell_surface *shell_surface,
2846 uint32_t serial)
2847{
2848 wl_shell_surface_pong(shell_surface, serial);
2849}
2850
2851static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002852handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002853 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002854{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002855 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002856
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002857 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002858 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002859}
2860
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002861static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002862menu_destroy(struct menu *menu)
2863{
2864 widget_destroy(menu->widget);
2865 window_destroy(menu->window);
2866 free(menu);
2867}
2868
2869static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002870handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2871{
2872 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002873 struct menu *menu = window->widget->user_data;
2874
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002875 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002876 * device. Or just use wl_callback. And this really needs to
2877 * be a window vfunc that the menu can set. And we need the
2878 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002879
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002880 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002881 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002882 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002883}
2884
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002885static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002886 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002887 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002888 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002889};
2890
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002891void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002892window_get_allocation(struct window *window,
2893 struct rectangle *allocation)
2894{
2895 *allocation = window->allocation;
2896}
2897
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002898static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002899widget_redraw(struct widget *widget)
2900{
2901 struct widget *child;
2902
2903 if (widget->redraw_handler)
2904 widget->redraw_handler(widget, widget->user_data);
2905 wl_list_for_each(child, &widget->child_list, link)
2906 widget_redraw(child);
2907}
2908
2909static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002910frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2911{
2912 struct window *window = data;
2913
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002914 assert(callback == window->frame_cb);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002915 wl_callback_destroy(callback);
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002916 window->frame_cb = 0;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002917 window->redraw_scheduled = 0;
2918 if (window->redraw_needed)
2919 window_schedule_redraw(window);
2920}
2921
2922static const struct wl_callback_listener listener = {
2923 frame_callback
2924};
2925
2926static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002927idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002928{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002929 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002930
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002931 if (window->resize_needed)
2932 idle_resize(window);
2933
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002934 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002935 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002936 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002937 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002938 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002939
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002940 window->frame_cb = wl_surface_frame(window->surface);
2941 wl_callback_add_listener(window->frame_cb, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002942}
2943
2944void
2945window_schedule_redraw(struct window *window)
2946{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002947 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002948 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002949 window->redraw_task.run = idle_redraw;
2950 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002951 window->redraw_scheduled = 1;
2952 }
2953}
2954
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002955void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002956window_set_fullscreen(struct window *window, int fullscreen)
2957{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002958 if (!window->display->shell)
2959 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002960
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002961 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002962 return;
2963
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002964 if (fullscreen) {
2965 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002966 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002967 wl_shell_surface_set_fullscreen(window->shell_surface,
2968 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2969 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002970 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002971 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002972 wl_shell_surface_set_toplevel(window->shell_surface);
2973 window_schedule_resize(window,
2974 window->saved_allocation.width,
2975 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002976 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002977}
2978
2979void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002980window_set_maximized(struct window *window, int maximized)
2981{
2982 if (!window->display->shell)
2983 return;
2984
2985 if ((window->type == TYPE_MAXIMIZED) == maximized)
2986 return;
2987
2988 if (window->type == TYPE_TOPLEVEL) {
2989 window->saved_allocation = window->allocation;
2990 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2991 window->type = TYPE_MAXIMIZED;
2992 } else {
2993 wl_shell_surface_set_toplevel(window->shell_surface);
2994 window->type = TYPE_TOPLEVEL;
2995 window_schedule_resize(window,
2996 window->saved_allocation.width,
2997 window->saved_allocation.height);
2998 }
2999}
3000
3001void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003002window_set_user_data(struct window *window, void *data)
3003{
3004 window->user_data = data;
3005}
3006
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003007void *
3008window_get_user_data(struct window *window)
3009{
3010 return window->user_data;
3011}
3012
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003013void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003014window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003015 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003016{
3017 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003018}
3019
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003020void
3021window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04003022 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003023{
3024 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05003025}
3026
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003027void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003028window_set_data_handler(struct window *window, window_data_handler_t handler)
3029{
3030 window->data_handler = handler;
3031}
3032
3033void
3034window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3035{
3036 window->drop_handler = handler;
3037}
3038
3039void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003040window_set_close_handler(struct window *window,
3041 window_close_handler_t handler)
3042{
3043 window->close_handler = handler;
3044}
3045
3046void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003047window_set_fullscreen_handler(struct window *window,
3048 window_fullscreen_handler_t handler)
3049{
3050 window->fullscreen_handler = handler;
3051}
3052
3053void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003054window_set_title(struct window *window, const char *title)
3055{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05003056 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003057 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04003058 if (window->shell_surface)
3059 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13003060}
3061
3062const char *
3063window_get_title(struct window *window)
3064{
3065 return window->title;
3066}
3067
3068void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003069window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3070{
3071 struct text_cursor_position *text_cursor_position =
3072 window->display->text_cursor_position;
3073
Scott Moreau9295ce02012-06-01 12:46:10 -06003074 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003075 return;
3076
3077 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06003078 window->surface,
3079 wl_fixed_from_int(x),
3080 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003081}
3082
3083void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003084window_damage(struct window *window, int32_t x, int32_t y,
3085 int32_t width, int32_t height)
3086{
3087 wl_surface_damage(window->surface, x, y, width, height);
3088}
3089
Casey Dahlin9074db52012-04-19 22:50:09 -04003090static void
3091surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003092 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003093{
Rob Bradford7507b572012-05-15 17:55:34 +01003094 struct window *window = data;
3095 struct output *output;
3096 struct output *output_found = NULL;
3097 struct window_output *window_output;
3098
3099 wl_list_for_each(output, &window->display->output_list, link) {
3100 if (output->output == wl_output) {
3101 output_found = output;
3102 break;
3103 }
3104 }
3105
3106 if (!output_found)
3107 return;
3108
3109 window_output = malloc (sizeof *window_output);
3110 window_output->output = output_found;
3111
3112 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003113}
3114
3115static void
3116surface_leave(void *data,
3117 struct wl_surface *wl_surface, struct wl_output *output)
3118{
Rob Bradford7507b572012-05-15 17:55:34 +01003119 struct window *window = data;
3120 struct window_output *window_output;
3121 struct window_output *window_output_found = NULL;
3122
3123 wl_list_for_each(window_output, &window->window_output_list, link) {
3124 if (window_output->output->output == output) {
3125 window_output_found = window_output;
3126 break;
3127 }
3128 }
3129
3130 if (window_output_found) {
3131 wl_list_remove(&window_output_found->link);
3132 free(window_output_found);
3133 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003134}
3135
3136static const struct wl_surface_listener surface_listener = {
3137 surface_enter,
3138 surface_leave
3139};
3140
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003141static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003142window_create_internal(struct display *display,
3143 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003144{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003145 struct window *window;
3146
3147 window = malloc(sizeof *window);
3148 if (window == NULL)
3149 return NULL;
3150
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003151 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003152 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003153 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003154 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003155 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003156 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003157 window->shell_surface =
3158 wl_shell_get_shell_surface(display->shell,
3159 window->surface);
3160 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003161 window->allocation.x = 0;
3162 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003163 window->allocation.width = 0;
3164 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003165 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003166 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003167 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003168 window->input_region = NULL;
3169 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003170
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003171 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003172#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003173 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003174#else
3175 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3176#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003177 else
3178 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003179
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003180 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003181 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003182 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003183
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003184 if (window->shell_surface) {
3185 wl_shell_surface_set_user_data(window->shell_surface, window);
3186 wl_shell_surface_add_listener(window->shell_surface,
3187 &shell_surface_listener, window);
3188 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003189
Rob Bradford7507b572012-05-15 17:55:34 +01003190 wl_list_init (&window->window_output_list);
3191
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003192 return window;
3193}
3194
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003195struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003196window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003197{
3198 struct window *window;
3199
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003200 window = window_create_internal(display, NULL, TYPE_NONE);
3201 if (!window)
3202 return NULL;
3203
3204 return window;
3205}
3206
3207struct window *
3208window_create_custom(struct display *display)
3209{
3210 struct window *window;
3211
3212 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003213 if (!window)
3214 return NULL;
3215
3216 return window;
3217}
3218
3219struct window *
3220window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003221 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003222{
3223 struct window *window;
3224
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003225 window = window_create_internal(parent->display,
3226 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003227 if (!window)
3228 return NULL;
3229
3230 window->x = x;
3231 window->y = y;
3232
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003233 if (display->shell)
3234 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003235 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003236 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003237
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003238 return window;
3239}
3240
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003241static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003242menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003243{
3244 int next;
3245
3246 next = (sy - 8) / 20;
3247 if (menu->current != next) {
3248 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003249 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003250 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003251}
3252
3253static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003254menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003255 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003256 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003257{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003258 struct menu *menu = data;
3259
3260 if (widget == menu->widget)
3261 menu_set_item(data, y);
3262
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003263 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003264}
3265
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003266static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003267menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003268 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003269{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003270 struct menu *menu = data;
3271
3272 if (widget == menu->widget)
3273 menu_set_item(data, y);
3274
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003275 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003276}
3277
3278static void
3279menu_leave_handler(struct widget *widget, struct input *input, void *data)
3280{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003281 struct menu *menu = data;
3282
3283 if (widget == menu->widget)
3284 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003285}
3286
3287static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003288menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003289 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003290 uint32_t button, enum wl_pointer_button_state state,
3291 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003292
3293{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003294 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003295
Daniel Stone4dbadb12012-05-30 16:31:51 +01003296 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3297 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003298 /* Either relase after press-drag-release or
3299 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003300 menu->func(menu->window->parent,
3301 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003302 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003303 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003304 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003305}
3306
3307static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003308menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003309{
3310 cairo_t *cr;
3311 const int32_t r = 3, margin = 3;
3312 struct menu *menu = data;
3313 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003314 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003316 cr = cairo_create(window->cairo_surface);
3317 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3318 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3319 cairo_paint(cr);
3320
3321 width = window->allocation.width;
3322 height = window->allocation.height;
3323 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003324
3325 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003326 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3327 cairo_fill(cr);
3328
3329 for (i = 0; i < menu->count; i++) {
3330 if (i == menu->current) {
3331 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3332 cairo_rectangle(cr, margin, i * 20 + margin,
3333 width - 2 * margin, 20);
3334 cairo_fill(cr);
3335 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3336 cairo_move_to(cr, 10, i * 20 + 16);
3337 cairo_show_text(cr, menu->entries[i]);
3338 } else {
3339 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3340 cairo_move_to(cr, 10, i * 20 + 16);
3341 cairo_show_text(cr, menu->entries[i]);
3342 }
3343 }
3344
3345 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003346}
3347
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003348void
3349window_show_menu(struct display *display,
3350 struct input *input, uint32_t time, struct window *parent,
3351 int32_t x, int32_t y,
3352 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003353{
3354 struct window *window;
3355 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003356 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003357
3358 menu = malloc(sizeof *menu);
3359 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003360 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003361
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003362 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003363 if (!window) {
3364 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003365 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003366 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003367
3368 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003369 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003370 menu->entries = entries;
3371 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003372 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003373 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003374 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003375 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003376 window->type = TYPE_MENU;
3377 window->x = x;
3378 window->y = y;
3379
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003380 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003381 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003382 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003383 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003384 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003385
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003386 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003387 widget_set_enter_handler(menu->widget, menu_enter_handler);
3388 widget_set_leave_handler(menu->widget, menu_leave_handler);
3389 widget_set_motion_handler(menu->widget, menu_motion_handler);
3390 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003391
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003392 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003393 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003394}
3395
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003396void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003397window_set_buffer_type(struct window *window, enum window_buffer_type type)
3398{
3399 window->buffer_type = type;
3400}
3401
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003402
3403static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003404display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003405 struct wl_output *wl_output,
3406 int x, int y,
3407 int physical_width,
3408 int physical_height,
3409 int subpixel,
3410 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003411 const char *model,
3412 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003413{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003414 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003415
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003416 output->allocation.x = x;
3417 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003418}
3419
3420static void
3421display_handle_mode(void *data,
3422 struct wl_output *wl_output,
3423 uint32_t flags,
3424 int width,
3425 int height,
3426 int refresh)
3427{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003428 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003429 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003430
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003431 if (flags & WL_OUTPUT_MODE_CURRENT) {
3432 output->allocation.width = width;
3433 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003434 if (display->output_configure_handler)
3435 (*display->output_configure_handler)(
3436 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003437 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003438}
3439
3440static const struct wl_output_listener output_listener = {
3441 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003442 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003443};
3444
3445static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003446display_add_output(struct display *d, uint32_t id)
3447{
3448 struct output *output;
3449
3450 output = malloc(sizeof *output);
3451 if (output == NULL)
3452 return;
3453
3454 memset(output, 0, sizeof *output);
3455 output->display = d;
3456 output->output =
3457 wl_display_bind(d->display, id, &wl_output_interface);
3458 wl_list_insert(d->output_list.prev, &output->link);
3459
3460 wl_output_add_listener(output->output, &output_listener, output);
3461}
3462
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003463static void
3464output_destroy(struct output *output)
3465{
3466 if (output->destroy_handler)
3467 (*output->destroy_handler)(output, output->user_data);
3468
3469 wl_output_destroy(output->output);
3470 wl_list_remove(&output->link);
3471 free(output);
3472}
3473
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003474void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003475display_set_output_configure_handler(struct display *display,
3476 display_output_handler_t handler)
3477{
3478 struct output *output;
3479
3480 display->output_configure_handler = handler;
3481 if (!handler)
3482 return;
3483
3484 wl_list_for_each(output, &display->output_list, link)
3485 (*display->output_configure_handler)(output,
3486 display->user_data);
3487}
3488
3489void
3490output_set_user_data(struct output *output, void *data)
3491{
3492 output->user_data = data;
3493}
3494
3495void *
3496output_get_user_data(struct output *output)
3497{
3498 return output->user_data;
3499}
3500
3501void
3502output_set_destroy_handler(struct output *output,
3503 display_output_handler_t handler)
3504{
3505 output->destroy_handler = handler;
3506 /* FIXME: implement this, once we have way to remove outputs */
3507}
3508
3509void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003510output_get_allocation(struct output *output, struct rectangle *allocation)
3511{
3512 *allocation = output->allocation;
3513}
3514
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003515struct wl_output *
3516output_get_wl_output(struct output *output)
3517{
3518 return output->output;
3519}
3520
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003521static void
Daniel Stone97f68542012-05-30 16:32:01 +01003522fini_xkb(struct input *input)
3523{
3524 xkb_state_unref(input->xkb.state);
3525 xkb_map_unref(input->xkb.keymap);
3526}
3527
3528static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003529display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003530{
3531 struct input *input;
3532
3533 input = malloc(sizeof *input);
3534 if (input == NULL)
3535 return;
3536
3537 memset(input, 0, sizeof *input);
3538 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003539 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003540 input->pointer_focus = NULL;
3541 input->keyboard_focus = NULL;
3542 wl_list_insert(d->input_list.prev, &input->link);
3543
Daniel Stone37816df2012-05-16 18:45:18 +01003544 wl_seat_add_listener(input->seat, &seat_listener, input);
3545 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003546
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547 input->data_device =
3548 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003549 input->seat);
3550 wl_data_device_add_listener(input->data_device, &data_device_listener,
3551 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003552
3553 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003554
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003555 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3556 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003557 input->repeat_task.run = keyboard_repeat_func;
3558 display_watch_fd(d, input->repeat_timer_fd,
3559 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003560}
3561
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003562static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003563input_destroy(struct input *input)
3564{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003565 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003566 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003567
3568 if (input->drag_offer)
3569 data_offer_destroy(input->drag_offer);
3570
3571 if (input->selection_offer)
3572 data_offer_destroy(input->selection_offer);
3573
3574 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003575 fini_xkb(input);
3576
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003577 wl_surface_destroy(input->pointer_surface);
3578
Pekka Paalanene1207c72011-12-16 12:02:09 +02003579 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003580 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003581 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003582 free(input);
3583}
3584
3585static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003586init_workspace_manager(struct display *d, uint32_t id)
3587{
3588 d->workspace_manager =
3589 wl_display_bind(d->display, id, &workspace_manager_interface);
3590 if (d->workspace_manager != NULL)
3591 workspace_manager_add_listener(d->workspace_manager,
3592 &workspace_manager_listener,
3593 d);
3594}
3595
3596static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003597display_handle_global(struct wl_display *display, uint32_t id,
3598 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003599{
3600 struct display *d = data;
3601
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003602 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003603 d->compositor =
3604 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003605 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003606 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003607 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003608 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003609 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003610 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003611 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003612 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003613 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3614 d->data_device_manager =
3615 wl_display_bind(display, id,
3616 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003617 } else if (strcmp(interface, "text_cursor_position") == 0) {
3618 d->text_cursor_position =
3619 wl_display_bind(display, id,
3620 &text_cursor_position_interface);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02003621 } else if (strcmp(interface, "workspace_manager") == 0) {
3622 init_workspace_manager(d, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003623 }
3624}
3625
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003626#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003627static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003628init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003629{
3630 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003631 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003632
Rob Clark6396ed32012-03-11 19:48:41 -05003633#ifdef USE_CAIRO_GLESV2
3634# define GL_BIT EGL_OPENGL_ES2_BIT
3635#else
3636# define GL_BIT EGL_OPENGL_BIT
3637#endif
3638
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003639 static const EGLint argb_cfg_attribs[] = {
3640 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003641 EGL_RED_SIZE, 1,
3642 EGL_GREEN_SIZE, 1,
3643 EGL_BLUE_SIZE, 1,
3644 EGL_ALPHA_SIZE, 1,
3645 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003646 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003647 EGL_NONE
3648 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003649
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003650#ifdef USE_CAIRO_GLESV2
3651 static const EGLint context_attribs[] = {
3652 EGL_CONTEXT_CLIENT_VERSION, 2,
3653 EGL_NONE
3654 };
3655 EGLint api = EGL_OPENGL_ES_API;
3656#else
3657 EGLint *context_attribs = NULL;
3658 EGLint api = EGL_OPENGL_API;
3659#endif
3660
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003661 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003662 if (!eglInitialize(d->dpy, &major, &minor)) {
3663 fprintf(stderr, "failed to initialize display\n");
3664 return -1;
3665 }
3666
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003667 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003668 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3669 return -1;
3670 }
3671
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003672 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3673 &d->argb_config, 1, &n) || n != 1) {
3674 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003675 return -1;
3676 }
3677
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003678 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003679 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003680 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003681 fprintf(stderr, "failed to create context\n");
3682 return -1;
3683 }
3684
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003685 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003686 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003687 return -1;
3688 }
3689
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003690#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003691 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3692 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3693 fprintf(stderr, "failed to get cairo egl argb device\n");
3694 return -1;
3695 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003696#endif
3697
3698 return 0;
3699}
3700
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003701static void
3702fini_egl(struct display *display)
3703{
3704#ifdef HAVE_CAIRO_EGL
3705 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003706#endif
3707
3708 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3709 EGL_NO_CONTEXT);
3710
3711 eglTerminate(display->dpy);
3712 eglReleaseThread();
3713}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003714#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003715
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003716static int
3717event_mask_update(uint32_t mask, void *data)
3718{
3719 struct display *d = data;
3720
3721 d->mask = mask;
3722
3723 return 0;
3724}
3725
3726static void
3727handle_display_data(struct task *task, uint32_t events)
3728{
3729 struct display *display =
3730 container_of(task, struct display, display_task);
U. Artie Eoff44874d92012-10-02 21:12:35 -07003731
3732 display->display_fd_events = events;
3733
3734 if (events & EPOLLERR || events & EPOLLHUP) {
3735 display_exit(display);
3736 return;
3737 }
3738
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003739 wl_display_iterate(display->display, display->mask);
3740}
3741
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003742struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003743display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003744{
3745 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003746
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003747 d = malloc(sizeof *d);
3748 if (d == NULL)
3749 return NULL;
3750
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003751 memset(d, 0, sizeof *d);
3752
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003753 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003754 if (d->display == NULL) {
3755 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003756 return NULL;
3757 }
3758
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003759 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003760 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3761 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07003762 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3763 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003764
3765 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003766 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003767 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003768
Daniel Stone97f68542012-05-30 16:32:01 +01003769 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003770 if (d->xkb_context == NULL) {
3771 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003772 return NULL;
3773 }
3774
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02003775 d->workspace = 0;
3776 d->workspace_count = 1;
3777
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003778 /* Set up listener so we'll catch all events. */
3779 wl_display_add_global_listener(d->display,
3780 display_handle_global, d);
3781
3782 /* Process connection events. */
3783 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003784#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003785 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003786 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003787#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003788
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003789 d->image_target_texture_2d =
3790 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3791 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3792 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3793
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003794 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003795
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003796 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003797
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003798 wl_list_init(&d->window_list);
3799
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003800 return d;
3801}
3802
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003803static void
3804display_destroy_outputs(struct display *display)
3805{
3806 struct output *tmp;
3807 struct output *output;
3808
3809 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3810 output_destroy(output);
3811}
3812
Pekka Paalanene1207c72011-12-16 12:02:09 +02003813static void
3814display_destroy_inputs(struct display *display)
3815{
3816 struct input *tmp;
3817 struct input *input;
3818
3819 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3820 input_destroy(input);
3821}
3822
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003823void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003824display_destroy(struct display *display)
3825{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003826 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07003827 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3828 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02003829
3830 if (!wl_list_empty(&display->deferred_list))
3831 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3832
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003833 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003834 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003835
Daniel Stone97f68542012-05-30 16:32:01 +01003836 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003837
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003838 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003839 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003840
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003841#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003842 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003843#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003844
Pekka Paalanenc2052982011-12-16 11:41:32 +02003845 if (display->shell)
3846 wl_shell_destroy(display->shell);
3847
3848 if (display->shm)
3849 wl_shm_destroy(display->shm);
3850
3851 if (display->data_device_manager)
3852 wl_data_device_manager_destroy(display->data_device_manager);
3853
3854 wl_compositor_destroy(display->compositor);
3855
3856 close(display->epoll_fd);
3857
U. Artie Eoff44874d92012-10-02 21:12:35 -07003858 if (!(display->display_fd_events & EPOLLERR) &&
3859 !(display->display_fd_events & EPOLLHUP))
3860 wl_display_flush(display->display);
3861
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003862 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003863 free(display);
3864}
3865
3866void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003867display_set_user_data(struct display *display, void *data)
3868{
3869 display->user_data = data;
3870}
3871
3872void *
3873display_get_user_data(struct display *display)
3874{
3875 return display->user_data;
3876}
3877
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003878struct wl_display *
3879display_get_display(struct display *display)
3880{
3881 return display->display;
3882}
3883
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003884struct output *
3885display_get_output(struct display *display)
3886{
3887 return container_of(display->output_list.next, struct output, link);
3888}
3889
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003890struct wl_compositor *
3891display_get_compositor(struct display *display)
3892{
3893 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003894}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003895
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003896uint32_t
3897display_get_serial(struct display *display)
3898{
3899 return display->serial;
3900}
3901
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003902EGLDisplay
3903display_get_egl_display(struct display *d)
3904{
3905 return d->dpy;
3906}
3907
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003908struct wl_data_source *
3909display_create_data_source(struct display *display)
3910{
3911 return wl_data_device_manager_create_data_source(display->data_device_manager);
3912}
3913
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003914EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003915display_get_argb_egl_config(struct display *d)
3916{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003917 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003918}
3919
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003920struct wl_shell *
3921display_get_shell(struct display *display)
3922{
3923 return display->shell;
3924}
3925
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003926int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003927display_acquire_window_surface(struct display *display,
3928 struct window *window,
3929 EGLContext ctx)
3930{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003931#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003932 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003933 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003934
3935 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003936 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003937 device = cairo_surface_get_device(window->cairo_surface);
3938 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003939 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003940
Benjamin Franzke0c991632011-09-27 21:57:31 +02003941 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003942 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003943 ctx = display->argb_ctx;
3944 else
3945 assert(0);
3946 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003947
3948 data = cairo_surface_get_user_data(window->cairo_surface,
3949 &surface_data_key);
3950
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003951 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003952 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003953 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3954 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003955
3956 return 0;
3957#else
3958 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003959#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003960}
3961
3962void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003963display_release_window_surface(struct display *display,
3964 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003965{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003966#ifdef HAVE_CAIRO_EGL
3967 cairo_device_t *device;
3968
3969 device = cairo_surface_get_device(window->cairo_surface);
3970 if (!device)
3971 return;
3972
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003973 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003974 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003975 cairo_device_release(device);
3976#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003977}
3978
3979void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003980display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003981{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003982 wl_list_insert(&display->deferred_list, &task->link);
3983}
3984
3985void
3986display_watch_fd(struct display *display,
3987 int fd, uint32_t events, struct task *task)
3988{
3989 struct epoll_event ep;
3990
3991 ep.events = events;
3992 ep.data.ptr = task;
3993 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3994}
3995
3996void
3997display_run(struct display *display)
3998{
3999 struct task *task;
4000 struct epoll_event ep[16];
4001 int i, count;
4002
Pekka Paalanen826d7952011-12-15 10:14:07 +02004003 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004004 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004005 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004006
4007 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03004008 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004009 struct task, link);
4010 wl_list_remove(&task->link);
4011 task->run(task, 0);
4012 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05004013
4014 if (!display->running)
4015 break;
4016
4017 wl_display_flush(display->display);
4018
4019 count = epoll_wait(display->epoll_fd,
4020 ep, ARRAY_LENGTH(ep), -1);
4021 for (i = 0; i < count; i++) {
4022 task = ep[i].data.ptr;
4023 task->run(task, ep[i].events);
4024 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004025 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004026}
Pekka Paalanen826d7952011-12-15 10:14:07 +02004027
4028void
4029display_exit(struct display *display)
4030{
4031 display->running = 0;
4032}