blob: 0bd362fc4c315b58b326ae5e7f7f1b206387f309 [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"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400138 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500139 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500140 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400141 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400142 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400143 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400144 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400145 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400147 int transparent;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400148 int focus_count;
149
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400150 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500151
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400152 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500153
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700154 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400155
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500156 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500157 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400158 window_data_handler_t data_handler;
159 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500160 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400161 window_fullscreen_handler_t fullscreen_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400162
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200163 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500164 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500165
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500166 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400167 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500168};
169
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500170struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500171 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300172 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500173 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400174 struct wl_list link;
175 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500176 widget_resize_handler_t resize_handler;
177 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500178 widget_enter_handler_t enter_handler;
179 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500180 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500181 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500183 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300184 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400185};
186
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187struct input {
188 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100189 struct wl_seat *seat;
190 struct wl_pointer *pointer;
191 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192 struct window *pointer_focus;
193 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300194 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300195 uint32_t cursor_anim_start;
196 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300197 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400198 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400199 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400200 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400201 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400202 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400203
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500204 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500205 struct widget *grab;
206 uint32_t grab_button;
207
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400208 struct wl_data_device *data_device;
209 struct data_offer *drag_offer;
210 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100211
212 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100213 struct xkb_keymap *keymap;
214 struct xkb_state *state;
215 xkb_mod_mask_t control_mask;
216 xkb_mod_mask_t alt_mask;
217 xkb_mod_mask_t shift_mask;
218 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400219
220 struct task repeat_task;
221 int repeat_timer_fd;
222 uint32_t repeat_sym;
223 uint32_t repeat_key;
224 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400225};
226
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500227struct output {
228 struct display *display;
229 struct wl_output *output;
230 struct rectangle allocation;
231 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200232
233 display_output_handler_t destroy_handler;
234 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500235};
236
Martin Minarik1998b152012-05-10 02:04:35 +0200237enum frame_button_action {
238 FRAME_BUTTON_NULL = 0,
239 FRAME_BUTTON_ICON = 1,
240 FRAME_BUTTON_CLOSE = 2,
241 FRAME_BUTTON_MINIMIZE = 3,
242 FRAME_BUTTON_MAXIMIZE = 4,
243};
244
245enum frame_button_pointer {
246 FRAME_BUTTON_DEFAULT = 0,
247 FRAME_BUTTON_OVER = 1,
248 FRAME_BUTTON_ACTIVE = 2,
249};
250
251enum frame_button_align {
252 FRAME_BUTTON_RIGHT = 0,
253 FRAME_BUTTON_LEFT = 1,
254};
255
256enum frame_button_decoration {
257 FRAME_BUTTON_NONE = 0,
258 FRAME_BUTTON_FANCY = 1,
259};
260
261struct frame_button {
262 struct widget *widget;
263 struct frame *frame;
264 cairo_surface_t *icon;
265 enum frame_button_action type;
266 enum frame_button_pointer state;
267 struct wl_list link; /* buttons_list */
268 enum frame_button_align align;
269 enum frame_button_decoration decoration;
270};
271
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500272struct frame {
273 struct widget *widget;
274 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200275 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500276};
277
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500278struct menu {
279 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500280 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500281 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500282 const char **entries;
283 uint32_t time;
284 int current;
285 int count;
286 menu_func_t func;
287};
288
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300289struct tooltip {
290 struct widget *parent;
291 struct window *window;
292 struct widget *widget;
293 char *entry;
294 struct task tooltip_task;
295 int tooltip_fd;
296 float x, y;
297};
298
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700299struct shm_pool {
300 struct wl_shm_pool *pool;
301 size_t size;
302 size_t used;
303 void *data;
304};
305
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400306enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300307 CURSOR_DEFAULT = 100,
308 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400309};
310
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500311enum window_location {
312 WINDOW_INTERIOR = 0,
313 WINDOW_RESIZING_TOP = 1,
314 WINDOW_RESIZING_BOTTOM = 2,
315 WINDOW_RESIZING_LEFT = 4,
316 WINDOW_RESIZING_TOP_LEFT = 5,
317 WINDOW_RESIZING_BOTTOM_LEFT = 6,
318 WINDOW_RESIZING_RIGHT = 8,
319 WINDOW_RESIZING_TOP_RIGHT = 9,
320 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
321 WINDOW_RESIZING_MASK = 15,
322 WINDOW_EXTERIOR = 16,
323 WINDOW_TITLEBAR = 17,
324 WINDOW_CLIENT_AREA = 18,
325};
326
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327static const cairo_user_data_key_t surface_data_key;
328struct surface_data {
329 struct wl_buffer *buffer;
330};
331
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500332#define MULT(_d,c,a,t) \
333 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
334
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500335#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100337struct egl_window_surface_data {
338 struct display *display;
339 struct wl_surface *surface;
340 struct wl_egl_window *window;
341 EGLSurface surf;
342};
343
344static void
345egl_window_surface_data_destroy(void *p)
346{
347 struct egl_window_surface_data *data = p;
348 struct display *d = data->display;
349
350 eglDestroySurface(d->dpy, data->surf);
351 wl_egl_window_destroy(data->window);
352 data->surface = NULL;
353
354 free(p);
355}
356
357static cairo_surface_t *
358display_create_egl_window_surface(struct display *display,
359 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400360 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100361 struct rectangle *rectangle)
362{
363 cairo_surface_t *cairo_surface;
364 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200366 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100367
368 data = malloc(sizeof *data);
369 if (data == NULL)
370 return NULL;
371
372 data->display = display;
373 data->surface = surface;
374
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500375 config = display->argb_config;
376 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400378 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100379 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400380 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500383 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100384
Benjamin Franzke0c991632011-09-27 21:57:31 +0200385 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100386 data->surf,
387 rectangle->width,
388 rectangle->height);
389
390 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
391 data, egl_window_surface_data_destroy);
392
393 return cairo_surface;
394}
395
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396#endif
397
398struct wl_buffer *
399display_get_buffer_for_surface(struct display *display,
400 cairo_surface_t *surface)
401{
402 struct surface_data *data;
403
404 data = cairo_surface_get_user_data (surface, &surface_data_key);
405
406 return data->buffer;
407}
408
409struct shm_surface_data {
410 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700411 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412};
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415shm_pool_destroy(struct shm_pool *pool);
416
417static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418shm_surface_data_destroy(void *p)
419{
420 struct shm_surface_data *data = p;
421
422 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423 if (data->pool)
424 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300425
426 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427}
428
Kristian Høgsberg16626282012-04-03 11:21:27 -0400429static struct wl_shm_pool *
430make_shm_pool(struct display *display, int size, void **data)
431{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400432 struct wl_shm_pool *pool;
433 int fd;
434
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300435 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400436 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300437 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
438 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400443 if (*data == MAP_FAILED) {
444 fprintf(stderr, "mmap failed: %m\n");
445 close(fd);
446 return NULL;
447 }
448
449 pool = wl_shm_create_pool(display->shm, fd, size);
450
451 close(fd);
452
453 return pool;
454}
455
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700456static struct shm_pool *
457shm_pool_create(struct display *display, size_t size)
458{
459 struct shm_pool *pool = malloc(sizeof *pool);
460
461 if (!pool)
462 return NULL;
463
464 pool->pool = make_shm_pool(display, size, &pool->data);
465 if (!pool->pool) {
466 free(pool);
467 return NULL;
468 }
469
470 pool->size = size;
471 pool->used = 0;
472
473 return pool;
474}
475
476static void *
477shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
478{
479 if (pool->used + size > pool->size)
480 return NULL;
481
482 *offset = pool->used;
483 pool->used += size;
484
485 return (char *) pool->data + *offset;
486}
487
488/* destroy the pool. this does not unmap the memory though */
489static void
490shm_pool_destroy(struct shm_pool *pool)
491{
492 munmap(pool->data, pool->size);
493 wl_shm_pool_destroy(pool->pool);
494 free(pool);
495}
496
497/* Start allocating from the beginning of the pool again */
498static void
499shm_pool_reset(struct shm_pool *pool)
500{
501 pool->used = 0;
502}
503
504static int
505data_length_for_shm_surface(struct rectangle *rect)
506{
507 int stride;
508
509 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
510 rect->width);
511 return stride * rect->height;
512}
513
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500514static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700515display_create_shm_surface_from_pool(struct display *display,
516 struct rectangle *rectangle,
517 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518{
519 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400520 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400521 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700522 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400523 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524
525 data = malloc(sizeof *data);
526 if (data == NULL)
527 return NULL;
528
529 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700531 length = stride * rectangle->height;
532 data->pool = NULL;
533 map = shm_pool_allocate(pool, length, &offset);
534
535 if (!map) {
536 free(data);
537 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400538 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541 CAIRO_FORMAT_ARGB32,
542 rectangle->width,
543 rectangle->height,
544 stride);
545
546 cairo_surface_set_user_data (surface, &surface_data_key,
547 data, shm_surface_data_destroy);
548
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500550 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700554 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400555 rectangle->width,
556 rectangle->height,
557 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700559 return surface;
560}
561
562static cairo_surface_t *
563display_create_shm_surface(struct display *display,
564 struct rectangle *rectangle, uint32_t flags,
565 struct window *window)
566{
567 struct shm_surface_data *data;
568 struct shm_pool *pool;
569 cairo_surface_t *surface;
570
571 if (window && window->pool) {
572 shm_pool_reset(window->pool);
573 surface = display_create_shm_surface_from_pool(display,
574 rectangle,
575 flags,
576 window->pool);
577 if (surface)
578 return surface;
579 }
580
581 pool = shm_pool_create(display,
582 data_length_for_shm_surface(rectangle));
583 if (!pool)
584 return NULL;
585
586 surface =
587 display_create_shm_surface_from_pool(display, rectangle,
588 flags, pool);
589
590 if (!surface) {
591 shm_pool_destroy(pool);
592 return NULL;
593 }
594
595 /* make sure we destroy the pool when the surface is destroyed */
596 data = cairo_surface_get_user_data(surface, &surface_data_key);
597 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598
599 return surface;
600}
601
nobled7b87cb02011-02-01 18:51:47 +0000602static int
603check_size(struct rectangle *rect)
604{
605 if (rect->width && rect->height)
606 return 0;
607
608 fprintf(stderr, "tried to create surface of "
609 "width: %d, height: %d\n", rect->width, rect->height);
610 return -1;
611}
612
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613cairo_surface_t *
614display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100615 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400616 struct rectangle *rectangle,
617 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618{
nobled7b87cb02011-02-01 18:51:47 +0000619 if (check_size(rectangle) < 0)
620 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500621#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300622 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400623 return display_create_egl_window_surface(display,
624 surface,
625 flags,
626 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400627#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400628 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629}
630
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300631static const char *cursors[] = {
632 "bottom_left_corner",
633 "bottom_right_corner",
634 "bottom_side",
635 "grabbing",
636 "left_ptr",
637 "left_side",
638 "right_side",
639 "top_left_corner",
640 "top_right_corner",
641 "top_side",
642 "xterm",
643 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400644 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300645};
646
647static void
648create_cursors(struct display *display)
649{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100650 char *config_file;
651 char *theme = NULL;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300652 unsigned int i;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100653 struct config_key shell_keys[] = {
654 { "cursor-theme", CONFIG_KEY_STRING, &theme },
655 };
656 struct config_section cs[] = {
657 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
658 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300659
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100660 config_file = config_file_path("weston.ini");
661 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
662 free(config_file);
663
664 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300665 display->cursors =
666 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
667
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300668 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300669 display->cursors[i] =
670 wl_cursor_theme_get_cursor(display->cursor_theme,
671 cursors[i]);
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300672 if (!display->cursors[i])
673 fprintf(stderr, "could not load cursor '%s'\n",
674 cursors[i]);
675 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676}
677
678static void
679destroy_cursors(struct display *display)
680{
681 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800682 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300683}
684
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300685struct wl_cursor_image *
686display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400687{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300688 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300689 wl_cursor_theme_get_cursor(display->cursor_theme,
690 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300692 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693}
694
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400695static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200696window_get_resize_dx_dy(struct window *window, int *x, int *y)
697{
698 if (window->resize_edges & WINDOW_RESIZING_LEFT)
699 *x = window->server_allocation.width - window->allocation.width;
700 else
701 *x = 0;
702
703 if (window->resize_edges & WINDOW_RESIZING_TOP)
704 *y = window->server_allocation.height -
705 window->allocation.height;
706 else
707 *y = 0;
708
709 window->resize_edges = 0;
710}
711
712static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500713window_attach_surface(struct window *window)
714{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400715 struct display *display = window->display;
716 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000717#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100718 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000719#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500720 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400722 if (window->type == TYPE_NONE) {
723 window->type = TYPE_TOPLEVEL;
724 if (display->shell)
725 wl_shell_surface_set_toplevel(window->shell_surface);
726 }
727
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000729#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
731 data = cairo_surface_get_user_data(window->cairo_surface,
732 &surface_data_key);
733
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100734 cairo_gl_surface_swapbuffers(window->cairo_surface);
735 wl_egl_window_get_attached_size(data->window,
736 &window->server_allocation.width,
737 &window->server_allocation.height);
738 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000739#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 buffer =
742 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400743 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100744
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200745 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100746 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400747 wl_surface_damage(window->surface, 0, 0,
748 window->allocation.width,
749 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100750 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400751 cairo_surface_destroy(window->cairo_surface);
752 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100753 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000754 default:
755 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100756 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500757
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500758 if (window->input_region) {
759 wl_surface_set_input_region(window->surface,
760 window->input_region);
761 wl_region_destroy(window->input_region);
762 window->input_region = NULL;
763 }
764
765 if (window->opaque_region) {
766 wl_surface_set_opaque_region(window->surface,
767 window->opaque_region);
768 wl_region_destroy(window->opaque_region);
769 window->opaque_region = NULL;
770 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500771}
772
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400773int
774window_has_focus(struct window *window)
775{
776 return window->focus_count > 0;
777}
778
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500779void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400780window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500781{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400782 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100783 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500784}
785
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400786void
787window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400788{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500789 cairo_surface_reference(surface);
790
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400791 if (window->cairo_surface != NULL)
792 cairo_surface_destroy(window->cairo_surface);
793
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500794 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400795}
796
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798static void
799window_resize_cairo_window_surface(struct window *window)
800{
801 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200802 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100803
804 data = cairo_surface_get_user_data(window->cairo_surface,
805 &surface_data_key);
806
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200807 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100808 wl_egl_window_resize(data->window,
809 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200810 window->allocation.height,
811 x,y);
812
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 cairo_gl_surface_set_size(window->cairo_surface,
814 window->allocation.width,
815 window->allocation.height);
816}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400819struct display *
820window_get_display(struct window *window)
821{
822 return window->display;
823}
824
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400825void
826window_create_surface(struct window *window)
827{
828 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829 uint32_t flags = 0;
830
831 if (!window->transparent)
832 flags = SURFACE_OPAQUE;
833
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500835#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
837 if (window->cairo_surface) {
838 window_resize_cairo_window_surface(window);
839 return;
840 }
841 surface = display_create_surface(window->display,
842 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400843 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845#endif
846 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400847 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400848 &window->allocation,
849 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800851 default:
852 surface = NULL;
853 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400854 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855
856 window_set_surface(window, surface);
857 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400858}
859
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200860static void frame_destroy(struct frame *frame);
861
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400862void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500863window_destroy(struct window *window)
864{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200865 struct display *display = window->display;
866 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100867 struct window_output *window_output;
868 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200869
870 if (window->redraw_scheduled)
871 wl_list_remove(&window->redraw_task.link);
872
873 wl_list_for_each(input, &display->input_list, link) {
874 if (input->pointer_focus == window)
875 input->pointer_focus = NULL;
876 if (input->keyboard_focus == window)
877 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500878 if (input->focus_widget &&
879 input->focus_widget->window == window)
880 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200881 }
882
Rob Bradford7507b572012-05-15 17:55:34 +0100883 wl_list_for_each_safe(window_output, window_output_tmp,
884 &window->window_output_list, link) {
885 free (window_output);
886 }
887
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500888 if (window->input_region)
889 wl_region_destroy(window->input_region);
890 if (window->opaque_region)
891 wl_region_destroy(window->opaque_region);
892
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200893 if (window->frame)
894 frame_destroy(window->frame);
895
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200896 if (window->shell_surface)
897 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500898 wl_surface_destroy(window->surface);
899 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200900
901 if (window->cairo_surface != NULL)
902 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200903
904 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500905 free(window);
906}
907
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500908static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500909widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500911 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500913 wl_list_for_each(child, &widget->child_list, link) {
914 target = widget_find_widget(child, x, y);
915 if (target)
916 return target;
917 }
918
919 if (widget->allocation.x <= x &&
920 x < widget->allocation.x + widget->allocation.width &&
921 widget->allocation.y <= y &&
922 y < widget->allocation.y + widget->allocation.height) {
923 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400924 }
925
926 return NULL;
927}
928
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500929static struct widget *
930widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400931{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500932 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400933
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500934 widget = malloc(sizeof *widget);
935 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500936 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500937 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500938 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500939 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500940 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300941 widget->tooltip = NULL;
942 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500943
944 return widget;
945}
946
947struct widget *
948window_add_widget(struct window *window, void *data)
949{
950 window->widget = widget_create(window, data);
951 wl_list_init(&window->widget->link);
952
953 return window->widget;
954}
955
956struct widget *
957widget_add_widget(struct widget *parent, void *data)
958{
959 struct widget *widget;
960
961 widget = widget_create(parent->window, data);
962 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965}
966
967void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500968widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200970 struct display *display = widget->window->display;
971 struct input *input;
972
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300973 if (widget->tooltip) {
974 free(widget->tooltip);
975 widget->tooltip = NULL;
976 }
977
Pekka Paalanene156fb62012-01-19 13:51:38 +0200978 wl_list_for_each(input, &display->input_list, link) {
979 if (input->focus_widget == widget)
980 input->focus_widget = NULL;
981 }
982
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500983 wl_list_remove(&widget->link);
984 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985}
986
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500990 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991}
992
993void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500994widget_set_size(struct widget *widget, int32_t width, int32_t height)
995{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500996 widget->allocation.width = width;
997 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500998}
999
1000void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001001widget_set_allocation(struct widget *widget,
1002 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001004 widget->allocation.x = x;
1005 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001006 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007}
1008
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001009void
1010widget_set_transparent(struct widget *widget, int transparent)
1011{
1012 widget->opaque = !transparent;
1013}
1014
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001016widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001018 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019}
1020
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001021void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001022widget_set_resize_handler(struct widget *widget,
1023 widget_resize_handler_t handler)
1024{
1025 widget->resize_handler = handler;
1026}
1027
1028void
1029widget_set_redraw_handler(struct widget *widget,
1030 widget_redraw_handler_t handler)
1031{
1032 widget->redraw_handler = handler;
1033}
1034
1035void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001036widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001037{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001038 widget->enter_handler = handler;
1039}
1040
1041void
1042widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1043{
1044 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001045}
1046
1047void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001048widget_set_motion_handler(struct widget *widget,
1049 widget_motion_handler_t handler)
1050{
1051 widget->motion_handler = handler;
1052}
1053
1054void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001055widget_set_button_handler(struct widget *widget,
1056 widget_button_handler_t handler)
1057{
1058 widget->button_handler = handler;
1059}
1060
1061void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001062widget_schedule_redraw(struct widget *widget)
1063{
1064 window_schedule_redraw(widget->window);
1065}
1066
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001067cairo_surface_t *
1068window_get_surface(struct window *window)
1069{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001070 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001071}
1072
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001073struct wl_surface *
1074window_get_wl_surface(struct window *window)
1075{
1076 return window->surface;
1077}
1078
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001079struct wl_shell_surface *
1080window_get_wl_shell_surface(struct window *window)
1081{
1082 return window->shell_surface;
1083}
1084
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001085static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001086tooltip_redraw_handler(struct widget *widget, void *data)
1087{
1088 cairo_t *cr;
1089 const int32_t r = 3;
1090 struct tooltip *tooltip = data;
1091 int32_t width, height;
1092 struct window *window = widget->window;
1093
1094 cr = cairo_create(window->cairo_surface);
1095 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1096 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1097 cairo_paint(cr);
1098
1099 width = window->allocation.width;
1100 height = window->allocation.height;
1101 rounded_rect(cr, 0, 0, width, height, r);
1102
1103 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1104 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1105 cairo_fill(cr);
1106
1107 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1108 cairo_move_to(cr, 10, 16);
1109 cairo_show_text(cr, tooltip->entry);
1110 cairo_destroy(cr);
1111}
1112
1113static cairo_text_extents_t
1114get_text_extents(struct tooltip *tooltip)
1115{
1116 struct window *window;
1117 cairo_t *cr;
1118 cairo_text_extents_t extents;
1119
1120 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1121 * created yet */
1122 window = tooltip->widget->window->parent;
1123 cr = cairo_create(window->cairo_surface);
1124 cairo_text_extents(cr, tooltip->entry, &extents);
1125 cairo_destroy(cr);
1126
1127 return extents;
1128}
1129
1130static int
1131window_create_tooltip(struct tooltip *tooltip)
1132{
1133 struct widget *parent = tooltip->parent;
1134 struct display *display = parent->window->display;
1135 struct window *window;
1136 const int offset_y = 27;
1137 const int margin = 3;
1138 cairo_text_extents_t extents;
1139
1140 if (tooltip->widget)
1141 return 0;
1142
1143 window = window_create_transient(display, parent->window, tooltip->x,
1144 tooltip->y + offset_y,
1145 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1146 if (!window)
1147 return -1;
1148
1149 tooltip->window = window;
1150 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1151
1152 extents = get_text_extents(tooltip);
1153 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1154 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1155
1156 return 0;
1157}
1158
1159void
1160widget_destroy_tooltip(struct widget *parent)
1161{
1162 struct tooltip *tooltip = parent->tooltip;
1163
1164 parent->tooltip_count = 0;
1165 if (!tooltip)
1166 return;
1167
1168 if (tooltip->widget) {
1169 widget_destroy(tooltip->widget);
1170 window_destroy(tooltip->window);
1171 tooltip->widget = NULL;
1172 tooltip->window = NULL;
1173 }
1174
1175 close(tooltip->tooltip_fd);
1176 free(tooltip->entry);
1177 free(tooltip);
1178 parent->tooltip = NULL;
1179}
1180
1181static void
1182tooltip_func(struct task *task, uint32_t events)
1183{
1184 struct tooltip *tooltip =
1185 container_of(task, struct tooltip, tooltip_task);
1186 uint64_t exp;
1187
Martin Olsson8df662a2012-07-08 03:03:47 +02001188 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1189 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001190 window_create_tooltip(tooltip);
1191}
1192
1193#define TOOLTIP_TIMEOUT 500
1194static int
1195tooltip_timer_reset(struct tooltip *tooltip)
1196{
1197 struct itimerspec its;
1198
1199 its.it_interval.tv_sec = 0;
1200 its.it_interval.tv_nsec = 0;
1201 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1202 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1203 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1204 fprintf(stderr, "could not set timerfd\n: %m");
1205 return -1;
1206 }
1207
1208 return 0;
1209}
1210
1211int
1212widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1213{
1214 struct tooltip *tooltip = parent->tooltip;
1215
1216 parent->tooltip_count++;
1217 if (tooltip) {
1218 tooltip->x = x;
1219 tooltip->y = y;
1220 tooltip_timer_reset(tooltip);
1221 return 0;
1222 }
1223
1224 /* the handler might be triggered too fast via input device motion, so
1225 * we need this check here to make sure tooltip is fully initialized */
1226 if (parent->tooltip_count > 1)
1227 return 0;
1228
1229 tooltip = malloc(sizeof *tooltip);
1230 if (!tooltip)
1231 return -1;
1232
1233 parent->tooltip = tooltip;
1234 tooltip->parent = parent;
1235 tooltip->widget = NULL;
1236 tooltip->window = NULL;
1237 tooltip->x = x;
1238 tooltip->y = y;
1239 tooltip->entry = strdup(entry);
1240 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1241 if (tooltip->tooltip_fd < 0) {
1242 fprintf(stderr, "could not create timerfd\n: %m");
1243 return -1;
1244 }
1245
1246 tooltip->tooltip_task.run = tooltip_func;
1247 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1248 EPOLLIN, &tooltip->tooltip_task);
1249 tooltip_timer_reset(tooltip);
1250
1251 return 0;
1252}
1253
1254static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255frame_resize_handler(struct widget *widget,
1256 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 struct frame *frame = data;
1259 struct widget *child = frame->child;
1260 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001261 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001262 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001263 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001264 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001266 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001268 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001269 decoration_width = (t->width + t->margin) * 2;
1270 decoration_height = t->width +
1271 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001272
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001273 allocation.x = t->width + t->margin;
1274 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001275 allocation.width = width - decoration_width;
1276 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001277
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001278 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001279
1280 wl_list_for_each(button, &frame->buttons_list, link)
1281 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001282 } else {
1283 decoration_width = 0;
1284 decoration_height = 0;
1285
1286 allocation.x = 0;
1287 allocation.y = 0;
1288 allocation.width = width;
1289 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001290 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001291
1292 wl_list_for_each(button, &frame->buttons_list, link)
1293 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001294 }
1295
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001296 widget_set_allocation(child, allocation.x, allocation.y,
1297 allocation.width, allocation.height);
1298
1299 if (child->resize_handler)
1300 child->resize_handler(child,
1301 allocation.width,
1302 allocation.height,
1303 child->user_data);
1304
Scott Moreauf7e498c2012-05-14 11:39:29 -06001305 width = child->allocation.width + decoration_width;
1306 height = child->allocation.height + decoration_height;
1307
Kristian Høgsberg023be102012-07-31 11:59:12 -04001308 if (widget->window->type != TYPE_FULLSCREEN) {
1309 widget->window->input_region =
1310 wl_compositor_create_region(display->compositor);
1311 wl_region_add(widget->window->input_region,
1312 t->margin, t->margin,
1313 width - 2 * t->margin,
1314 height - 2 * t->margin);
1315 }
1316
Scott Moreauf7e498c2012-05-14 11:39:29 -06001317 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001318
1319 if (child->opaque) {
1320 widget->window->opaque_region =
1321 wl_compositor_create_region(display->compositor);
1322 wl_region_add(widget->window->opaque_region,
1323 opaque_margin, opaque_margin,
1324 widget->allocation.width - 2 * opaque_margin,
1325 widget->allocation.height - 2 * opaque_margin);
1326 }
Martin Minarik1998b152012-05-10 02:04:35 +02001327
1328 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001329 x_r = frame->widget->allocation.width - t->width - t->margin;
1330 x_l = t->width + t->margin;
1331 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001332 wl_list_for_each(button, &frame->buttons_list, link) {
1333 const int button_padding = 4;
1334 w = cairo_image_surface_get_width(button->icon);
1335 h = cairo_image_surface_get_height(button->icon);
1336
1337 if (button->decoration == FRAME_BUTTON_FANCY)
1338 w += 10;
1339
1340 if (button->align == FRAME_BUTTON_LEFT) {
1341 widget_set_allocation(button->widget,
1342 x_l, y , w + 1, h + 1);
1343 x_l += w;
1344 x_l += button_padding;
1345 } else {
1346 x_r -= w;
1347 widget_set_allocation(button->widget,
1348 x_r, y , w + 1, h + 1);
1349 x_r -= button_padding;
1350 }
1351 }
1352}
1353
1354static int
1355frame_button_enter_handler(struct widget *widget,
1356 struct input *input, float x, float y, void *data)
1357{
1358 struct frame_button *frame_button = data;
1359
1360 widget_schedule_redraw(frame_button->widget);
1361 frame_button->state = FRAME_BUTTON_OVER;
1362
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001363 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001364}
1365
1366static void
1367frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1368{
1369 struct frame_button *frame_button = data;
1370
1371 widget_schedule_redraw(frame_button->widget);
1372 frame_button->state = FRAME_BUTTON_DEFAULT;
1373}
1374
1375static void
1376frame_button_button_handler(struct widget *widget,
1377 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001378 uint32_t button,
1379 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001380{
1381 struct frame_button *frame_button = data;
1382 struct window *window = widget->window;
1383
1384 if (button != BTN_LEFT)
1385 return;
1386
1387 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001388 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001389 frame_button->state = FRAME_BUTTON_ACTIVE;
1390 widget_schedule_redraw(frame_button->widget);
1391
1392 if (frame_button->type == FRAME_BUTTON_ICON)
1393 window_show_frame_menu(window, input, time);
1394 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001395 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001396 frame_button->state = FRAME_BUTTON_DEFAULT;
1397 widget_schedule_redraw(frame_button->widget);
1398 break;
1399 }
1400
1401 switch (frame_button->type) {
1402 case FRAME_BUTTON_CLOSE:
1403 if (window->close_handler)
1404 window->close_handler(window->parent,
1405 window->user_data);
1406 else
1407 display_exit(window->display);
1408 break;
1409 case FRAME_BUTTON_MINIMIZE:
1410 fprintf(stderr,"Minimize stub\n");
1411 break;
1412 case FRAME_BUTTON_MAXIMIZE:
1413 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1414 break;
1415 default:
1416 /* Unknown operation */
1417 break;
1418 }
1419}
1420
1421static void
1422frame_button_redraw_handler(struct widget *widget, void *data)
1423{
1424 struct frame_button *frame_button = data;
1425 cairo_t *cr;
1426 int width, height, x, y;
1427 struct window *window = widget->window;
1428
1429 x = widget->allocation.x;
1430 y = widget->allocation.y;
1431 width = widget->allocation.width;
1432 height = widget->allocation.height;
1433
1434 if (!width)
1435 return;
1436 if (!height)
1437 return;
1438 if (widget->opaque)
1439 return;
1440
1441 cr = cairo_create(window->cairo_surface);
1442
1443 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1444 cairo_set_line_width(cr, 1);
1445
1446 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1447 cairo_rectangle (cr, x, y, 25, 16);
1448
1449 cairo_stroke_preserve(cr);
1450
1451 switch (frame_button->state) {
1452 case FRAME_BUTTON_DEFAULT:
1453 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1454 break;
1455 case FRAME_BUTTON_OVER:
1456 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1457 break;
1458 case FRAME_BUTTON_ACTIVE:
1459 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1460 break;
1461 }
1462
1463 cairo_fill (cr);
1464
1465 x += 4;
1466 }
1467
1468 cairo_set_source_surface(cr, frame_button->icon, x, y);
1469 cairo_paint(cr);
1470
1471 cairo_destroy(cr);
1472}
1473
1474static struct widget *
1475frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1476 enum frame_button_align align, enum frame_button_decoration style)
1477{
1478 struct frame_button *frame_button;
1479 const char *icon = data;
1480
1481 frame_button = malloc (sizeof *frame_button);
1482 memset(frame_button, 0, sizeof *frame_button);
1483
1484 frame_button->icon = cairo_image_surface_create_from_png(icon);
1485 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1486 frame_button->frame = frame;
1487 frame_button->type = type;
1488 frame_button->align = align;
1489 frame_button->decoration = style;
1490
1491 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1492
1493 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1494 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1495 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1496 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1497 return frame_button->widget;
1498}
1499
1500static void
1501frame_button_destroy(struct frame_button *frame_button)
1502{
1503 widget_destroy(frame_button->widget);
1504 wl_list_remove(&frame_button->link);
1505 cairo_surface_destroy(frame_button->icon);
1506 free(frame_button);
1507
1508 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509}
1510
1511static void
1512frame_redraw_handler(struct widget *widget, void *data)
1513{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001515 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001516 struct theme *t = window->display->theme;
1517 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001518
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001519 if (window->type == TYPE_FULLSCREEN)
1520 return;
1521
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001522 cr = cairo_create(window->cairo_surface);
1523
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001524 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001525 flags |= THEME_FRAME_ACTIVE;
1526 theme_render_frame(t, cr, widget->allocation.width,
1527 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001528
1529 cairo_destroy(cr);
1530}
1531
1532static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001533frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001534{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001536 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001537
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001538 location = theme_get_location(t, input->sx, input->sy,
1539 frame->widget->allocation.width,
1540 frame->widget->allocation.height);
1541
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001543 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001544 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001545 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001546 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001547 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001548 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001549 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001550 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001551 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001552 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001553 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001554 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001555 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001556 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001557 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001558 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001559 case THEME_LOCATION_EXTERIOR:
1560 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001561 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001562 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001563 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001564}
1565
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001566static void
1567frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001568{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001569 switch (index) {
1570 case 0: /* close */
1571 if (window->close_handler)
1572 window->close_handler(window->parent,
1573 window->user_data);
1574 else
1575 display_exit(window->display);
1576 break;
1577 case 1: /* fullscreen */
1578 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001579 if (window->fullscreen_handler)
1580 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582 }
1583}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001584
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001585void
1586window_show_frame_menu(struct window *window,
1587 struct input *input, uint32_t time)
1588{
1589 int32_t x, y;
1590
1591 static const char *entries[] = {
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001592 "Close", "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001593 };
1594
1595 input_get_position(input, &x, &y);
1596 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001597 x - 10, y - 10, frame_menu_func, entries,
1598 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001599}
1600
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601static int
1602frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001603 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604{
1605 return frame_get_pointer_image_for_location(data, input);
1606}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001607
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608static int
1609frame_motion_handler(struct widget *widget,
1610 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001611 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612{
1613 return frame_get_pointer_image_for_location(data, input);
1614}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001615
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617frame_button_handler(struct widget *widget,
1618 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001619 uint32_t button, enum wl_pointer_button_state state,
1620 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621
1622{
1623 struct frame *frame = data;
1624 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001625 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001627
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001628 location = theme_get_location(display->theme, input->sx, input->sy,
1629 frame->widget->allocation.width,
1630 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001631
Daniel Stone4dbadb12012-05-30 16:31:51 +01001632 if (window->display->shell && button == BTN_LEFT &&
1633 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001634 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001635 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 if (!window->shell_surface)
1637 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001638 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001640 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001641 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001642 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001643 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001644 case THEME_LOCATION_RESIZING_TOP:
1645 case THEME_LOCATION_RESIZING_BOTTOM:
1646 case THEME_LOCATION_RESIZING_LEFT:
1647 case THEME_LOCATION_RESIZING_RIGHT:
1648 case THEME_LOCATION_RESIZING_TOP_LEFT:
1649 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1650 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1651 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001652 if (!window->shell_surface)
1653 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001654 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001655
1656 if (!display->dpy) {
1657 /* If we're using shm, allocate a big
1658 pool to create buffers out of while
1659 we resize. We should probably base
1660 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001661 window->pool =
1662 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001663 }
1664
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001665 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001666 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001667 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001668 break;
1669 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001670 } else if (button == BTN_RIGHT &&
1671 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001672 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001673 }
1674}
1675
1676struct widget *
1677frame_create(struct window *window, void *data)
1678{
1679 struct frame *frame;
1680
1681 frame = malloc(sizeof *frame);
1682 memset(frame, 0, sizeof *frame);
1683
1684 frame->widget = window_add_widget(window, frame);
1685 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001686
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001687 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1688 widget_set_resize_handler(frame->widget, frame_resize_handler);
1689 widget_set_enter_handler(frame->widget, frame_enter_handler);
1690 widget_set_motion_handler(frame->widget, frame_motion_handler);
1691 widget_set_button_handler(frame->widget, frame_button_handler);
1692
Martin Minarik1998b152012-05-10 02:04:35 +02001693 /* Create empty list for frame buttons */
1694 wl_list_init(&frame->buttons_list);
1695
1696 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1697 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1698
1699 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1700 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1701
1702 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1703 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1704
1705 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1706 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1707
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001708 window->frame = frame;
1709
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001710 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001711}
1712
Kristian Høgsberga1627922012-06-20 17:30:03 -04001713void
1714frame_set_child_size(struct widget *widget, int child_width, int child_height)
1715{
1716 struct display *display = widget->window->display;
1717 struct theme *t = display->theme;
1718 int decoration_width, decoration_height;
1719 int width, height;
1720
1721 if (widget->window->type != TYPE_FULLSCREEN) {
1722 decoration_width = (t->width + t->margin) * 2;
1723 decoration_height = t->width +
1724 t->titlebar_height + t->margin * 2;
1725
1726 width = child_width + decoration_width;
1727 height = child_height + decoration_height;
1728 } else {
1729 width = child_width;
1730 height = child_height;
1731 }
1732
1733 window_schedule_resize(widget->window, width, height);
1734}
1735
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001736static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001737frame_destroy(struct frame *frame)
1738{
Martin Minarik1998b152012-05-10 02:04:35 +02001739 struct frame_button *button, *tmp;
1740
1741 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1742 frame_button_destroy(button);
1743
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001744 /* frame->child must be destroyed by the application */
1745 widget_destroy(frame->widget);
1746 free(frame);
1747}
1748
1749static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001750input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001751 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001754 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001755
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001756 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001757 return;
1758
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001759 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001760 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761 widget = old;
1762 if (input->grab)
1763 widget = input->grab;
1764 if (widget->leave_handler)
1765 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001766 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001767 }
1768
1769 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770 widget = focus;
1771 if (input->grab)
1772 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001773 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001774 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001775 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001776 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001777
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001778 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001779 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001780}
1781
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001782void
1783input_grab(struct input *input, struct widget *widget, uint32_t button)
1784{
1785 input->grab = widget;
1786 input->grab_button = button;
1787}
1788
1789void
1790input_ungrab(struct input *input)
1791{
1792 struct widget *widget;
1793
1794 input->grab = NULL;
1795 if (input->pointer_focus) {
1796 widget = widget_find_widget(input->pointer_focus->widget,
1797 input->sx, input->sy);
1798 input_set_focus_widget(input, widget, input->sx, input->sy);
1799 }
1800}
1801
1802static void
1803input_remove_pointer_focus(struct input *input)
1804{
1805 struct window *window = input->pointer_focus;
1806
1807 if (!window)
1808 return;
1809
1810 input_set_focus_widget(input, NULL, 0, 0);
1811
1812 input->pointer_focus = NULL;
1813 input->current_cursor = CURSOR_UNSET;
1814}
1815
1816static void
1817pointer_handle_enter(void *data, struct wl_pointer *pointer,
1818 uint32_t serial, struct wl_surface *surface,
1819 wl_fixed_t sx_w, wl_fixed_t sy_w)
1820{
1821 struct input *input = data;
1822 struct window *window;
1823 struct widget *widget;
1824 float sx = wl_fixed_to_double(sx_w);
1825 float sy = wl_fixed_to_double(sy_w);
1826
1827 if (!surface) {
1828 /* enter event for a window we've just destroyed */
1829 return;
1830 }
1831
1832 input->display->serial = serial;
1833 input->pointer_enter_serial = serial;
1834 input->pointer_focus = wl_surface_get_user_data(surface);
1835 window = input->pointer_focus;
1836
1837 if (window->pool) {
1838 shm_pool_destroy(window->pool);
1839 window->pool = NULL;
1840 /* Schedule a redraw to free the pool */
1841 window_schedule_redraw(window);
1842 }
1843
1844 input->sx = sx;
1845 input->sy = sy;
1846
1847 widget = widget_find_widget(window->widget, sx, sy);
1848 input_set_focus_widget(input, widget, sx, sy);
1849}
1850
1851static void
1852pointer_handle_leave(void *data, struct wl_pointer *pointer,
1853 uint32_t serial, struct wl_surface *surface)
1854{
1855 struct input *input = data;
1856
1857 input->display->serial = serial;
1858 input_remove_pointer_focus(input);
1859}
1860
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001861static void
Daniel Stone37816df2012-05-16 18:45:18 +01001862pointer_handle_motion(void *data, struct wl_pointer *pointer,
1863 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001864{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001865 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001866 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001867 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001868 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001869 float sx = wl_fixed_to_double(sx_w);
1870 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001871
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001872 input->sx = sx;
1873 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001874
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001875 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001876 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001878 }
1879
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001880 if (input->grab)
1881 widget = input->grab;
1882 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001883 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001884 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001885 cursor = widget->motion_handler(input->focus_widget,
1886 input, time, sx, sy,
1887 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001888
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001889 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001890}
1891
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001892static void
Daniel Stone37816df2012-05-16 18:45:18 +01001893pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001894 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001895{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001896 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001897 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001898 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001899
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001900 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001901 if (input->focus_widget && input->grab == NULL &&
1902 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001903 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001904
Neil Roberts6b28aad2012-01-23 19:11:18 +00001905 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001906 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001907 (*widget->button_handler)(widget,
1908 input, time,
1909 button, state,
1910 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001911
Daniel Stone4dbadb12012-05-30 16:31:51 +01001912 if (input->grab && input->grab_button == button &&
1913 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001914 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001915}
1916
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001917static void
Daniel Stone37816df2012-05-16 18:45:18 +01001918pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001919 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001920{
1921}
1922
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001923static const struct wl_pointer_listener pointer_listener = {
1924 pointer_handle_enter,
1925 pointer_handle_leave,
1926 pointer_handle_motion,
1927 pointer_handle_button,
1928 pointer_handle_axis,
1929};
1930
1931static void
1932input_remove_keyboard_focus(struct input *input)
1933{
1934 struct window *window = input->keyboard_focus;
1935 struct itimerspec its;
1936
1937 its.it_interval.tv_sec = 0;
1938 its.it_interval.tv_nsec = 0;
1939 its.it_value.tv_sec = 0;
1940 its.it_value.tv_nsec = 0;
1941 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1942
1943 if (!window)
1944 return;
1945
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001946 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001947 if (window->keyboard_focus_handler)
1948 (*window->keyboard_focus_handler)(window, NULL,
1949 window->user_data);
1950
1951 input->keyboard_focus = NULL;
1952}
1953
1954static void
1955keyboard_repeat_func(struct task *task, uint32_t events)
1956{
1957 struct input *input =
1958 container_of(task, struct input, repeat_task);
1959 struct window *window = input->keyboard_focus;
1960 uint64_t exp;
1961
1962 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1963 /* If we change the timer between the fd becoming
1964 * readable and getting here, there'll be nothing to
1965 * read and we get EAGAIN. */
1966 return;
1967
1968 if (window && window->key_handler) {
1969 (*window->key_handler)(window, input, input->repeat_time,
1970 input->repeat_key, input->repeat_sym,
1971 WL_KEYBOARD_KEY_STATE_PRESSED,
1972 window->user_data);
1973 }
1974}
1975
1976static void
1977keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1978 uint32_t format, int fd, uint32_t size)
1979{
1980 struct input *input = data;
1981 char *map_str;
1982
1983 if (!data) {
1984 close(fd);
1985 return;
1986 }
1987
1988 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1989 close(fd);
1990 return;
1991 }
1992
1993 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1994 if (map_str == MAP_FAILED) {
1995 close(fd);
1996 return;
1997 }
1998
1999 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2000 map_str,
2001 XKB_KEYMAP_FORMAT_TEXT_V1,
2002 0);
2003 munmap(map_str, size);
2004 close(fd);
2005
2006 if (!input->xkb.keymap) {
2007 fprintf(stderr, "failed to compile keymap\n");
2008 return;
2009 }
2010
2011 input->xkb.state = xkb_state_new(input->xkb.keymap);
2012 if (!input->xkb.state) {
2013 fprintf(stderr, "failed to create XKB state\n");
2014 xkb_map_unref(input->xkb.keymap);
2015 input->xkb.keymap = NULL;
2016 return;
2017 }
2018
2019 input->xkb.control_mask =
2020 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2021 input->xkb.alt_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2023 input->xkb.shift_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2025}
2026
2027static void
2028keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2029 uint32_t serial, struct wl_surface *surface,
2030 struct wl_array *keys)
2031{
2032 struct input *input = data;
2033 struct window *window;
2034
2035 input->display->serial = serial;
2036 input->keyboard_focus = wl_surface_get_user_data(surface);
2037
2038 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002039 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002040 if (window->keyboard_focus_handler)
2041 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002042 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002043}
2044
2045static void
2046keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2047 uint32_t serial, struct wl_surface *surface)
2048{
2049 struct input *input = data;
2050
2051 input->display->serial = serial;
2052 input_remove_keyboard_focus(input);
2053}
2054
Scott Moreau210d0792012-03-22 10:47:01 -06002055static void
Daniel Stone37816df2012-05-16 18:45:18 +01002056keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002057 uint32_t serial, uint32_t time, uint32_t key,
2058 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002059{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002060 struct input *input = data;
2061 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002062 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002063 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002064 const xkb_keysym_t *syms;
2065 xkb_keysym_t sym;
2066 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002067 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002068
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002069 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002070 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002071 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002072 return;
2073
Daniel Stone97f68542012-05-30 16:32:01 +01002074 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002075
Daniel Stone97f68542012-05-30 16:32:01 +01002076 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002077 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002078 XKB_STATE_LATCHED);
2079 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002080 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002081 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002082 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002083 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002084 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002085 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002086
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002087 sym = XKB_KEY_NoSymbol;
2088 if (num_syms == 1)
2089 sym = syms[0];
2090
2091 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002092 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002093 window_set_maximized(window,
2094 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002095 } else if (sym == XKB_KEY_F11 &&
2096 window->fullscreen_handler &&
2097 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2098 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002099 } else if (sym == XKB_KEY_F4 &&
2100 input->modifiers == MOD_ALT_MASK &&
2101 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2102 if (window->close_handler)
2103 window->close_handler(window->parent,
2104 window->user_data);
2105 else
2106 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002107 } else if (window->key_handler) {
2108 (*window->key_handler)(window, input, time, key,
2109 sym, state, window->user_data);
2110 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002111
2112 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2113 key == input->repeat_key) {
2114 its.it_interval.tv_sec = 0;
2115 its.it_interval.tv_nsec = 0;
2116 its.it_value.tv_sec = 0;
2117 its.it_value.tv_nsec = 0;
2118 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2119 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2120 input->repeat_sym = sym;
2121 input->repeat_key = key;
2122 input->repeat_time = time;
2123 its.it_interval.tv_sec = 0;
2124 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2125 its.it_value.tv_sec = 0;
2126 its.it_value.tv_nsec = 400 * 1000 * 1000;
2127 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2128 }
2129}
2130
2131static void
Daniel Stone351eb612012-05-31 15:27:47 -04002132keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2133 uint32_t serial, uint32_t mods_depressed,
2134 uint32_t mods_latched, uint32_t mods_locked,
2135 uint32_t group)
2136{
2137 struct input *input = data;
2138
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002139 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2140 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002141}
2142
Daniel Stone37816df2012-05-16 18:45:18 +01002143static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002144 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002145 keyboard_handle_enter,
2146 keyboard_handle_leave,
2147 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002148 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002149};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002150
2151static void
Daniel Stone37816df2012-05-16 18:45:18 +01002152seat_handle_capabilities(void *data, struct wl_seat *seat,
2153 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002154{
Daniel Stone37816df2012-05-16 18:45:18 +01002155 struct input *input = data;
2156
2157 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2158 input->pointer = wl_seat_get_pointer(seat);
2159 wl_pointer_set_user_data(input->pointer, input);
2160 wl_pointer_add_listener(input->pointer, &pointer_listener,
2161 input);
2162 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2163 wl_pointer_destroy(input->pointer);
2164 input->pointer = NULL;
2165 }
2166
2167 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2168 input->keyboard = wl_seat_get_keyboard(seat);
2169 wl_keyboard_set_user_data(input->keyboard, input);
2170 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2171 input);
2172 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2173 wl_keyboard_destroy(input->keyboard);
2174 input->keyboard = NULL;
2175 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002176}
2177
Daniel Stone37816df2012-05-16 18:45:18 +01002178static const struct wl_seat_listener seat_listener = {
2179 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002180};
2181
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002182void
2183input_get_position(struct input *input, int32_t *x, int32_t *y)
2184{
2185 *x = input->sx;
2186 *y = input->sy;
2187}
2188
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002189struct display *
2190input_get_display(struct input *input)
2191{
2192 return input->display;
2193}
2194
Daniel Stone37816df2012-05-16 18:45:18 +01002195struct wl_seat *
2196input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002197{
Daniel Stone37816df2012-05-16 18:45:18 +01002198 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002199}
2200
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002201uint32_t
2202input_get_modifiers(struct input *input)
2203{
2204 return input->modifiers;
2205}
2206
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002207struct widget *
2208input_get_focus_widget(struct input *input)
2209{
2210 return input->focus_widget;
2211}
2212
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002213struct data_offer {
2214 struct wl_data_offer *offer;
2215 struct input *input;
2216 struct wl_array types;
2217 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002218
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002219 struct task io_task;
2220 int fd;
2221 data_func_t func;
2222 int32_t x, y;
2223 void *user_data;
2224};
2225
2226static void
2227data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2228{
2229 struct data_offer *offer = data;
2230 char **p;
2231
2232 p = wl_array_add(&offer->types, sizeof *p);
2233 *p = strdup(type);
2234}
2235
2236static const struct wl_data_offer_listener data_offer_listener = {
2237 data_offer_offer,
2238};
2239
2240static void
2241data_offer_destroy(struct data_offer *offer)
2242{
2243 char **p;
2244
2245 offer->refcount--;
2246 if (offer->refcount == 0) {
2247 wl_data_offer_destroy(offer->offer);
2248 for (p = offer->types.data; *p; p++)
2249 free(*p);
2250 wl_array_release(&offer->types);
2251 free(offer);
2252 }
2253}
2254
2255static void
2256data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002257 struct wl_data_device *data_device,
2258 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002259{
2260 struct data_offer *offer;
2261
2262 offer = malloc(sizeof *offer);
2263
2264 wl_array_init(&offer->types);
2265 offer->refcount = 1;
2266 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002267 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002268 wl_data_offer_add_listener(offer->offer,
2269 &data_offer_listener, offer);
2270}
2271
2272static void
2273data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002274 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002275 wl_fixed_t x_w, wl_fixed_t y_w,
2276 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002277{
2278 struct input *input = data;
2279 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002280 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002281 float x = wl_fixed_to_double(x_w);
2282 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002283 char **p;
2284
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002285 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002286 window = wl_surface_get_user_data(surface);
2287 input->pointer_focus = window;
2288
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002289 if (offer) {
2290 input->drag_offer = wl_data_offer_get_user_data(offer);
2291
2292 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2293 *p = NULL;
2294
2295 types_data = input->drag_offer->types.data;
2296 } else {
2297 input->drag_offer = NULL;
2298 types_data = NULL;
2299 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300
2301 window = input->pointer_focus;
2302 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002303 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002304 window->user_data);
2305}
2306
2307static void
2308data_device_leave(void *data, struct wl_data_device *data_device)
2309{
2310 struct input *input = data;
2311
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002312 if (input->drag_offer) {
2313 data_offer_destroy(input->drag_offer);
2314 input->drag_offer = NULL;
2315 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316}
2317
2318static void
2319data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002320 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002321{
2322 struct input *input = data;
2323 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002324 float x = wl_fixed_to_double(x_w);
2325 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002326 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002327
2328 input->sx = x;
2329 input->sy = y;
2330
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002331 if (input->drag_offer)
2332 types_data = input->drag_offer->types.data;
2333 else
2334 types_data = NULL;
2335
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002336 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002337 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002338 window->user_data);
2339}
2340
2341static void
2342data_device_drop(void *data, struct wl_data_device *data_device)
2343{
2344 struct input *input = data;
2345 struct window *window = input->pointer_focus;
2346
2347 if (window->drop_handler)
2348 window->drop_handler(window, input,
2349 input->sx, input->sy, window->user_data);
2350}
2351
2352static void
2353data_device_selection(void *data,
2354 struct wl_data_device *wl_data_device,
2355 struct wl_data_offer *offer)
2356{
2357 struct input *input = data;
2358 char **p;
2359
2360 if (input->selection_offer)
2361 data_offer_destroy(input->selection_offer);
2362
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002363 if (offer) {
2364 input->selection_offer = wl_data_offer_get_user_data(offer);
2365 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2366 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002367 } else {
2368 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002369 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002370}
2371
2372static const struct wl_data_device_listener data_device_listener = {
2373 data_device_data_offer,
2374 data_device_enter,
2375 data_device_leave,
2376 data_device_motion,
2377 data_device_drop,
2378 data_device_selection
2379};
2380
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002381static void
2382input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002384 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002385 struct wl_cursor *cursor;
2386 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002387
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002388 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002389 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002390 return;
2391
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002392 if (index >= (int) cursor->image_count) {
2393 fprintf(stderr, "cursor index out of range\n");
2394 return;
2395 }
2396
2397 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002398 buffer = wl_cursor_image_get_buffer(image);
2399 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400 return;
2401
Kristian Høgsbergae277372012-08-01 09:41:08 -04002402 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002403 input->pointer_surface,
2404 image->hotspot_x, image->hotspot_y);
2405 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2406 wl_surface_damage(input->pointer_surface, 0, 0,
2407 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002408}
2409
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002410static const struct wl_callback_listener pointer_surface_listener;
2411
2412static void
2413pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2414 uint32_t time)
2415{
2416 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002417 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002418 int i;
2419
2420 if (callback) {
2421 assert(callback == input->cursor_frame_cb);
2422 wl_callback_destroy(callback);
2423 input->cursor_frame_cb = NULL;
2424 }
2425
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002426 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002427 wl_pointer_set_cursor(input->pointer,
2428 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002429 NULL, 0, 0);
2430 return;
2431 }
2432
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002433 if (input->current_cursor == CURSOR_UNSET)
2434 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002435 cursor = input->display->cursors[input->current_cursor];
2436 if (!cursor)
2437 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002438
2439 /* FIXME We don't have the current time on the first call so we set
2440 * the animation start to the time of the first frame callback. */
2441 if (time == 0)
2442 input->cursor_anim_start = 0;
2443 else if (input->cursor_anim_start == 0)
2444 input->cursor_anim_start = time;
2445
2446 if (time == 0 || input->cursor_anim_start == 0)
2447 i = 0;
2448 else
2449 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2450
2451 input_set_pointer_image_index(input, i);
2452
2453 if (cursor->image_count == 1)
2454 return;
2455
2456 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2457 wl_callback_add_listener(input->cursor_frame_cb,
2458 &pointer_surface_listener, input);
2459}
2460
2461static const struct wl_callback_listener pointer_surface_listener = {
2462 pointer_surface_frame_callback
2463};
2464
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002465void
2466input_set_pointer_image(struct input *input, int pointer)
2467{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002468 int force = 0;
2469
2470 if (input->pointer_enter_serial > input->cursor_serial)
2471 force = 1;
2472
2473 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002474 return;
2475
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002476 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002477 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002478 if (!input->cursor_frame_cb)
2479 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002480 else if (force) {
2481 /* The current frame callback may be stuck if, for instance,
2482 * the set cursor request was processed by the server after
2483 * this client lost the focus. In this case the cursor surface
2484 * might not be mapped and the frame callback wouldn't ever
2485 * complete. Send a set_cursor and attach to try to map the
2486 * cursor surface again so that the callback will finish */
2487 input_set_pointer_image_index(input, 0);
2488 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002489}
2490
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002491struct wl_data_device *
2492input_get_data_device(struct input *input)
2493{
2494 return input->data_device;
2495}
2496
2497void
2498input_set_selection(struct input *input,
2499 struct wl_data_source *source, uint32_t time)
2500{
2501 wl_data_device_set_selection(input->data_device, source, time);
2502}
2503
2504void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002505input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002506{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002507 wl_data_offer_accept(input->drag_offer->offer,
2508 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002509}
2510
2511static void
2512offer_io_func(struct task *task, uint32_t events)
2513{
2514 struct data_offer *offer =
2515 container_of(task, struct data_offer, io_task);
2516 unsigned int len;
2517 char buffer[4096];
2518
2519 len = read(offer->fd, buffer, sizeof buffer);
2520 offer->func(buffer, len,
2521 offer->x, offer->y, offer->user_data);
2522
2523 if (len == 0) {
2524 close(offer->fd);
2525 data_offer_destroy(offer);
2526 }
2527}
2528
2529static void
2530data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2531 data_func_t func, void *user_data)
2532{
2533 int p[2];
2534
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002535 if (pipe2(p, O_CLOEXEC) == -1)
2536 return;
2537
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002538 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2539 close(p[1]);
2540
2541 offer->io_task.run = offer_io_func;
2542 offer->fd = p[0];
2543 offer->func = func;
2544 offer->refcount++;
2545 offer->user_data = user_data;
2546
2547 display_watch_fd(offer->input->display,
2548 offer->fd, EPOLLIN, &offer->io_task);
2549}
2550
2551void
2552input_receive_drag_data(struct input *input, const char *mime_type,
2553 data_func_t func, void *data)
2554{
2555 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2556 input->drag_offer->x = input->sx;
2557 input->drag_offer->y = input->sy;
2558}
2559
2560int
2561input_receive_selection_data(struct input *input, const char *mime_type,
2562 data_func_t func, void *data)
2563{
2564 char **p;
2565
2566 if (input->selection_offer == NULL)
2567 return -1;
2568
2569 for (p = input->selection_offer->types.data; *p; p++)
2570 if (strcmp(mime_type, *p) == 0)
2571 break;
2572
2573 if (*p == NULL)
2574 return -1;
2575
2576 data_offer_receive_data(input->selection_offer,
2577 mime_type, func, data);
2578 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002579}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002580
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002581int
2582input_receive_selection_data_to_fd(struct input *input,
2583 const char *mime_type, int fd)
2584{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002585 if (input->selection_offer)
2586 wl_data_offer_receive(input->selection_offer->offer,
2587 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002588
2589 return 0;
2590}
2591
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002592void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002593window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002594{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002595 if (!window->shell_surface)
2596 return;
2597
Daniel Stone37816df2012-05-16 18:45:18 +01002598 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002599}
2600
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002601static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002602idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002603{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002604 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002605
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002606 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002608 widget_set_allocation(widget,
2609 window->pending_allocation.x,
2610 window->pending_allocation.y,
2611 window->pending_allocation.width,
2612 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002613
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002614 if (window->input_region) {
2615 wl_region_destroy(window->input_region);
2616 window->input_region = NULL;
2617 }
2618
2619 if (window->opaque_region) {
2620 wl_region_destroy(window->opaque_region);
2621 window->opaque_region = NULL;
2622 }
2623
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002624 if (widget->resize_handler)
2625 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002626 widget->allocation.width,
2627 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002628 widget->user_data);
2629
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002630 if (window->allocation.width != widget->allocation.width ||
2631 window->allocation.height != widget->allocation.height) {
2632 window->allocation = widget->allocation;
2633 window_schedule_redraw(window);
2634 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002635}
2636
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002637void
2638window_schedule_resize(struct window *window, int width, int height)
2639{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002640 window->pending_allocation.x = 0;
2641 window->pending_allocation.y = 0;
2642 window->pending_allocation.width = width;
2643 window->pending_allocation.height = height;
2644
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002645 if (window->min_allocation.width == 0)
2646 window->min_allocation = window->pending_allocation;
2647 if (window->pending_allocation.width < window->min_allocation.width)
2648 window->pending_allocation.width = window->min_allocation.width;
2649 if (window->pending_allocation.height < window->min_allocation.height)
2650 window->pending_allocation.height = window->min_allocation.height;
2651
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002652 window->resize_needed = 1;
2653 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002654}
2655
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002656void
2657widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2658{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002659 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002660}
2661
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002662static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002663handle_ping(void *data, struct wl_shell_surface *shell_surface,
2664 uint32_t serial)
2665{
2666 wl_shell_surface_pong(shell_surface, serial);
2667}
2668
2669static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002670handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002671 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002672{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002673 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002674
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002675 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002676 return;
2677
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002678 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002679 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002680}
2681
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002683menu_destroy(struct menu *menu)
2684{
2685 widget_destroy(menu->widget);
2686 window_destroy(menu->window);
2687 free(menu);
2688}
2689
2690static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002691handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2692{
2693 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002694 struct menu *menu = window->widget->user_data;
2695
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002696 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002697 * device. Or just use wl_callback. And this really needs to
2698 * be a window vfunc that the menu can set. And we need the
2699 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002701 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002702 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002703 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002704}
2705
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002706static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002707 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002708 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002709 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002710};
2711
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002712void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002713window_get_allocation(struct window *window,
2714 struct rectangle *allocation)
2715{
2716 *allocation = window->allocation;
2717}
2718
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002719static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002720widget_redraw(struct widget *widget)
2721{
2722 struct widget *child;
2723
2724 if (widget->redraw_handler)
2725 widget->redraw_handler(widget, widget->user_data);
2726 wl_list_for_each(child, &widget->child_list, link)
2727 widget_redraw(child);
2728}
2729
2730static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002731frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2732{
2733 struct window *window = data;
2734
2735 wl_callback_destroy(callback);
2736 window->redraw_scheduled = 0;
2737 if (window->redraw_needed)
2738 window_schedule_redraw(window);
2739}
2740
2741static const struct wl_callback_listener listener = {
2742 frame_callback
2743};
2744
2745static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002746idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002747{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002748 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002749 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002750
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002751 if (window->resize_needed)
2752 idle_resize(window);
2753
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002754 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002755 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002756 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002757 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002758 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002759
2760 callback = wl_surface_frame(window->surface);
2761 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002762}
2763
2764void
2765window_schedule_redraw(struct window *window)
2766{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002767 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002768 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002769 window->redraw_task.run = idle_redraw;
2770 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002771 window->redraw_scheduled = 1;
2772 }
2773}
2774
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002775void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002776window_set_fullscreen(struct window *window, int fullscreen)
2777{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002778 if (!window->display->shell)
2779 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002780
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002781 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002782 return;
2783
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002784 if (fullscreen) {
2785 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002786 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002787 wl_shell_surface_set_fullscreen(window->shell_surface,
2788 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2789 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002790 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002791 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002792 wl_shell_surface_set_toplevel(window->shell_surface);
2793 window_schedule_resize(window,
2794 window->saved_allocation.width,
2795 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002796 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002797}
2798
2799void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002800window_set_maximized(struct window *window, int maximized)
2801{
2802 if (!window->display->shell)
2803 return;
2804
2805 if ((window->type == TYPE_MAXIMIZED) == maximized)
2806 return;
2807
2808 if (window->type == TYPE_TOPLEVEL) {
2809 window->saved_allocation = window->allocation;
2810 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2811 window->type = TYPE_MAXIMIZED;
2812 } else {
2813 wl_shell_surface_set_toplevel(window->shell_surface);
2814 window->type = TYPE_TOPLEVEL;
2815 window_schedule_resize(window,
2816 window->saved_allocation.width,
2817 window->saved_allocation.height);
2818 }
2819}
2820
2821void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002822window_set_user_data(struct window *window, void *data)
2823{
2824 window->user_data = data;
2825}
2826
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002827void *
2828window_get_user_data(struct window *window)
2829{
2830 return window->user_data;
2831}
2832
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002833void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002834window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002835 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002836{
2837 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002838}
2839
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002840void
2841window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002842 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002843{
2844 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002845}
2846
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002847void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002848window_set_data_handler(struct window *window, window_data_handler_t handler)
2849{
2850 window->data_handler = handler;
2851}
2852
2853void
2854window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2855{
2856 window->drop_handler = handler;
2857}
2858
2859void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002860window_set_close_handler(struct window *window,
2861 window_close_handler_t handler)
2862{
2863 window->close_handler = handler;
2864}
2865
2866void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002867window_set_fullscreen_handler(struct window *window,
2868 window_fullscreen_handler_t handler)
2869{
2870 window->fullscreen_handler = handler;
2871}
2872
2873void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002874window_set_title(struct window *window, const char *title)
2875{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002876 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002877 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002878 if (window->shell_surface)
2879 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002880}
2881
2882const char *
2883window_get_title(struct window *window)
2884{
2885 return window->title;
2886}
2887
2888void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002889window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2890{
2891 struct text_cursor_position *text_cursor_position =
2892 window->display->text_cursor_position;
2893
Scott Moreau9295ce02012-06-01 12:46:10 -06002894 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002895 return;
2896
2897 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002898 window->surface,
2899 wl_fixed_from_int(x),
2900 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002901}
2902
2903void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002904window_damage(struct window *window, int32_t x, int32_t y,
2905 int32_t width, int32_t height)
2906{
2907 wl_surface_damage(window->surface, x, y, width, height);
2908}
2909
Casey Dahlin9074db52012-04-19 22:50:09 -04002910static void
2911surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002912 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002913{
Rob Bradford7507b572012-05-15 17:55:34 +01002914 struct window *window = data;
2915 struct output *output;
2916 struct output *output_found = NULL;
2917 struct window_output *window_output;
2918
2919 wl_list_for_each(output, &window->display->output_list, link) {
2920 if (output->output == wl_output) {
2921 output_found = output;
2922 break;
2923 }
2924 }
2925
2926 if (!output_found)
2927 return;
2928
2929 window_output = malloc (sizeof *window_output);
2930 window_output->output = output_found;
2931
2932 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002933}
2934
2935static void
2936surface_leave(void *data,
2937 struct wl_surface *wl_surface, struct wl_output *output)
2938{
Rob Bradford7507b572012-05-15 17:55:34 +01002939 struct window *window = data;
2940 struct window_output *window_output;
2941 struct window_output *window_output_found = NULL;
2942
2943 wl_list_for_each(window_output, &window->window_output_list, link) {
2944 if (window_output->output->output == output) {
2945 window_output_found = window_output;
2946 break;
2947 }
2948 }
2949
2950 if (window_output_found) {
2951 wl_list_remove(&window_output_found->link);
2952 free(window_output_found);
2953 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002954}
2955
2956static const struct wl_surface_listener surface_listener = {
2957 surface_enter,
2958 surface_leave
2959};
2960
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002961static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002962window_create_internal(struct display *display,
2963 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002964{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002965 struct window *window;
2966
2967 window = malloc(sizeof *window);
2968 if (window == NULL)
2969 return NULL;
2970
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002971 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002972 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002973 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002974 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002975 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002976 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002977 window->shell_surface =
2978 wl_shell_get_shell_surface(display->shell,
2979 window->surface);
2980 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002981 window->allocation.x = 0;
2982 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002983 window->allocation.width = 0;
2984 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002985 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002986 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002987 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002988 window->input_region = NULL;
2989 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002990
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002991 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002992#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002993 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002994#else
2995 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2996#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002997 else
2998 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002999
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003000 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003001 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003002 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003003
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003004 if (window->shell_surface) {
3005 wl_shell_surface_set_user_data(window->shell_surface, window);
3006 wl_shell_surface_add_listener(window->shell_surface,
3007 &shell_surface_listener, window);
3008 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003009
Rob Bradford7507b572012-05-15 17:55:34 +01003010 wl_list_init (&window->window_output_list);
3011
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003012 return window;
3013}
3014
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003015struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003016window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003017{
3018 struct window *window;
3019
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003020 window = window_create_internal(display, NULL, TYPE_NONE);
3021 if (!window)
3022 return NULL;
3023
3024 return window;
3025}
3026
3027struct window *
3028window_create_custom(struct display *display)
3029{
3030 struct window *window;
3031
3032 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003033 if (!window)
3034 return NULL;
3035
3036 return window;
3037}
3038
3039struct window *
3040window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003041 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003042{
3043 struct window *window;
3044
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003045 window = window_create_internal(parent->display,
3046 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003047 if (!window)
3048 return NULL;
3049
3050 window->x = x;
3051 window->y = y;
3052
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003053 if (display->shell)
3054 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003055 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003056 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003057
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003058 return window;
3059}
3060
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003061static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003062menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003063{
3064 int next;
3065
3066 next = (sy - 8) / 20;
3067 if (menu->current != next) {
3068 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003069 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003070 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003071}
3072
3073static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003074menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003075 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003076 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003077{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003078 struct menu *menu = data;
3079
3080 if (widget == menu->widget)
3081 menu_set_item(data, y);
3082
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003083 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003084}
3085
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003086static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003087menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003088 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003089{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003090 struct menu *menu = data;
3091
3092 if (widget == menu->widget)
3093 menu_set_item(data, y);
3094
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003095 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003096}
3097
3098static void
3099menu_leave_handler(struct widget *widget, struct input *input, void *data)
3100{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003101 struct menu *menu = data;
3102
3103 if (widget == menu->widget)
3104 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003105}
3106
3107static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003108menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003109 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003110 uint32_t button, enum wl_pointer_button_state state,
3111 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003112
3113{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003114 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003115
Daniel Stone4dbadb12012-05-30 16:31:51 +01003116 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3117 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003118 /* Either relase after press-drag-release or
3119 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003120 menu->func(menu->window->parent,
3121 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003122 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003123 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003124 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003125}
3126
3127static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003128menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003129{
3130 cairo_t *cr;
3131 const int32_t r = 3, margin = 3;
3132 struct menu *menu = data;
3133 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003134 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003135
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003136 cr = cairo_create(window->cairo_surface);
3137 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3138 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3139 cairo_paint(cr);
3140
3141 width = window->allocation.width;
3142 height = window->allocation.height;
3143 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003144
3145 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003146 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3147 cairo_fill(cr);
3148
3149 for (i = 0; i < menu->count; i++) {
3150 if (i == menu->current) {
3151 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3152 cairo_rectangle(cr, margin, i * 20 + margin,
3153 width - 2 * margin, 20);
3154 cairo_fill(cr);
3155 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3156 cairo_move_to(cr, 10, i * 20 + 16);
3157 cairo_show_text(cr, menu->entries[i]);
3158 } else {
3159 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3160 cairo_move_to(cr, 10, i * 20 + 16);
3161 cairo_show_text(cr, menu->entries[i]);
3162 }
3163 }
3164
3165 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003166}
3167
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003168void
3169window_show_menu(struct display *display,
3170 struct input *input, uint32_t time, struct window *parent,
3171 int32_t x, int32_t y,
3172 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003173{
3174 struct window *window;
3175 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003176 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003177
3178 menu = malloc(sizeof *menu);
3179 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003180 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003181
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003182 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003183 if (!window) {
3184 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003185 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003186 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003187
3188 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003189 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003190 menu->entries = entries;
3191 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003192 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003193 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003194 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003195 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003196 window->type = TYPE_MENU;
3197 window->x = x;
3198 window->y = y;
3199
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003200 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003201 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003202 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003203 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003204 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003205
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003206 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003207 widget_set_enter_handler(menu->widget, menu_enter_handler);
3208 widget_set_leave_handler(menu->widget, menu_leave_handler);
3209 widget_set_motion_handler(menu->widget, menu_motion_handler);
3210 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003211
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003212 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003213 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003214}
3215
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003216void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003217window_set_buffer_type(struct window *window, enum window_buffer_type type)
3218{
3219 window->buffer_type = type;
3220}
3221
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003222
3223static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003224display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003225 struct wl_output *wl_output,
3226 int x, int y,
3227 int physical_width,
3228 int physical_height,
3229 int subpixel,
3230 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003231 const char *model,
3232 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003233{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003234 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003235
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003236 output->allocation.x = x;
3237 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003238}
3239
3240static void
3241display_handle_mode(void *data,
3242 struct wl_output *wl_output,
3243 uint32_t flags,
3244 int width,
3245 int height,
3246 int refresh)
3247{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003248 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003249 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003250
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003251 if (flags & WL_OUTPUT_MODE_CURRENT) {
3252 output->allocation.width = width;
3253 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003254 if (display->output_configure_handler)
3255 (*display->output_configure_handler)(
3256 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003257 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003258}
3259
3260static const struct wl_output_listener output_listener = {
3261 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003262 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003263};
3264
3265static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003266display_add_output(struct display *d, uint32_t id)
3267{
3268 struct output *output;
3269
3270 output = malloc(sizeof *output);
3271 if (output == NULL)
3272 return;
3273
3274 memset(output, 0, sizeof *output);
3275 output->display = d;
3276 output->output =
3277 wl_display_bind(d->display, id, &wl_output_interface);
3278 wl_list_insert(d->output_list.prev, &output->link);
3279
3280 wl_output_add_listener(output->output, &output_listener, output);
3281}
3282
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003283static void
3284output_destroy(struct output *output)
3285{
3286 if (output->destroy_handler)
3287 (*output->destroy_handler)(output, output->user_data);
3288
3289 wl_output_destroy(output->output);
3290 wl_list_remove(&output->link);
3291 free(output);
3292}
3293
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003294void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003295display_set_output_configure_handler(struct display *display,
3296 display_output_handler_t handler)
3297{
3298 struct output *output;
3299
3300 display->output_configure_handler = handler;
3301 if (!handler)
3302 return;
3303
3304 wl_list_for_each(output, &display->output_list, link)
3305 (*display->output_configure_handler)(output,
3306 display->user_data);
3307}
3308
3309void
3310output_set_user_data(struct output *output, void *data)
3311{
3312 output->user_data = data;
3313}
3314
3315void *
3316output_get_user_data(struct output *output)
3317{
3318 return output->user_data;
3319}
3320
3321void
3322output_set_destroy_handler(struct output *output,
3323 display_output_handler_t handler)
3324{
3325 output->destroy_handler = handler;
3326 /* FIXME: implement this, once we have way to remove outputs */
3327}
3328
3329void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003330output_get_allocation(struct output *output, struct rectangle *allocation)
3331{
3332 *allocation = output->allocation;
3333}
3334
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003335struct wl_output *
3336output_get_wl_output(struct output *output)
3337{
3338 return output->output;
3339}
3340
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003341static void
Daniel Stone97f68542012-05-30 16:32:01 +01003342fini_xkb(struct input *input)
3343{
3344 xkb_state_unref(input->xkb.state);
3345 xkb_map_unref(input->xkb.keymap);
3346}
3347
3348static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003349display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003350{
3351 struct input *input;
3352
3353 input = malloc(sizeof *input);
3354 if (input == NULL)
3355 return;
3356
3357 memset(input, 0, sizeof *input);
3358 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003359 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003360 input->pointer_focus = NULL;
3361 input->keyboard_focus = NULL;
3362 wl_list_insert(d->input_list.prev, &input->link);
3363
Daniel Stone37816df2012-05-16 18:45:18 +01003364 wl_seat_add_listener(input->seat, &seat_listener, input);
3365 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003366
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003367 input->data_device =
3368 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003369 input->seat);
3370 wl_data_device_add_listener(input->data_device, &data_device_listener,
3371 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003372
3373 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003374
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003375 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3376 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003377 input->repeat_task.run = keyboard_repeat_func;
3378 display_watch_fd(d, input->repeat_timer_fd,
3379 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003380}
3381
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003382static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003383input_destroy(struct input *input)
3384{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003385 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003386 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003387
3388 if (input->drag_offer)
3389 data_offer_destroy(input->drag_offer);
3390
3391 if (input->selection_offer)
3392 data_offer_destroy(input->selection_offer);
3393
3394 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003395 fini_xkb(input);
3396
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003397 wl_surface_destroy(input->pointer_surface);
3398
Pekka Paalanene1207c72011-12-16 12:02:09 +02003399 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003400 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003401 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003402 free(input);
3403}
3404
3405static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003406display_handle_global(struct wl_display *display, uint32_t id,
3407 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003408{
3409 struct display *d = data;
3410
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003411 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003412 d->compositor =
3413 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003414 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003415 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003416 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003417 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003418 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003419 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003420 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003421 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003422 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3423 d->data_device_manager =
3424 wl_display_bind(display, id,
3425 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003426 } else if (strcmp(interface, "text_cursor_position") == 0) {
3427 d->text_cursor_position =
3428 wl_display_bind(display, id,
3429 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003430 }
3431}
3432
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003433#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003434static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003435init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003436{
3437 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003438 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003439
Rob Clark6396ed32012-03-11 19:48:41 -05003440#ifdef USE_CAIRO_GLESV2
3441# define GL_BIT EGL_OPENGL_ES2_BIT
3442#else
3443# define GL_BIT EGL_OPENGL_BIT
3444#endif
3445
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003446 static const EGLint argb_cfg_attribs[] = {
3447 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003448 EGL_RED_SIZE, 1,
3449 EGL_GREEN_SIZE, 1,
3450 EGL_BLUE_SIZE, 1,
3451 EGL_ALPHA_SIZE, 1,
3452 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003453 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003454 EGL_NONE
3455 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003456
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003457#ifdef USE_CAIRO_GLESV2
3458 static const EGLint context_attribs[] = {
3459 EGL_CONTEXT_CLIENT_VERSION, 2,
3460 EGL_NONE
3461 };
3462 EGLint api = EGL_OPENGL_ES_API;
3463#else
3464 EGLint *context_attribs = NULL;
3465 EGLint api = EGL_OPENGL_API;
3466#endif
3467
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003468 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003469 if (!eglInitialize(d->dpy, &major, &minor)) {
3470 fprintf(stderr, "failed to initialize display\n");
3471 return -1;
3472 }
3473
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003474 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003475 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3476 return -1;
3477 }
3478
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003479 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3480 &d->argb_config, 1, &n) || n != 1) {
3481 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003482 return -1;
3483 }
3484
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003485 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003486 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003487 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003488 fprintf(stderr, "failed to create context\n");
3489 return -1;
3490 }
3491
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003492 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003493 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003494 return -1;
3495 }
3496
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003497#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003498 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3499 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3500 fprintf(stderr, "failed to get cairo egl argb device\n");
3501 return -1;
3502 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003503#endif
3504
3505 return 0;
3506}
3507
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003508static void
3509fini_egl(struct display *display)
3510{
3511#ifdef HAVE_CAIRO_EGL
3512 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003513#endif
3514
3515 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3516 EGL_NO_CONTEXT);
3517
3518 eglTerminate(display->dpy);
3519 eglReleaseThread();
3520}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003521#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003522
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003523static int
3524event_mask_update(uint32_t mask, void *data)
3525{
3526 struct display *d = data;
3527
3528 d->mask = mask;
3529
3530 return 0;
3531}
3532
3533static void
3534handle_display_data(struct task *task, uint32_t events)
3535{
3536 struct display *display =
3537 container_of(task, struct display, display_task);
3538
3539 wl_display_iterate(display->display, display->mask);
3540}
3541
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003542struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003543display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003544{
3545 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003546
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003547 d = malloc(sizeof *d);
3548 if (d == NULL)
3549 return NULL;
3550
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003551 memset(d, 0, sizeof *d);
3552
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003553 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003554 if (d->display == NULL) {
3555 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003556 return NULL;
3557 }
3558
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003559 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003560 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3561 d->display_task.run = handle_display_data;
3562 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3563
3564 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003565 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003566 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003567
Daniel Stone97f68542012-05-30 16:32:01 +01003568 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003569 if (d->xkb_context == NULL) {
3570 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003571 return NULL;
3572 }
3573
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003574 /* Set up listener so we'll catch all events. */
3575 wl_display_add_global_listener(d->display,
3576 display_handle_global, d);
3577
3578 /* Process connection events. */
3579 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003580#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003581 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003582 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003583#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003584
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003585 d->image_target_texture_2d =
3586 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3587 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3588 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3589
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003590 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003591
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003592 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003593
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003594 wl_list_init(&d->window_list);
3595
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003596 return d;
3597}
3598
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003599static void
3600display_destroy_outputs(struct display *display)
3601{
3602 struct output *tmp;
3603 struct output *output;
3604
3605 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3606 output_destroy(output);
3607}
3608
Pekka Paalanene1207c72011-12-16 12:02:09 +02003609static void
3610display_destroy_inputs(struct display *display)
3611{
3612 struct input *tmp;
3613 struct input *input;
3614
3615 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3616 input_destroy(input);
3617}
3618
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003619void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003620display_destroy(struct display *display)
3621{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003622 if (!wl_list_empty(&display->window_list))
3623 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3624
3625 if (!wl_list_empty(&display->deferred_list))
3626 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3627
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003628 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003629 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003630
Daniel Stone97f68542012-05-30 16:32:01 +01003631 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003632
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003633 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003634 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003635
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003636#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003637 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003638#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003639
Pekka Paalanenc2052982011-12-16 11:41:32 +02003640 if (display->shell)
3641 wl_shell_destroy(display->shell);
3642
3643 if (display->shm)
3644 wl_shm_destroy(display->shm);
3645
3646 if (display->data_device_manager)
3647 wl_data_device_manager_destroy(display->data_device_manager);
3648
3649 wl_compositor_destroy(display->compositor);
3650
3651 close(display->epoll_fd);
3652
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003653 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003654 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003655 free(display);
3656}
3657
3658void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003659display_set_user_data(struct display *display, void *data)
3660{
3661 display->user_data = data;
3662}
3663
3664void *
3665display_get_user_data(struct display *display)
3666{
3667 return display->user_data;
3668}
3669
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003670struct wl_display *
3671display_get_display(struct display *display)
3672{
3673 return display->display;
3674}
3675
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003676struct output *
3677display_get_output(struct display *display)
3678{
3679 return container_of(display->output_list.next, struct output, link);
3680}
3681
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003682struct wl_compositor *
3683display_get_compositor(struct display *display)
3684{
3685 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003686}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003687
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003688uint32_t
3689display_get_serial(struct display *display)
3690{
3691 return display->serial;
3692}
3693
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003694EGLDisplay
3695display_get_egl_display(struct display *d)
3696{
3697 return d->dpy;
3698}
3699
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003700struct wl_data_source *
3701display_create_data_source(struct display *display)
3702{
3703 return wl_data_device_manager_create_data_source(display->data_device_manager);
3704}
3705
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003706EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003707display_get_argb_egl_config(struct display *d)
3708{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003709 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003710}
3711
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003712struct wl_shell *
3713display_get_shell(struct display *display)
3714{
3715 return display->shell;
3716}
3717
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003718int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003719display_acquire_window_surface(struct display *display,
3720 struct window *window,
3721 EGLContext ctx)
3722{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003723#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003724 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003725 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003726
3727 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003728 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003729 device = cairo_surface_get_device(window->cairo_surface);
3730 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003731 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003732
Benjamin Franzke0c991632011-09-27 21:57:31 +02003733 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003734 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003735 ctx = display->argb_ctx;
3736 else
3737 assert(0);
3738 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003739
3740 data = cairo_surface_get_user_data(window->cairo_surface,
3741 &surface_data_key);
3742
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003743 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003744 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003745 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3746 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003747
3748 return 0;
3749#else
3750 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003751#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003752}
3753
3754void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003755display_release_window_surface(struct display *display,
3756 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003757{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003758#ifdef HAVE_CAIRO_EGL
3759 cairo_device_t *device;
3760
3761 device = cairo_surface_get_device(window->cairo_surface);
3762 if (!device)
3763 return;
3764
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003765 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003766 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003767 cairo_device_release(device);
3768#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003769}
3770
3771void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003772display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003773{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003774 wl_list_insert(&display->deferred_list, &task->link);
3775}
3776
3777void
3778display_watch_fd(struct display *display,
3779 int fd, uint32_t events, struct task *task)
3780{
3781 struct epoll_event ep;
3782
3783 ep.events = events;
3784 ep.data.ptr = task;
3785 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3786}
3787
3788void
3789display_run(struct display *display)
3790{
3791 struct task *task;
3792 struct epoll_event ep[16];
3793 int i, count;
3794
Pekka Paalanen826d7952011-12-15 10:14:07 +02003795 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003796 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003797 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003798
3799 while (!wl_list_empty(&display->deferred_list)) {
3800 task = container_of(display->deferred_list.next,
3801 struct task, link);
3802 wl_list_remove(&task->link);
3803 task->run(task, 0);
3804 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003805
3806 if (!display->running)
3807 break;
3808
3809 wl_display_flush(display->display);
3810
3811 count = epoll_wait(display->epoll_fd,
3812 ep, ARRAY_LENGTH(ep), -1);
3813 for (i = 0; i < count; i++) {
3814 task = ep[i].data.ptr;
3815 task->run(task, ep[i].events);
3816 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003817 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003818}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003819
3820void
3821display_exit(struct display *display)
3822{
3823 display->running = 0;
3824}