blob: dd65328eb822408486c438fd96adfc88b03f789a [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;
1582 case 2: /* rotate */
1583 case 3: /* scale */
1584 break;
1585 }
1586}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001587
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001588void
1589window_show_frame_menu(struct window *window,
1590 struct input *input, uint32_t time)
1591{
1592 int32_t x, y;
1593
1594 static const char *entries[] = {
1595 "Close", "Fullscreen", "Rotate", "Scale"
1596 };
1597
1598 input_get_position(input, &x, &y);
1599 window_show_menu(window->display, input, time, window,
1600 x - 10, y - 10, frame_menu_func, entries, 4);
1601}
1602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603static int
1604frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001605 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606{
1607 return frame_get_pointer_image_for_location(data, input);
1608}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001609
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610static int
1611frame_motion_handler(struct widget *widget,
1612 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001613 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614{
1615 return frame_get_pointer_image_for_location(data, input);
1616}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001617
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619frame_button_handler(struct widget *widget,
1620 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001621 uint32_t button, enum wl_pointer_button_state state,
1622 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623
1624{
1625 struct frame *frame = data;
1626 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001627 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001629
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001630 location = theme_get_location(display->theme, input->sx, input->sy,
1631 frame->widget->allocation.width,
1632 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001633
Daniel Stone4dbadb12012-05-30 16:31:51 +01001634 if (window->display->shell && button == BTN_LEFT &&
1635 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001637 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001638 if (!window->shell_surface)
1639 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001640 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001641 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001643 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001644 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001645 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001646 case THEME_LOCATION_RESIZING_TOP:
1647 case THEME_LOCATION_RESIZING_BOTTOM:
1648 case THEME_LOCATION_RESIZING_LEFT:
1649 case THEME_LOCATION_RESIZING_RIGHT:
1650 case THEME_LOCATION_RESIZING_TOP_LEFT:
1651 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1652 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1653 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001654 if (!window->shell_surface)
1655 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001656 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001657
1658 if (!display->dpy) {
1659 /* If we're using shm, allocate a big
1660 pool to create buffers out of while
1661 we resize. We should probably base
1662 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001663 window->pool =
1664 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001665 }
1666
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001667 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001668 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001669 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001670 break;
1671 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001672 } else if (button == BTN_RIGHT &&
1673 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001674 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001675 }
1676}
1677
1678struct widget *
1679frame_create(struct window *window, void *data)
1680{
1681 struct frame *frame;
1682
1683 frame = malloc(sizeof *frame);
1684 memset(frame, 0, sizeof *frame);
1685
1686 frame->widget = window_add_widget(window, frame);
1687 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001688
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001689 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1690 widget_set_resize_handler(frame->widget, frame_resize_handler);
1691 widget_set_enter_handler(frame->widget, frame_enter_handler);
1692 widget_set_motion_handler(frame->widget, frame_motion_handler);
1693 widget_set_button_handler(frame->widget, frame_button_handler);
1694
Martin Minarik1998b152012-05-10 02:04:35 +02001695 /* Create empty list for frame buttons */
1696 wl_list_init(&frame->buttons_list);
1697
1698 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1699 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1700
1701 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1702 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1703
1704 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1705 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1706
1707 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1708 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1709
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001710 window->frame = frame;
1711
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001712 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001713}
1714
Kristian Høgsberga1627922012-06-20 17:30:03 -04001715void
1716frame_set_child_size(struct widget *widget, int child_width, int child_height)
1717{
1718 struct display *display = widget->window->display;
1719 struct theme *t = display->theme;
1720 int decoration_width, decoration_height;
1721 int width, height;
1722
1723 if (widget->window->type != TYPE_FULLSCREEN) {
1724 decoration_width = (t->width + t->margin) * 2;
1725 decoration_height = t->width +
1726 t->titlebar_height + t->margin * 2;
1727
1728 width = child_width + decoration_width;
1729 height = child_height + decoration_height;
1730 } else {
1731 width = child_width;
1732 height = child_height;
1733 }
1734
1735 window_schedule_resize(widget->window, width, height);
1736}
1737
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001738static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001739frame_destroy(struct frame *frame)
1740{
Martin Minarik1998b152012-05-10 02:04:35 +02001741 struct frame_button *button, *tmp;
1742
1743 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1744 frame_button_destroy(button);
1745
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001746 /* frame->child must be destroyed by the application */
1747 widget_destroy(frame->widget);
1748 free(frame);
1749}
1750
1751static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001752input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001753 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001755 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001756 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001757
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001758 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001759 return;
1760
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001761 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001762 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001763 widget = old;
1764 if (input->grab)
1765 widget = input->grab;
1766 if (widget->leave_handler)
1767 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001768 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001769 }
1770
1771 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001772 widget = focus;
1773 if (input->grab)
1774 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001775 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001776 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001779
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001780 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001781 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001782}
1783
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001784void
1785input_grab(struct input *input, struct widget *widget, uint32_t button)
1786{
1787 input->grab = widget;
1788 input->grab_button = button;
1789}
1790
1791void
1792input_ungrab(struct input *input)
1793{
1794 struct widget *widget;
1795
1796 input->grab = NULL;
1797 if (input->pointer_focus) {
1798 widget = widget_find_widget(input->pointer_focus->widget,
1799 input->sx, input->sy);
1800 input_set_focus_widget(input, widget, input->sx, input->sy);
1801 }
1802}
1803
1804static void
1805input_remove_pointer_focus(struct input *input)
1806{
1807 struct window *window = input->pointer_focus;
1808
1809 if (!window)
1810 return;
1811
1812 input_set_focus_widget(input, NULL, 0, 0);
1813
1814 input->pointer_focus = NULL;
1815 input->current_cursor = CURSOR_UNSET;
1816}
1817
1818static void
1819pointer_handle_enter(void *data, struct wl_pointer *pointer,
1820 uint32_t serial, struct wl_surface *surface,
1821 wl_fixed_t sx_w, wl_fixed_t sy_w)
1822{
1823 struct input *input = data;
1824 struct window *window;
1825 struct widget *widget;
1826 float sx = wl_fixed_to_double(sx_w);
1827 float sy = wl_fixed_to_double(sy_w);
1828
1829 if (!surface) {
1830 /* enter event for a window we've just destroyed */
1831 return;
1832 }
1833
1834 input->display->serial = serial;
1835 input->pointer_enter_serial = serial;
1836 input->pointer_focus = wl_surface_get_user_data(surface);
1837 window = input->pointer_focus;
1838
1839 if (window->pool) {
1840 shm_pool_destroy(window->pool);
1841 window->pool = NULL;
1842 /* Schedule a redraw to free the pool */
1843 window_schedule_redraw(window);
1844 }
1845
1846 input->sx = sx;
1847 input->sy = sy;
1848
1849 widget = widget_find_widget(window->widget, sx, sy);
1850 input_set_focus_widget(input, widget, sx, sy);
1851}
1852
1853static void
1854pointer_handle_leave(void *data, struct wl_pointer *pointer,
1855 uint32_t serial, struct wl_surface *surface)
1856{
1857 struct input *input = data;
1858
1859 input->display->serial = serial;
1860 input_remove_pointer_focus(input);
1861}
1862
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001863static void
Daniel Stone37816df2012-05-16 18:45:18 +01001864pointer_handle_motion(void *data, struct wl_pointer *pointer,
1865 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001866{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001867 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001868 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001869 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001870 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001871 float sx = wl_fixed_to_double(sx_w);
1872 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001873
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001874 input->sx = sx;
1875 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001876
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001877 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001878 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001879 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001880 }
1881
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001882 if (input->grab)
1883 widget = input->grab;
1884 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001885 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001886 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001887 cursor = widget->motion_handler(input->focus_widget,
1888 input, time, sx, sy,
1889 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001890
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001891 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001892}
1893
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001894static void
Daniel Stone37816df2012-05-16 18:45:18 +01001895pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001896 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001897{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001898 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001899 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001900 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001901
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001902 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001903 if (input->focus_widget && input->grab == NULL &&
1904 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001905 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001906
Neil Roberts6b28aad2012-01-23 19:11:18 +00001907 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001908 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001909 (*widget->button_handler)(widget,
1910 input, time,
1911 button, state,
1912 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001913
Daniel Stone4dbadb12012-05-30 16:31:51 +01001914 if (input->grab && input->grab_button == button &&
1915 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001916 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001917}
1918
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001919static void
Daniel Stone37816df2012-05-16 18:45:18 +01001920pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001921 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001922{
1923}
1924
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001925static const struct wl_pointer_listener pointer_listener = {
1926 pointer_handle_enter,
1927 pointer_handle_leave,
1928 pointer_handle_motion,
1929 pointer_handle_button,
1930 pointer_handle_axis,
1931};
1932
1933static void
1934input_remove_keyboard_focus(struct input *input)
1935{
1936 struct window *window = input->keyboard_focus;
1937 struct itimerspec its;
1938
1939 its.it_interval.tv_sec = 0;
1940 its.it_interval.tv_nsec = 0;
1941 its.it_value.tv_sec = 0;
1942 its.it_value.tv_nsec = 0;
1943 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1944
1945 if (!window)
1946 return;
1947
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001948 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001949 if (window->keyboard_focus_handler)
1950 (*window->keyboard_focus_handler)(window, NULL,
1951 window->user_data);
1952
1953 input->keyboard_focus = NULL;
1954}
1955
1956static void
1957keyboard_repeat_func(struct task *task, uint32_t events)
1958{
1959 struct input *input =
1960 container_of(task, struct input, repeat_task);
1961 struct window *window = input->keyboard_focus;
1962 uint64_t exp;
1963
1964 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1965 /* If we change the timer between the fd becoming
1966 * readable and getting here, there'll be nothing to
1967 * read and we get EAGAIN. */
1968 return;
1969
1970 if (window && window->key_handler) {
1971 (*window->key_handler)(window, input, input->repeat_time,
1972 input->repeat_key, input->repeat_sym,
1973 WL_KEYBOARD_KEY_STATE_PRESSED,
1974 window->user_data);
1975 }
1976}
1977
1978static void
1979keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1980 uint32_t format, int fd, uint32_t size)
1981{
1982 struct input *input = data;
1983 char *map_str;
1984
1985 if (!data) {
1986 close(fd);
1987 return;
1988 }
1989
1990 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1991 close(fd);
1992 return;
1993 }
1994
1995 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1996 if (map_str == MAP_FAILED) {
1997 close(fd);
1998 return;
1999 }
2000
2001 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2002 map_str,
2003 XKB_KEYMAP_FORMAT_TEXT_V1,
2004 0);
2005 munmap(map_str, size);
2006 close(fd);
2007
2008 if (!input->xkb.keymap) {
2009 fprintf(stderr, "failed to compile keymap\n");
2010 return;
2011 }
2012
2013 input->xkb.state = xkb_state_new(input->xkb.keymap);
2014 if (!input->xkb.state) {
2015 fprintf(stderr, "failed to create XKB state\n");
2016 xkb_map_unref(input->xkb.keymap);
2017 input->xkb.keymap = NULL;
2018 return;
2019 }
2020
2021 input->xkb.control_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2023 input->xkb.alt_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2025 input->xkb.shift_mask =
2026 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2027}
2028
2029static void
2030keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2031 uint32_t serial, struct wl_surface *surface,
2032 struct wl_array *keys)
2033{
2034 struct input *input = data;
2035 struct window *window;
2036
2037 input->display->serial = serial;
2038 input->keyboard_focus = wl_surface_get_user_data(surface);
2039
2040 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002041 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002042 if (window->keyboard_focus_handler)
2043 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002044 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002045}
2046
2047static void
2048keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2049 uint32_t serial, struct wl_surface *surface)
2050{
2051 struct input *input = data;
2052
2053 input->display->serial = serial;
2054 input_remove_keyboard_focus(input);
2055}
2056
Scott Moreau210d0792012-03-22 10:47:01 -06002057static void
Daniel Stone37816df2012-05-16 18:45:18 +01002058keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002059 uint32_t serial, uint32_t time, uint32_t key,
2060 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002061{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002062 struct input *input = data;
2063 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002064 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002065 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002066 const xkb_keysym_t *syms;
2067 xkb_keysym_t sym;
2068 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002069 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002070
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002071 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002072 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002073 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002074 return;
2075
Daniel Stone97f68542012-05-30 16:32:01 +01002076 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002077
Daniel Stone97f68542012-05-30 16:32:01 +01002078 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002079 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002080 XKB_STATE_LATCHED);
2081 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002082 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002083 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002084 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002085 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002086 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002087 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002088
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002089 sym = XKB_KEY_NoSymbol;
2090 if (num_syms == 1)
2091 sym = syms[0];
2092
2093 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002094 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002095 window_set_maximized(window,
2096 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002097 } else if (sym == XKB_KEY_F11 &&
2098 window->fullscreen_handler &&
2099 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2100 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002101 } else if (sym == XKB_KEY_F4 &&
2102 input->modifiers == MOD_ALT_MASK &&
2103 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2104 if (window->close_handler)
2105 window->close_handler(window->parent,
2106 window->user_data);
2107 else
2108 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002109 } else if (window->key_handler) {
2110 (*window->key_handler)(window, input, time, key,
2111 sym, state, window->user_data);
2112 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002113
2114 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2115 key == input->repeat_key) {
2116 its.it_interval.tv_sec = 0;
2117 its.it_interval.tv_nsec = 0;
2118 its.it_value.tv_sec = 0;
2119 its.it_value.tv_nsec = 0;
2120 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2121 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2122 input->repeat_sym = sym;
2123 input->repeat_key = key;
2124 input->repeat_time = time;
2125 its.it_interval.tv_sec = 0;
2126 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2127 its.it_value.tv_sec = 0;
2128 its.it_value.tv_nsec = 400 * 1000 * 1000;
2129 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2130 }
2131}
2132
2133static void
Daniel Stone351eb612012-05-31 15:27:47 -04002134keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2135 uint32_t serial, uint32_t mods_depressed,
2136 uint32_t mods_latched, uint32_t mods_locked,
2137 uint32_t group)
2138{
2139 struct input *input = data;
2140
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002141 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2142 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002143}
2144
Daniel Stone37816df2012-05-16 18:45:18 +01002145static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002146 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002147 keyboard_handle_enter,
2148 keyboard_handle_leave,
2149 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002150 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002151};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002152
2153static void
Daniel Stone37816df2012-05-16 18:45:18 +01002154seat_handle_capabilities(void *data, struct wl_seat *seat,
2155 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002156{
Daniel Stone37816df2012-05-16 18:45:18 +01002157 struct input *input = data;
2158
2159 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2160 input->pointer = wl_seat_get_pointer(seat);
2161 wl_pointer_set_user_data(input->pointer, input);
2162 wl_pointer_add_listener(input->pointer, &pointer_listener,
2163 input);
2164 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2165 wl_pointer_destroy(input->pointer);
2166 input->pointer = NULL;
2167 }
2168
2169 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2170 input->keyboard = wl_seat_get_keyboard(seat);
2171 wl_keyboard_set_user_data(input->keyboard, input);
2172 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2173 input);
2174 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2175 wl_keyboard_destroy(input->keyboard);
2176 input->keyboard = NULL;
2177 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002178}
2179
Daniel Stone37816df2012-05-16 18:45:18 +01002180static const struct wl_seat_listener seat_listener = {
2181 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002182};
2183
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002184void
2185input_get_position(struct input *input, int32_t *x, int32_t *y)
2186{
2187 *x = input->sx;
2188 *y = input->sy;
2189}
2190
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002191struct display *
2192input_get_display(struct input *input)
2193{
2194 return input->display;
2195}
2196
Daniel Stone37816df2012-05-16 18:45:18 +01002197struct wl_seat *
2198input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002199{
Daniel Stone37816df2012-05-16 18:45:18 +01002200 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002201}
2202
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002203uint32_t
2204input_get_modifiers(struct input *input)
2205{
2206 return input->modifiers;
2207}
2208
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002209struct widget *
2210input_get_focus_widget(struct input *input)
2211{
2212 return input->focus_widget;
2213}
2214
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002215struct data_offer {
2216 struct wl_data_offer *offer;
2217 struct input *input;
2218 struct wl_array types;
2219 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002220
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002221 struct task io_task;
2222 int fd;
2223 data_func_t func;
2224 int32_t x, y;
2225 void *user_data;
2226};
2227
2228static void
2229data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2230{
2231 struct data_offer *offer = data;
2232 char **p;
2233
2234 p = wl_array_add(&offer->types, sizeof *p);
2235 *p = strdup(type);
2236}
2237
2238static const struct wl_data_offer_listener data_offer_listener = {
2239 data_offer_offer,
2240};
2241
2242static void
2243data_offer_destroy(struct data_offer *offer)
2244{
2245 char **p;
2246
2247 offer->refcount--;
2248 if (offer->refcount == 0) {
2249 wl_data_offer_destroy(offer->offer);
2250 for (p = offer->types.data; *p; p++)
2251 free(*p);
2252 wl_array_release(&offer->types);
2253 free(offer);
2254 }
2255}
2256
2257static void
2258data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002259 struct wl_data_device *data_device,
2260 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261{
2262 struct data_offer *offer;
2263
2264 offer = malloc(sizeof *offer);
2265
2266 wl_array_init(&offer->types);
2267 offer->refcount = 1;
2268 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002269 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002270 wl_data_offer_add_listener(offer->offer,
2271 &data_offer_listener, offer);
2272}
2273
2274static void
2275data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002276 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002277 wl_fixed_t x_w, wl_fixed_t y_w,
2278 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002279{
2280 struct input *input = data;
2281 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002282 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002283 float x = wl_fixed_to_double(x_w);
2284 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002285 char **p;
2286
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002287 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002288 window = wl_surface_get_user_data(surface);
2289 input->pointer_focus = window;
2290
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002291 if (offer) {
2292 input->drag_offer = wl_data_offer_get_user_data(offer);
2293
2294 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2295 *p = NULL;
2296
2297 types_data = input->drag_offer->types.data;
2298 } else {
2299 input->drag_offer = NULL;
2300 types_data = NULL;
2301 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002302
2303 window = input->pointer_focus;
2304 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002305 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002306 window->user_data);
2307}
2308
2309static void
2310data_device_leave(void *data, struct wl_data_device *data_device)
2311{
2312 struct input *input = data;
2313
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002314 if (input->drag_offer) {
2315 data_offer_destroy(input->drag_offer);
2316 input->drag_offer = NULL;
2317 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002318}
2319
2320static void
2321data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002322 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002323{
2324 struct input *input = data;
2325 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002326 float x = wl_fixed_to_double(x_w);
2327 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002328 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002329
2330 input->sx = x;
2331 input->sy = y;
2332
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002333 if (input->drag_offer)
2334 types_data = input->drag_offer->types.data;
2335 else
2336 types_data = NULL;
2337
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002338 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002339 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002340 window->user_data);
2341}
2342
2343static void
2344data_device_drop(void *data, struct wl_data_device *data_device)
2345{
2346 struct input *input = data;
2347 struct window *window = input->pointer_focus;
2348
2349 if (window->drop_handler)
2350 window->drop_handler(window, input,
2351 input->sx, input->sy, window->user_data);
2352}
2353
2354static void
2355data_device_selection(void *data,
2356 struct wl_data_device *wl_data_device,
2357 struct wl_data_offer *offer)
2358{
2359 struct input *input = data;
2360 char **p;
2361
2362 if (input->selection_offer)
2363 data_offer_destroy(input->selection_offer);
2364
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002365 if (offer) {
2366 input->selection_offer = wl_data_offer_get_user_data(offer);
2367 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2368 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002369 } else {
2370 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002371 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002372}
2373
2374static const struct wl_data_device_listener data_device_listener = {
2375 data_device_data_offer,
2376 data_device_enter,
2377 data_device_leave,
2378 data_device_motion,
2379 data_device_drop,
2380 data_device_selection
2381};
2382
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002383static void
2384input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002385{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002387 struct wl_cursor *cursor;
2388 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002390 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002391 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002392 return;
2393
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002394 if (index >= (int) cursor->image_count) {
2395 fprintf(stderr, "cursor index out of range\n");
2396 return;
2397 }
2398
2399 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002400 buffer = wl_cursor_image_get_buffer(image);
2401 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002402 return;
2403
Kristian Høgsbergae277372012-08-01 09:41:08 -04002404 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002405 input->pointer_surface,
2406 image->hotspot_x, image->hotspot_y);
2407 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2408 wl_surface_damage(input->pointer_surface, 0, 0,
2409 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002410}
2411
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002412static const struct wl_callback_listener pointer_surface_listener;
2413
2414static void
2415pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2416 uint32_t time)
2417{
2418 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002419 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002420 int i;
2421
2422 if (callback) {
2423 assert(callback == input->cursor_frame_cb);
2424 wl_callback_destroy(callback);
2425 input->cursor_frame_cb = NULL;
2426 }
2427
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002428 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002429 wl_pointer_set_cursor(input->pointer,
2430 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002431 NULL, 0, 0);
2432 return;
2433 }
2434
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002435 if (input->current_cursor == CURSOR_UNSET)
2436 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002437 cursor = input->display->cursors[input->current_cursor];
2438 if (!cursor)
2439 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002440
2441 /* FIXME We don't have the current time on the first call so we set
2442 * the animation start to the time of the first frame callback. */
2443 if (time == 0)
2444 input->cursor_anim_start = 0;
2445 else if (input->cursor_anim_start == 0)
2446 input->cursor_anim_start = time;
2447
2448 if (time == 0 || input->cursor_anim_start == 0)
2449 i = 0;
2450 else
2451 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2452
2453 input_set_pointer_image_index(input, i);
2454
2455 if (cursor->image_count == 1)
2456 return;
2457
2458 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2459 wl_callback_add_listener(input->cursor_frame_cb,
2460 &pointer_surface_listener, input);
2461}
2462
2463static const struct wl_callback_listener pointer_surface_listener = {
2464 pointer_surface_frame_callback
2465};
2466
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002467void
2468input_set_pointer_image(struct input *input, int pointer)
2469{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002470 int force = 0;
2471
2472 if (input->pointer_enter_serial > input->cursor_serial)
2473 force = 1;
2474
2475 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002476 return;
2477
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002478 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002479 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002480 if (!input->cursor_frame_cb)
2481 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002482 else if (force) {
2483 /* The current frame callback may be stuck if, for instance,
2484 * the set cursor request was processed by the server after
2485 * this client lost the focus. In this case the cursor surface
2486 * might not be mapped and the frame callback wouldn't ever
2487 * complete. Send a set_cursor and attach to try to map the
2488 * cursor surface again so that the callback will finish */
2489 input_set_pointer_image_index(input, 0);
2490 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002491}
2492
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002493struct wl_data_device *
2494input_get_data_device(struct input *input)
2495{
2496 return input->data_device;
2497}
2498
2499void
2500input_set_selection(struct input *input,
2501 struct wl_data_source *source, uint32_t time)
2502{
2503 wl_data_device_set_selection(input->data_device, source, time);
2504}
2505
2506void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002507input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002508{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002509 wl_data_offer_accept(input->drag_offer->offer,
2510 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002511}
2512
2513static void
2514offer_io_func(struct task *task, uint32_t events)
2515{
2516 struct data_offer *offer =
2517 container_of(task, struct data_offer, io_task);
2518 unsigned int len;
2519 char buffer[4096];
2520
2521 len = read(offer->fd, buffer, sizeof buffer);
2522 offer->func(buffer, len,
2523 offer->x, offer->y, offer->user_data);
2524
2525 if (len == 0) {
2526 close(offer->fd);
2527 data_offer_destroy(offer);
2528 }
2529}
2530
2531static void
2532data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2533 data_func_t func, void *user_data)
2534{
2535 int p[2];
2536
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002537 if (pipe2(p, O_CLOEXEC) == -1)
2538 return;
2539
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002540 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2541 close(p[1]);
2542
2543 offer->io_task.run = offer_io_func;
2544 offer->fd = p[0];
2545 offer->func = func;
2546 offer->refcount++;
2547 offer->user_data = user_data;
2548
2549 display_watch_fd(offer->input->display,
2550 offer->fd, EPOLLIN, &offer->io_task);
2551}
2552
2553void
2554input_receive_drag_data(struct input *input, const char *mime_type,
2555 data_func_t func, void *data)
2556{
2557 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2558 input->drag_offer->x = input->sx;
2559 input->drag_offer->y = input->sy;
2560}
2561
2562int
2563input_receive_selection_data(struct input *input, const char *mime_type,
2564 data_func_t func, void *data)
2565{
2566 char **p;
2567
2568 if (input->selection_offer == NULL)
2569 return -1;
2570
2571 for (p = input->selection_offer->types.data; *p; p++)
2572 if (strcmp(mime_type, *p) == 0)
2573 break;
2574
2575 if (*p == NULL)
2576 return -1;
2577
2578 data_offer_receive_data(input->selection_offer,
2579 mime_type, func, data);
2580 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002581}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002582
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002583int
2584input_receive_selection_data_to_fd(struct input *input,
2585 const char *mime_type, int fd)
2586{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002587 if (input->selection_offer)
2588 wl_data_offer_receive(input->selection_offer->offer,
2589 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002590
2591 return 0;
2592}
2593
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002594void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002595window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002596{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002597 if (!window->shell_surface)
2598 return;
2599
Daniel Stone37816df2012-05-16 18:45:18 +01002600 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002601}
2602
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002603static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002604idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002605{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002606 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002607
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002608 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002609 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002610 widget_set_allocation(widget,
2611 window->pending_allocation.x,
2612 window->pending_allocation.y,
2613 window->pending_allocation.width,
2614 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002615
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002616 if (window->input_region) {
2617 wl_region_destroy(window->input_region);
2618 window->input_region = NULL;
2619 }
2620
2621 if (window->opaque_region) {
2622 wl_region_destroy(window->opaque_region);
2623 window->opaque_region = NULL;
2624 }
2625
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002626 if (widget->resize_handler)
2627 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002628 widget->allocation.width,
2629 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002630 widget->user_data);
2631
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002632 if (window->allocation.width != widget->allocation.width ||
2633 window->allocation.height != widget->allocation.height) {
2634 window->allocation = widget->allocation;
2635 window_schedule_redraw(window);
2636 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002637}
2638
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002639void
2640window_schedule_resize(struct window *window, int width, int height)
2641{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002642 window->pending_allocation.x = 0;
2643 window->pending_allocation.y = 0;
2644 window->pending_allocation.width = width;
2645 window->pending_allocation.height = height;
2646
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002647 if (window->min_allocation.width == 0)
2648 window->min_allocation = window->pending_allocation;
2649 if (window->pending_allocation.width < window->min_allocation.width)
2650 window->pending_allocation.width = window->min_allocation.width;
2651 if (window->pending_allocation.height < window->min_allocation.height)
2652 window->pending_allocation.height = window->min_allocation.height;
2653
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002654 window->resize_needed = 1;
2655 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002656}
2657
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002658void
2659widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2660{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002661 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002662}
2663
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002664static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002665handle_ping(void *data, struct wl_shell_surface *shell_surface,
2666 uint32_t serial)
2667{
2668 wl_shell_surface_pong(shell_surface, serial);
2669}
2670
2671static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002672handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002673 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002674{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002675 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002676
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002677 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002678 return;
2679
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002680 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002681 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002682}
2683
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002684static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002685menu_destroy(struct menu *menu)
2686{
2687 widget_destroy(menu->widget);
2688 window_destroy(menu->window);
2689 free(menu);
2690}
2691
2692static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002693handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2694{
2695 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002696 struct menu *menu = window->widget->user_data;
2697
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002698 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002699 * device. Or just use wl_callback. And this really needs to
2700 * be a window vfunc that the menu can set. And we need the
2701 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002702
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002703 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002704 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002705 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002706}
2707
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002708static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002709 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002710 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002711 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002712};
2713
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002714void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002715window_get_allocation(struct window *window,
2716 struct rectangle *allocation)
2717{
2718 *allocation = window->allocation;
2719}
2720
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002721static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002722widget_redraw(struct widget *widget)
2723{
2724 struct widget *child;
2725
2726 if (widget->redraw_handler)
2727 widget->redraw_handler(widget, widget->user_data);
2728 wl_list_for_each(child, &widget->child_list, link)
2729 widget_redraw(child);
2730}
2731
2732static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002733frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2734{
2735 struct window *window = data;
2736
2737 wl_callback_destroy(callback);
2738 window->redraw_scheduled = 0;
2739 if (window->redraw_needed)
2740 window_schedule_redraw(window);
2741}
2742
2743static const struct wl_callback_listener listener = {
2744 frame_callback
2745};
2746
2747static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002748idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002749{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002750 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002751 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002752
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002753 if (window->resize_needed)
2754 idle_resize(window);
2755
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002756 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002757 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002758 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002759 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002760 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002761
2762 callback = wl_surface_frame(window->surface);
2763 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002764}
2765
2766void
2767window_schedule_redraw(struct window *window)
2768{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002769 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002770 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002771 window->redraw_task.run = idle_redraw;
2772 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002773 window->redraw_scheduled = 1;
2774 }
2775}
2776
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002777void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002778window_set_fullscreen(struct window *window, int fullscreen)
2779{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002780 if (!window->display->shell)
2781 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002782
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002783 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002784 return;
2785
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002786 if (fullscreen) {
2787 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002788 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002789 wl_shell_surface_set_fullscreen(window->shell_surface,
2790 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2791 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002792 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002793 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002794 wl_shell_surface_set_toplevel(window->shell_surface);
2795 window_schedule_resize(window,
2796 window->saved_allocation.width,
2797 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002798 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002799}
2800
2801void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002802window_set_maximized(struct window *window, int maximized)
2803{
2804 if (!window->display->shell)
2805 return;
2806
2807 if ((window->type == TYPE_MAXIMIZED) == maximized)
2808 return;
2809
2810 if (window->type == TYPE_TOPLEVEL) {
2811 window->saved_allocation = window->allocation;
2812 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2813 window->type = TYPE_MAXIMIZED;
2814 } else {
2815 wl_shell_surface_set_toplevel(window->shell_surface);
2816 window->type = TYPE_TOPLEVEL;
2817 window_schedule_resize(window,
2818 window->saved_allocation.width,
2819 window->saved_allocation.height);
2820 }
2821}
2822
2823void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002824window_set_user_data(struct window *window, void *data)
2825{
2826 window->user_data = data;
2827}
2828
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002829void *
2830window_get_user_data(struct window *window)
2831{
2832 return window->user_data;
2833}
2834
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002835void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002836window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002837 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002838{
2839 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002840}
2841
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002842void
2843window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002844 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002845{
2846 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002847}
2848
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002849void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002850window_set_data_handler(struct window *window, window_data_handler_t handler)
2851{
2852 window->data_handler = handler;
2853}
2854
2855void
2856window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2857{
2858 window->drop_handler = handler;
2859}
2860
2861void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002862window_set_close_handler(struct window *window,
2863 window_close_handler_t handler)
2864{
2865 window->close_handler = handler;
2866}
2867
2868void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002869window_set_fullscreen_handler(struct window *window,
2870 window_fullscreen_handler_t handler)
2871{
2872 window->fullscreen_handler = handler;
2873}
2874
2875void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002876window_set_title(struct window *window, const char *title)
2877{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002878 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002879 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002880 if (window->shell_surface)
2881 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002882}
2883
2884const char *
2885window_get_title(struct window *window)
2886{
2887 return window->title;
2888}
2889
2890void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002891window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2892{
2893 struct text_cursor_position *text_cursor_position =
2894 window->display->text_cursor_position;
2895
Scott Moreau9295ce02012-06-01 12:46:10 -06002896 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002897 return;
2898
2899 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002900 window->surface,
2901 wl_fixed_from_int(x),
2902 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002903}
2904
2905void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002906window_damage(struct window *window, int32_t x, int32_t y,
2907 int32_t width, int32_t height)
2908{
2909 wl_surface_damage(window->surface, x, y, width, height);
2910}
2911
Casey Dahlin9074db52012-04-19 22:50:09 -04002912static void
2913surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002914 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002915{
Rob Bradford7507b572012-05-15 17:55:34 +01002916 struct window *window = data;
2917 struct output *output;
2918 struct output *output_found = NULL;
2919 struct window_output *window_output;
2920
2921 wl_list_for_each(output, &window->display->output_list, link) {
2922 if (output->output == wl_output) {
2923 output_found = output;
2924 break;
2925 }
2926 }
2927
2928 if (!output_found)
2929 return;
2930
2931 window_output = malloc (sizeof *window_output);
2932 window_output->output = output_found;
2933
2934 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002935}
2936
2937static void
2938surface_leave(void *data,
2939 struct wl_surface *wl_surface, struct wl_output *output)
2940{
Rob Bradford7507b572012-05-15 17:55:34 +01002941 struct window *window = data;
2942 struct window_output *window_output;
2943 struct window_output *window_output_found = NULL;
2944
2945 wl_list_for_each(window_output, &window->window_output_list, link) {
2946 if (window_output->output->output == output) {
2947 window_output_found = window_output;
2948 break;
2949 }
2950 }
2951
2952 if (window_output_found) {
2953 wl_list_remove(&window_output_found->link);
2954 free(window_output_found);
2955 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002956}
2957
2958static const struct wl_surface_listener surface_listener = {
2959 surface_enter,
2960 surface_leave
2961};
2962
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002963static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002964window_create_internal(struct display *display,
2965 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002966{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002967 struct window *window;
2968
2969 window = malloc(sizeof *window);
2970 if (window == NULL)
2971 return NULL;
2972
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002973 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002974 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002975 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002976 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002977 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002978 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002979 window->shell_surface =
2980 wl_shell_get_shell_surface(display->shell,
2981 window->surface);
2982 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002983 window->allocation.x = 0;
2984 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002985 window->allocation.width = 0;
2986 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002987 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002988 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002989 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002990 window->input_region = NULL;
2991 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002992
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002993 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002994#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002995 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002996#else
2997 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2998#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002999 else
3000 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003001
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003002 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003003 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003004 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003005
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003006 if (window->shell_surface) {
3007 wl_shell_surface_set_user_data(window->shell_surface, window);
3008 wl_shell_surface_add_listener(window->shell_surface,
3009 &shell_surface_listener, window);
3010 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003011
Rob Bradford7507b572012-05-15 17:55:34 +01003012 wl_list_init (&window->window_output_list);
3013
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003014 return window;
3015}
3016
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003017struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003018window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003019{
3020 struct window *window;
3021
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003022 window = window_create_internal(display, NULL, TYPE_NONE);
3023 if (!window)
3024 return NULL;
3025
3026 return window;
3027}
3028
3029struct window *
3030window_create_custom(struct display *display)
3031{
3032 struct window *window;
3033
3034 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003035 if (!window)
3036 return NULL;
3037
3038 return window;
3039}
3040
3041struct window *
3042window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003043 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003044{
3045 struct window *window;
3046
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003047 window = window_create_internal(parent->display,
3048 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003049 if (!window)
3050 return NULL;
3051
3052 window->x = x;
3053 window->y = y;
3054
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003055 if (display->shell)
3056 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003057 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003058 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003059
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003060 return window;
3061}
3062
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003063static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003064menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003065{
3066 int next;
3067
3068 next = (sy - 8) / 20;
3069 if (menu->current != next) {
3070 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003071 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003072 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003073}
3074
3075static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003076menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003077 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003078 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003079{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003080 struct menu *menu = data;
3081
3082 if (widget == menu->widget)
3083 menu_set_item(data, y);
3084
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003085 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003086}
3087
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003088static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003089menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003090 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003091{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003092 struct menu *menu = data;
3093
3094 if (widget == menu->widget)
3095 menu_set_item(data, y);
3096
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003097 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003098}
3099
3100static void
3101menu_leave_handler(struct widget *widget, struct input *input, void *data)
3102{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003103 struct menu *menu = data;
3104
3105 if (widget == menu->widget)
3106 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003107}
3108
3109static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003110menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003111 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003112 uint32_t button, enum wl_pointer_button_state state,
3113 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003114
3115{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003116 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003117
Daniel Stone4dbadb12012-05-30 16:31:51 +01003118 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3119 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003120 /* Either relase after press-drag-release or
3121 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003122 menu->func(menu->window->parent,
3123 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003124 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003125 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003126 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003127}
3128
3129static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003130menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003131{
3132 cairo_t *cr;
3133 const int32_t r = 3, margin = 3;
3134 struct menu *menu = data;
3135 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003136 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003137
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003138 cr = cairo_create(window->cairo_surface);
3139 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3140 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3141 cairo_paint(cr);
3142
3143 width = window->allocation.width;
3144 height = window->allocation.height;
3145 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003146
3147 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003148 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3149 cairo_fill(cr);
3150
3151 for (i = 0; i < menu->count; i++) {
3152 if (i == menu->current) {
3153 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3154 cairo_rectangle(cr, margin, i * 20 + margin,
3155 width - 2 * margin, 20);
3156 cairo_fill(cr);
3157 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3158 cairo_move_to(cr, 10, i * 20 + 16);
3159 cairo_show_text(cr, menu->entries[i]);
3160 } else {
3161 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3162 cairo_move_to(cr, 10, i * 20 + 16);
3163 cairo_show_text(cr, menu->entries[i]);
3164 }
3165 }
3166
3167 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003168}
3169
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003170void
3171window_show_menu(struct display *display,
3172 struct input *input, uint32_t time, struct window *parent,
3173 int32_t x, int32_t y,
3174 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003175{
3176 struct window *window;
3177 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003178 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003179
3180 menu = malloc(sizeof *menu);
3181 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003182 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003183
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003184 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003185 if (!window) {
3186 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003187 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003188 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003189
3190 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003191 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003192 menu->entries = entries;
3193 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003194 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003195 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003196 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003197 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003198 window->type = TYPE_MENU;
3199 window->x = x;
3200 window->y = y;
3201
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003202 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003203 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003204 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003205 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003206 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003207
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003208 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003209 widget_set_enter_handler(menu->widget, menu_enter_handler);
3210 widget_set_leave_handler(menu->widget, menu_leave_handler);
3211 widget_set_motion_handler(menu->widget, menu_motion_handler);
3212 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003213
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003214 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003215 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003216}
3217
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003218void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003219window_set_buffer_type(struct window *window, enum window_buffer_type type)
3220{
3221 window->buffer_type = type;
3222}
3223
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003224
3225static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003226display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003227 struct wl_output *wl_output,
3228 int x, int y,
3229 int physical_width,
3230 int physical_height,
3231 int subpixel,
3232 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003233 const char *model,
3234 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003235{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003236 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003237
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003238 output->allocation.x = x;
3239 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003240}
3241
3242static void
3243display_handle_mode(void *data,
3244 struct wl_output *wl_output,
3245 uint32_t flags,
3246 int width,
3247 int height,
3248 int refresh)
3249{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003250 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003251 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003252
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003253 if (flags & WL_OUTPUT_MODE_CURRENT) {
3254 output->allocation.width = width;
3255 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003256 if (display->output_configure_handler)
3257 (*display->output_configure_handler)(
3258 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003259 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003260}
3261
3262static const struct wl_output_listener output_listener = {
3263 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003264 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003265};
3266
3267static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003268display_add_output(struct display *d, uint32_t id)
3269{
3270 struct output *output;
3271
3272 output = malloc(sizeof *output);
3273 if (output == NULL)
3274 return;
3275
3276 memset(output, 0, sizeof *output);
3277 output->display = d;
3278 output->output =
3279 wl_display_bind(d->display, id, &wl_output_interface);
3280 wl_list_insert(d->output_list.prev, &output->link);
3281
3282 wl_output_add_listener(output->output, &output_listener, output);
3283}
3284
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003285static void
3286output_destroy(struct output *output)
3287{
3288 if (output->destroy_handler)
3289 (*output->destroy_handler)(output, output->user_data);
3290
3291 wl_output_destroy(output->output);
3292 wl_list_remove(&output->link);
3293 free(output);
3294}
3295
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003296void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003297display_set_output_configure_handler(struct display *display,
3298 display_output_handler_t handler)
3299{
3300 struct output *output;
3301
3302 display->output_configure_handler = handler;
3303 if (!handler)
3304 return;
3305
3306 wl_list_for_each(output, &display->output_list, link)
3307 (*display->output_configure_handler)(output,
3308 display->user_data);
3309}
3310
3311void
3312output_set_user_data(struct output *output, void *data)
3313{
3314 output->user_data = data;
3315}
3316
3317void *
3318output_get_user_data(struct output *output)
3319{
3320 return output->user_data;
3321}
3322
3323void
3324output_set_destroy_handler(struct output *output,
3325 display_output_handler_t handler)
3326{
3327 output->destroy_handler = handler;
3328 /* FIXME: implement this, once we have way to remove outputs */
3329}
3330
3331void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003332output_get_allocation(struct output *output, struct rectangle *allocation)
3333{
3334 *allocation = output->allocation;
3335}
3336
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003337struct wl_output *
3338output_get_wl_output(struct output *output)
3339{
3340 return output->output;
3341}
3342
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003343static void
Daniel Stone97f68542012-05-30 16:32:01 +01003344fini_xkb(struct input *input)
3345{
3346 xkb_state_unref(input->xkb.state);
3347 xkb_map_unref(input->xkb.keymap);
3348}
3349
3350static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003351display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003352{
3353 struct input *input;
3354
3355 input = malloc(sizeof *input);
3356 if (input == NULL)
3357 return;
3358
3359 memset(input, 0, sizeof *input);
3360 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003361 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003362 input->pointer_focus = NULL;
3363 input->keyboard_focus = NULL;
3364 wl_list_insert(d->input_list.prev, &input->link);
3365
Daniel Stone37816df2012-05-16 18:45:18 +01003366 wl_seat_add_listener(input->seat, &seat_listener, input);
3367 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003368
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369 input->data_device =
3370 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003371 input->seat);
3372 wl_data_device_add_listener(input->data_device, &data_device_listener,
3373 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003374
3375 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003376
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003377 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3378 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003379 input->repeat_task.run = keyboard_repeat_func;
3380 display_watch_fd(d, input->repeat_timer_fd,
3381 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003382}
3383
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003384static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003385input_destroy(struct input *input)
3386{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003387 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003388 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003389
3390 if (input->drag_offer)
3391 data_offer_destroy(input->drag_offer);
3392
3393 if (input->selection_offer)
3394 data_offer_destroy(input->selection_offer);
3395
3396 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003397 fini_xkb(input);
3398
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003399 wl_surface_destroy(input->pointer_surface);
3400
Pekka Paalanene1207c72011-12-16 12:02:09 +02003401 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003402 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003403 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003404 free(input);
3405}
3406
3407static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003408display_handle_global(struct wl_display *display, uint32_t id,
3409 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003410{
3411 struct display *d = data;
3412
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003413 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003414 d->compositor =
3415 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003416 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003417 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003418 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003419 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003420 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003421 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003422 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003423 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003424 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3425 d->data_device_manager =
3426 wl_display_bind(display, id,
3427 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003428 } else if (strcmp(interface, "text_cursor_position") == 0) {
3429 d->text_cursor_position =
3430 wl_display_bind(display, id,
3431 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003432 }
3433}
3434
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003435#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003436static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003437init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003438{
3439 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003440 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003441
Rob Clark6396ed32012-03-11 19:48:41 -05003442#ifdef USE_CAIRO_GLESV2
3443# define GL_BIT EGL_OPENGL_ES2_BIT
3444#else
3445# define GL_BIT EGL_OPENGL_BIT
3446#endif
3447
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003448 static const EGLint argb_cfg_attribs[] = {
3449 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003450 EGL_RED_SIZE, 1,
3451 EGL_GREEN_SIZE, 1,
3452 EGL_BLUE_SIZE, 1,
3453 EGL_ALPHA_SIZE, 1,
3454 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003455 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003456 EGL_NONE
3457 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003458
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003459#ifdef USE_CAIRO_GLESV2
3460 static const EGLint context_attribs[] = {
3461 EGL_CONTEXT_CLIENT_VERSION, 2,
3462 EGL_NONE
3463 };
3464 EGLint api = EGL_OPENGL_ES_API;
3465#else
3466 EGLint *context_attribs = NULL;
3467 EGLint api = EGL_OPENGL_API;
3468#endif
3469
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003470 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003471 if (!eglInitialize(d->dpy, &major, &minor)) {
3472 fprintf(stderr, "failed to initialize display\n");
3473 return -1;
3474 }
3475
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003476 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003477 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3478 return -1;
3479 }
3480
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003481 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3482 &d->argb_config, 1, &n) || n != 1) {
3483 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003484 return -1;
3485 }
3486
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003487 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003488 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003489 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003490 fprintf(stderr, "failed to create context\n");
3491 return -1;
3492 }
3493
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003494 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003495 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003496 return -1;
3497 }
3498
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003499#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003500 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3501 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3502 fprintf(stderr, "failed to get cairo egl argb device\n");
3503 return -1;
3504 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003505#endif
3506
3507 return 0;
3508}
3509
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003510static void
3511fini_egl(struct display *display)
3512{
3513#ifdef HAVE_CAIRO_EGL
3514 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003515#endif
3516
3517 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3518 EGL_NO_CONTEXT);
3519
3520 eglTerminate(display->dpy);
3521 eglReleaseThread();
3522}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003523#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003524
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003525static int
3526event_mask_update(uint32_t mask, void *data)
3527{
3528 struct display *d = data;
3529
3530 d->mask = mask;
3531
3532 return 0;
3533}
3534
3535static void
3536handle_display_data(struct task *task, uint32_t events)
3537{
3538 struct display *display =
3539 container_of(task, struct display, display_task);
3540
3541 wl_display_iterate(display->display, display->mask);
3542}
3543
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003544struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003545display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003546{
3547 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003548
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003549 d = malloc(sizeof *d);
3550 if (d == NULL)
3551 return NULL;
3552
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003553 memset(d, 0, sizeof *d);
3554
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003555 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003556 if (d->display == NULL) {
3557 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003558 return NULL;
3559 }
3560
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003561 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003562 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3563 d->display_task.run = handle_display_data;
3564 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3565
3566 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003567 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003568 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003569
Daniel Stone97f68542012-05-30 16:32:01 +01003570 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003571 if (d->xkb_context == NULL) {
3572 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003573 return NULL;
3574 }
3575
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003576 /* Set up listener so we'll catch all events. */
3577 wl_display_add_global_listener(d->display,
3578 display_handle_global, d);
3579
3580 /* Process connection events. */
3581 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003582#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003583 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003584 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003585#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003586
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003587 d->image_target_texture_2d =
3588 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3589 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3590 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3591
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003592 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003593
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003594 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003595
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003596 wl_list_init(&d->window_list);
3597
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003598 return d;
3599}
3600
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003601static void
3602display_destroy_outputs(struct display *display)
3603{
3604 struct output *tmp;
3605 struct output *output;
3606
3607 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3608 output_destroy(output);
3609}
3610
Pekka Paalanene1207c72011-12-16 12:02:09 +02003611static void
3612display_destroy_inputs(struct display *display)
3613{
3614 struct input *tmp;
3615 struct input *input;
3616
3617 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3618 input_destroy(input);
3619}
3620
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003621void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003622display_destroy(struct display *display)
3623{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003624 if (!wl_list_empty(&display->window_list))
3625 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3626
3627 if (!wl_list_empty(&display->deferred_list))
3628 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3629
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003630 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003631 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003632
Daniel Stone97f68542012-05-30 16:32:01 +01003633 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003634
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003635 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003636 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003637
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003638#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003639 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003640#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003641
Pekka Paalanenc2052982011-12-16 11:41:32 +02003642 if (display->shell)
3643 wl_shell_destroy(display->shell);
3644
3645 if (display->shm)
3646 wl_shm_destroy(display->shm);
3647
3648 if (display->data_device_manager)
3649 wl_data_device_manager_destroy(display->data_device_manager);
3650
3651 wl_compositor_destroy(display->compositor);
3652
3653 close(display->epoll_fd);
3654
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003655 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003656 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003657 free(display);
3658}
3659
3660void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003661display_set_user_data(struct display *display, void *data)
3662{
3663 display->user_data = data;
3664}
3665
3666void *
3667display_get_user_data(struct display *display)
3668{
3669 return display->user_data;
3670}
3671
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003672struct wl_display *
3673display_get_display(struct display *display)
3674{
3675 return display->display;
3676}
3677
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003678struct output *
3679display_get_output(struct display *display)
3680{
3681 return container_of(display->output_list.next, struct output, link);
3682}
3683
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003684struct wl_compositor *
3685display_get_compositor(struct display *display)
3686{
3687 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003688}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003689
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003690uint32_t
3691display_get_serial(struct display *display)
3692{
3693 return display->serial;
3694}
3695
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003696EGLDisplay
3697display_get_egl_display(struct display *d)
3698{
3699 return d->dpy;
3700}
3701
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003702struct wl_data_source *
3703display_create_data_source(struct display *display)
3704{
3705 return wl_data_device_manager_create_data_source(display->data_device_manager);
3706}
3707
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003708EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003709display_get_argb_egl_config(struct display *d)
3710{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003711 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003712}
3713
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003714struct wl_shell *
3715display_get_shell(struct display *display)
3716{
3717 return display->shell;
3718}
3719
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003720int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003721display_acquire_window_surface(struct display *display,
3722 struct window *window,
3723 EGLContext ctx)
3724{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003725#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003726 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003727 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003728
3729 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003730 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003731 device = cairo_surface_get_device(window->cairo_surface);
3732 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003733 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003734
Benjamin Franzke0c991632011-09-27 21:57:31 +02003735 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003736 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003737 ctx = display->argb_ctx;
3738 else
3739 assert(0);
3740 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003741
3742 data = cairo_surface_get_user_data(window->cairo_surface,
3743 &surface_data_key);
3744
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003745 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003746 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003747 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3748 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003749
3750 return 0;
3751#else
3752 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003753#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003754}
3755
3756void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003757display_release_window_surface(struct display *display,
3758 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003759{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003760#ifdef HAVE_CAIRO_EGL
3761 cairo_device_t *device;
3762
3763 device = cairo_surface_get_device(window->cairo_surface);
3764 if (!device)
3765 return;
3766
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003767 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003768 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003769 cairo_device_release(device);
3770#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003771}
3772
3773void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003774display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003775{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003776 wl_list_insert(&display->deferred_list, &task->link);
3777}
3778
3779void
3780display_watch_fd(struct display *display,
3781 int fd, uint32_t events, struct task *task)
3782{
3783 struct epoll_event ep;
3784
3785 ep.events = events;
3786 ep.data.ptr = task;
3787 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3788}
3789
3790void
3791display_run(struct display *display)
3792{
3793 struct task *task;
3794 struct epoll_event ep[16];
3795 int i, count;
3796
Pekka Paalanen826d7952011-12-15 10:14:07 +02003797 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003798 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003799 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003800
3801 while (!wl_list_empty(&display->deferred_list)) {
3802 task = container_of(display->deferred_list.next,
3803 struct task, link);
3804 wl_list_remove(&task->link);
3805 task->run(task, 0);
3806 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003807
3808 if (!display->running)
3809 break;
3810
3811 wl_display_flush(display->display);
3812
3813 count = epoll_wait(display->epoll_fd,
3814 ep, ARRAY_LENGTH(ep), -1);
3815 for (i = 0; i < count; i++) {
3816 task = ep[i].data.ptr;
3817 task->run(task, ep[i].events);
3818 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003819 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003820}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003821
3822void
3823display_exit(struct display *display)
3824{
3825 display->running = 0;
3826}