blob: 3262174810ee277cce38cbd498c130e8323424ad [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;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200182 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500184 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300185 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400186};
187
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400188struct input {
189 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100190 struct wl_seat *seat;
191 struct wl_pointer *pointer;
192 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 struct window *pointer_focus;
194 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300195 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300196 uint32_t cursor_anim_start;
197 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300198 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400199 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400200 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400201 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400202 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400203 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500205 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500206 struct widget *grab;
207 uint32_t grab_button;
208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400209 struct wl_data_device *data_device;
210 struct data_offer *drag_offer;
211 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100212
213 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100214 struct xkb_keymap *keymap;
215 struct xkb_state *state;
216 xkb_mod_mask_t control_mask;
217 xkb_mod_mask_t alt_mask;
218 xkb_mod_mask_t shift_mask;
219 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400220
221 struct task repeat_task;
222 int repeat_timer_fd;
223 uint32_t repeat_sym;
224 uint32_t repeat_key;
225 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400226};
227
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500228struct output {
229 struct display *display;
230 struct wl_output *output;
231 struct rectangle allocation;
232 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200233
234 display_output_handler_t destroy_handler;
235 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500236};
237
Martin Minarik1998b152012-05-10 02:04:35 +0200238enum frame_button_action {
239 FRAME_BUTTON_NULL = 0,
240 FRAME_BUTTON_ICON = 1,
241 FRAME_BUTTON_CLOSE = 2,
242 FRAME_BUTTON_MINIMIZE = 3,
243 FRAME_BUTTON_MAXIMIZE = 4,
244};
245
246enum frame_button_pointer {
247 FRAME_BUTTON_DEFAULT = 0,
248 FRAME_BUTTON_OVER = 1,
249 FRAME_BUTTON_ACTIVE = 2,
250};
251
252enum frame_button_align {
253 FRAME_BUTTON_RIGHT = 0,
254 FRAME_BUTTON_LEFT = 1,
255};
256
257enum frame_button_decoration {
258 FRAME_BUTTON_NONE = 0,
259 FRAME_BUTTON_FANCY = 1,
260};
261
262struct frame_button {
263 struct widget *widget;
264 struct frame *frame;
265 cairo_surface_t *icon;
266 enum frame_button_action type;
267 enum frame_button_pointer state;
268 struct wl_list link; /* buttons_list */
269 enum frame_button_align align;
270 enum frame_button_decoration decoration;
271};
272
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500273struct frame {
274 struct widget *widget;
275 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200276 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500277};
278
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500279struct menu {
280 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500281 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500282 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500283 const char **entries;
284 uint32_t time;
285 int current;
286 int count;
287 menu_func_t func;
288};
289
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300290struct tooltip {
291 struct widget *parent;
292 struct window *window;
293 struct widget *widget;
294 char *entry;
295 struct task tooltip_task;
296 int tooltip_fd;
297 float x, y;
298};
299
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700300struct shm_pool {
301 struct wl_shm_pool *pool;
302 size_t size;
303 size_t used;
304 void *data;
305};
306
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400307enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300308 CURSOR_DEFAULT = 100,
309 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400310};
311
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500312enum window_location {
313 WINDOW_INTERIOR = 0,
314 WINDOW_RESIZING_TOP = 1,
315 WINDOW_RESIZING_BOTTOM = 2,
316 WINDOW_RESIZING_LEFT = 4,
317 WINDOW_RESIZING_TOP_LEFT = 5,
318 WINDOW_RESIZING_BOTTOM_LEFT = 6,
319 WINDOW_RESIZING_RIGHT = 8,
320 WINDOW_RESIZING_TOP_RIGHT = 9,
321 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
322 WINDOW_RESIZING_MASK = 15,
323 WINDOW_EXTERIOR = 16,
324 WINDOW_TITLEBAR = 17,
325 WINDOW_CLIENT_AREA = 18,
326};
327
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328static const cairo_user_data_key_t surface_data_key;
329struct surface_data {
330 struct wl_buffer *buffer;
331};
332
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500333#define MULT(_d,c,a,t) \
334 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
335
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500336#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100338struct egl_window_surface_data {
339 struct display *display;
340 struct wl_surface *surface;
341 struct wl_egl_window *window;
342 EGLSurface surf;
343};
344
345static void
346egl_window_surface_data_destroy(void *p)
347{
348 struct egl_window_surface_data *data = p;
349 struct display *d = data->display;
350
351 eglDestroySurface(d->dpy, data->surf);
352 wl_egl_window_destroy(data->window);
353 data->surface = NULL;
354
355 free(p);
356}
357
358static cairo_surface_t *
359display_create_egl_window_surface(struct display *display,
360 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400361 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100362 struct rectangle *rectangle)
363{
364 cairo_surface_t *cairo_surface;
365 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200367 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100368
369 data = malloc(sizeof *data);
370 if (data == NULL)
371 return NULL;
372
373 data->display = display;
374 data->surface = surface;
375
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500376 config = display->argb_config;
377 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400379 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100382
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400383 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500384 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385
Benjamin Franzke0c991632011-09-27 21:57:31 +0200386 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387 data->surf,
388 rectangle->width,
389 rectangle->height);
390
391 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
392 data, egl_window_surface_data_destroy);
393
394 return cairo_surface;
395}
396
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397#endif
398
399struct wl_buffer *
400display_get_buffer_for_surface(struct display *display,
401 cairo_surface_t *surface)
402{
403 struct surface_data *data;
404
405 data = cairo_surface_get_user_data (surface, &surface_data_key);
406
407 return data->buffer;
408}
409
410struct shm_surface_data {
411 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700412 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413};
414
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500415static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700416shm_pool_destroy(struct shm_pool *pool);
417
418static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419shm_surface_data_destroy(void *p)
420{
421 struct shm_surface_data *data = p;
422
423 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700424 if (data->pool)
425 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300426
427 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428}
429
Kristian Høgsberg16626282012-04-03 11:21:27 -0400430static struct wl_shm_pool *
431make_shm_pool(struct display *display, int size, void **data)
432{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400433 struct wl_shm_pool *pool;
434 int fd;
435
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300436 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300438 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
439 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400440 return NULL;
441 }
442
443 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400444 if (*data == MAP_FAILED) {
445 fprintf(stderr, "mmap failed: %m\n");
446 close(fd);
447 return NULL;
448 }
449
450 pool = wl_shm_create_pool(display->shm, fd, size);
451
452 close(fd);
453
454 return pool;
455}
456
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700457static struct shm_pool *
458shm_pool_create(struct display *display, size_t size)
459{
460 struct shm_pool *pool = malloc(sizeof *pool);
461
462 if (!pool)
463 return NULL;
464
465 pool->pool = make_shm_pool(display, size, &pool->data);
466 if (!pool->pool) {
467 free(pool);
468 return NULL;
469 }
470
471 pool->size = size;
472 pool->used = 0;
473
474 return pool;
475}
476
477static void *
478shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
479{
480 if (pool->used + size > pool->size)
481 return NULL;
482
483 *offset = pool->used;
484 pool->used += size;
485
486 return (char *) pool->data + *offset;
487}
488
489/* destroy the pool. this does not unmap the memory though */
490static void
491shm_pool_destroy(struct shm_pool *pool)
492{
493 munmap(pool->data, pool->size);
494 wl_shm_pool_destroy(pool->pool);
495 free(pool);
496}
497
498/* Start allocating from the beginning of the pool again */
499static void
500shm_pool_reset(struct shm_pool *pool)
501{
502 pool->used = 0;
503}
504
505static int
506data_length_for_shm_surface(struct rectangle *rect)
507{
508 int stride;
509
510 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
511 rect->width);
512 return stride * rect->height;
513}
514
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500515static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700516display_create_shm_surface_from_pool(struct display *display,
517 struct rectangle *rectangle,
518 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519{
520 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400521 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400522 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400524 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400525
526 data = malloc(sizeof *data);
527 if (data == NULL)
528 return NULL;
529
530 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
531 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700532 length = stride * rectangle->height;
533 data->pool = NULL;
534 map = shm_pool_allocate(pool, length, &offset);
535
536 if (!map) {
537 free(data);
538 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400539 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400541 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542 CAIRO_FORMAT_ARGB32,
543 rectangle->width,
544 rectangle->height,
545 stride);
546
547 cairo_surface_set_user_data (surface, &surface_data_key,
548 data, shm_surface_data_destroy);
549
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500553 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400554
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700555 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400556 rectangle->width,
557 rectangle->height,
558 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700560 return surface;
561}
562
563static cairo_surface_t *
564display_create_shm_surface(struct display *display,
565 struct rectangle *rectangle, uint32_t flags,
566 struct window *window)
567{
568 struct shm_surface_data *data;
569 struct shm_pool *pool;
570 cairo_surface_t *surface;
571
572 if (window && window->pool) {
573 shm_pool_reset(window->pool);
574 surface = display_create_shm_surface_from_pool(display,
575 rectangle,
576 flags,
577 window->pool);
578 if (surface)
579 return surface;
580 }
581
582 pool = shm_pool_create(display,
583 data_length_for_shm_surface(rectangle));
584 if (!pool)
585 return NULL;
586
587 surface =
588 display_create_shm_surface_from_pool(display, rectangle,
589 flags, pool);
590
591 if (!surface) {
592 shm_pool_destroy(pool);
593 return NULL;
594 }
595
596 /* make sure we destroy the pool when the surface is destroyed */
597 data = cairo_surface_get_user_data(surface, &surface_data_key);
598 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599
600 return surface;
601}
602
nobled7b87cb02011-02-01 18:51:47 +0000603static int
604check_size(struct rectangle *rect)
605{
606 if (rect->width && rect->height)
607 return 0;
608
609 fprintf(stderr, "tried to create surface of "
610 "width: %d, height: %d\n", rect->width, rect->height);
611 return -1;
612}
613
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614cairo_surface_t *
615display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400617 struct rectangle *rectangle,
618 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400619{
nobled7b87cb02011-02-01 18:51:47 +0000620 if (check_size(rectangle) < 0)
621 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500622#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300623 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400624 return display_create_egl_window_surface(display,
625 surface,
626 flags,
627 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400629 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630}
631
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300632static const char *cursors[] = {
633 "bottom_left_corner",
634 "bottom_right_corner",
635 "bottom_side",
636 "grabbing",
637 "left_ptr",
638 "left_side",
639 "right_side",
640 "top_left_corner",
641 "top_right_corner",
642 "top_side",
643 "xterm",
644 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400645 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300646};
647
648static void
649create_cursors(struct display *display)
650{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100651 char *config_file;
652 char *theme = NULL;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653 unsigned int i;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100654 struct config_key shell_keys[] = {
655 { "cursor-theme", CONFIG_KEY_STRING, &theme },
656 };
657 struct config_section cs[] = {
658 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
659 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300660
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100661 config_file = config_file_path("weston.ini");
662 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
663 free(config_file);
664
665 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666 display->cursors =
667 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
668
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300669 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670 display->cursors[i] =
671 wl_cursor_theme_get_cursor(display->cursor_theme,
672 cursors[i]);
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300673 if (!display->cursors[i])
674 fprintf(stderr, "could not load cursor '%s'\n",
675 cursors[i]);
676 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300677}
678
679static void
680destroy_cursors(struct display *display)
681{
682 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800683 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300684}
685
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300686struct wl_cursor_image *
687display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300689 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300690 wl_cursor_theme_get_cursor(display->cursor_theme,
691 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300693 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694}
695
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400696static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200697window_get_resize_dx_dy(struct window *window, int *x, int *y)
698{
699 if (window->resize_edges & WINDOW_RESIZING_LEFT)
700 *x = window->server_allocation.width - window->allocation.width;
701 else
702 *x = 0;
703
704 if (window->resize_edges & WINDOW_RESIZING_TOP)
705 *y = window->server_allocation.height -
706 window->allocation.height;
707 else
708 *y = 0;
709
710 window->resize_edges = 0;
711}
712
713static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500714window_attach_surface(struct window *window)
715{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400716 struct display *display = window->display;
717 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000718#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000720#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500721 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400723 if (window->type == TYPE_NONE) {
724 window->type = TYPE_TOPLEVEL;
725 if (display->shell)
726 wl_shell_surface_set_toplevel(window->shell_surface);
727 }
728
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000730#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
732 data = cairo_surface_get_user_data(window->cairo_surface,
733 &surface_data_key);
734
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 cairo_gl_surface_swapbuffers(window->cairo_surface);
736 wl_egl_window_get_attached_size(data->window,
737 &window->server_allocation.width,
738 &window->server_allocation.height);
739 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000740#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 buffer =
743 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400744 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200746 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100747 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400748 wl_surface_damage(window->surface, 0, 0,
749 window->allocation.width,
750 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100751 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400752 cairo_surface_destroy(window->cairo_surface);
753 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100754 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000755 default:
756 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100757 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500758
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500759 if (window->input_region) {
760 wl_surface_set_input_region(window->surface,
761 window->input_region);
762 wl_region_destroy(window->input_region);
763 window->input_region = NULL;
764 }
765
766 if (window->opaque_region) {
767 wl_surface_set_opaque_region(window->surface,
768 window->opaque_region);
769 wl_region_destroy(window->opaque_region);
770 window->opaque_region = NULL;
771 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500772}
773
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400774int
775window_has_focus(struct window *window)
776{
777 return window->focus_count > 0;
778}
779
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500780void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400781window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500782{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400783 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100784 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500785}
786
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400787void
788window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400789{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500790 cairo_surface_reference(surface);
791
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400792 if (window->cairo_surface != NULL)
793 cairo_surface_destroy(window->cairo_surface);
794
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500795 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400796}
797
Benjamin Franzke22d54812011-07-16 19:50:32 +0000798#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799static void
800window_resize_cairo_window_surface(struct window *window)
801{
802 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200803 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100804
805 data = cairo_surface_get_user_data(window->cairo_surface,
806 &surface_data_key);
807
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200808 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 wl_egl_window_resize(data->window,
810 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200811 window->allocation.height,
812 x,y);
813
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 cairo_gl_surface_set_size(window->cairo_surface,
815 window->allocation.width,
816 window->allocation.height);
817}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400820struct display *
821window_get_display(struct window *window)
822{
823 return window->display;
824}
825
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400826void
827window_create_surface(struct window *window)
828{
829 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400830 uint32_t flags = 0;
831
832 if (!window->transparent)
833 flags = SURFACE_OPAQUE;
834
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500836#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
838 if (window->cairo_surface) {
839 window_resize_cairo_window_surface(window);
840 return;
841 }
842 surface = display_create_surface(window->display,
843 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400844 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846#endif
847 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400848 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400849 &window->allocation,
850 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800852 default:
853 surface = NULL;
854 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400855 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400856
857 window_set_surface(window, surface);
858 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859}
860
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200861static void frame_destroy(struct frame *frame);
862
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400863void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500864window_destroy(struct window *window)
865{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200866 struct display *display = window->display;
867 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100868 struct window_output *window_output;
869 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200870
871 if (window->redraw_scheduled)
872 wl_list_remove(&window->redraw_task.link);
873
874 wl_list_for_each(input, &display->input_list, link) {
875 if (input->pointer_focus == window)
876 input->pointer_focus = NULL;
877 if (input->keyboard_focus == window)
878 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500879 if (input->focus_widget &&
880 input->focus_widget->window == window)
881 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200882 }
883
Rob Bradford7507b572012-05-15 17:55:34 +0100884 wl_list_for_each_safe(window_output, window_output_tmp,
885 &window->window_output_list, link) {
886 free (window_output);
887 }
888
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500889 if (window->input_region)
890 wl_region_destroy(window->input_region);
891 if (window->opaque_region)
892 wl_region_destroy(window->opaque_region);
893
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200894 if (window->frame)
895 frame_destroy(window->frame);
896
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200897 if (window->shell_surface)
898 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500899 wl_surface_destroy(window->surface);
900 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200901
902 if (window->cairo_surface != NULL)
903 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200904
905 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500906 free(window);
907}
908
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500909static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500910widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400911{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500912 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400913
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500914 wl_list_for_each(child, &widget->child_list, link) {
915 target = widget_find_widget(child, x, y);
916 if (target)
917 return target;
918 }
919
920 if (widget->allocation.x <= x &&
921 x < widget->allocation.x + widget->allocation.width &&
922 widget->allocation.y <= y &&
923 y < widget->allocation.y + widget->allocation.height) {
924 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400925 }
926
927 return NULL;
928}
929
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500930static struct widget *
931widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400932{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500933 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400934
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500935 widget = malloc(sizeof *widget);
936 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500937 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500938 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500939 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500940 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500941 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300942 widget->tooltip = NULL;
943 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944
945 return widget;
946}
947
948struct widget *
949window_add_widget(struct window *window, void *data)
950{
951 window->widget = widget_create(window, data);
952 wl_list_init(&window->widget->link);
953
954 return window->widget;
955}
956
957struct widget *
958widget_add_widget(struct widget *parent, void *data)
959{
960 struct widget *widget;
961
962 widget = widget_create(parent->window, data);
963 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500965 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966}
967
968void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500969widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200971 struct display *display = widget->window->display;
972 struct input *input;
973
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300974 if (widget->tooltip) {
975 free(widget->tooltip);
976 widget->tooltip = NULL;
977 }
978
Pekka Paalanene156fb62012-01-19 13:51:38 +0200979 wl_list_for_each(input, &display->input_list, link) {
980 if (input->focus_widget == widget)
981 input->focus_widget = NULL;
982 }
983
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500984 wl_list_remove(&widget->link);
985 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400986}
987
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500989widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500991 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992}
993
994void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500995widget_set_size(struct widget *widget, int32_t width, int32_t height)
996{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500997 widget->allocation.width = width;
998 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500999}
1000
1001void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002widget_set_allocation(struct widget *widget,
1003 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001005 widget->allocation.x = x;
1006 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001007 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008}
1009
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001010void
1011widget_set_transparent(struct widget *widget, int transparent)
1012{
1013 widget->opaque = !transparent;
1014}
1015
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001016void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001017widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001019 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020}
1021
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001022void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001023widget_set_resize_handler(struct widget *widget,
1024 widget_resize_handler_t handler)
1025{
1026 widget->resize_handler = handler;
1027}
1028
1029void
1030widget_set_redraw_handler(struct widget *widget,
1031 widget_redraw_handler_t handler)
1032{
1033 widget->redraw_handler = handler;
1034}
1035
1036void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001037widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001038{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001039 widget->enter_handler = handler;
1040}
1041
1042void
1043widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1044{
1045 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001046}
1047
1048void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001049widget_set_motion_handler(struct widget *widget,
1050 widget_motion_handler_t handler)
1051{
1052 widget->motion_handler = handler;
1053}
1054
1055void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001056widget_set_button_handler(struct widget *widget,
1057 widget_button_handler_t handler)
1058{
1059 widget->button_handler = handler;
1060}
1061
1062void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001063widget_set_axis_handler(struct widget *widget,
1064 widget_axis_handler_t handler)
1065{
1066 widget->axis_handler = handler;
1067}
1068
1069void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001070widget_schedule_redraw(struct widget *widget)
1071{
1072 window_schedule_redraw(widget->window);
1073}
1074
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001075cairo_surface_t *
1076window_get_surface(struct window *window)
1077{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001078 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001079}
1080
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001081struct wl_surface *
1082window_get_wl_surface(struct window *window)
1083{
1084 return window->surface;
1085}
1086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087struct wl_shell_surface *
1088window_get_wl_shell_surface(struct window *window)
1089{
1090 return window->shell_surface;
1091}
1092
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001093static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001094tooltip_redraw_handler(struct widget *widget, void *data)
1095{
1096 cairo_t *cr;
1097 const int32_t r = 3;
1098 struct tooltip *tooltip = data;
1099 int32_t width, height;
1100 struct window *window = widget->window;
1101
1102 cr = cairo_create(window->cairo_surface);
1103 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1104 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1105 cairo_paint(cr);
1106
1107 width = window->allocation.width;
1108 height = window->allocation.height;
1109 rounded_rect(cr, 0, 0, width, height, r);
1110
1111 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1112 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1113 cairo_fill(cr);
1114
1115 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1116 cairo_move_to(cr, 10, 16);
1117 cairo_show_text(cr, tooltip->entry);
1118 cairo_destroy(cr);
1119}
1120
1121static cairo_text_extents_t
1122get_text_extents(struct tooltip *tooltip)
1123{
1124 struct window *window;
1125 cairo_t *cr;
1126 cairo_text_extents_t extents;
1127
1128 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1129 * created yet */
1130 window = tooltip->widget->window->parent;
1131 cr = cairo_create(window->cairo_surface);
1132 cairo_text_extents(cr, tooltip->entry, &extents);
1133 cairo_destroy(cr);
1134
1135 return extents;
1136}
1137
1138static int
1139window_create_tooltip(struct tooltip *tooltip)
1140{
1141 struct widget *parent = tooltip->parent;
1142 struct display *display = parent->window->display;
1143 struct window *window;
1144 const int offset_y = 27;
1145 const int margin = 3;
1146 cairo_text_extents_t extents;
1147
1148 if (tooltip->widget)
1149 return 0;
1150
1151 window = window_create_transient(display, parent->window, tooltip->x,
1152 tooltip->y + offset_y,
1153 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1154 if (!window)
1155 return -1;
1156
1157 tooltip->window = window;
1158 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1159
1160 extents = get_text_extents(tooltip);
1161 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1162 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1163
1164 return 0;
1165}
1166
1167void
1168widget_destroy_tooltip(struct widget *parent)
1169{
1170 struct tooltip *tooltip = parent->tooltip;
1171
1172 parent->tooltip_count = 0;
1173 if (!tooltip)
1174 return;
1175
1176 if (tooltip->widget) {
1177 widget_destroy(tooltip->widget);
1178 window_destroy(tooltip->window);
1179 tooltip->widget = NULL;
1180 tooltip->window = NULL;
1181 }
1182
1183 close(tooltip->tooltip_fd);
1184 free(tooltip->entry);
1185 free(tooltip);
1186 parent->tooltip = NULL;
1187}
1188
1189static void
1190tooltip_func(struct task *task, uint32_t events)
1191{
1192 struct tooltip *tooltip =
1193 container_of(task, struct tooltip, tooltip_task);
1194 uint64_t exp;
1195
Martin Olsson8df662a2012-07-08 03:03:47 +02001196 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1197 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001198 window_create_tooltip(tooltip);
1199}
1200
1201#define TOOLTIP_TIMEOUT 500
1202static int
1203tooltip_timer_reset(struct tooltip *tooltip)
1204{
1205 struct itimerspec its;
1206
1207 its.it_interval.tv_sec = 0;
1208 its.it_interval.tv_nsec = 0;
1209 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1210 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1211 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1212 fprintf(stderr, "could not set timerfd\n: %m");
1213 return -1;
1214 }
1215
1216 return 0;
1217}
1218
1219int
1220widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1221{
1222 struct tooltip *tooltip = parent->tooltip;
1223
1224 parent->tooltip_count++;
1225 if (tooltip) {
1226 tooltip->x = x;
1227 tooltip->y = y;
1228 tooltip_timer_reset(tooltip);
1229 return 0;
1230 }
1231
1232 /* the handler might be triggered too fast via input device motion, so
1233 * we need this check here to make sure tooltip is fully initialized */
1234 if (parent->tooltip_count > 1)
1235 return 0;
1236
1237 tooltip = malloc(sizeof *tooltip);
1238 if (!tooltip)
1239 return -1;
1240
1241 parent->tooltip = tooltip;
1242 tooltip->parent = parent;
1243 tooltip->widget = NULL;
1244 tooltip->window = NULL;
1245 tooltip->x = x;
1246 tooltip->y = y;
1247 tooltip->entry = strdup(entry);
1248 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1249 if (tooltip->tooltip_fd < 0) {
1250 fprintf(stderr, "could not create timerfd\n: %m");
1251 return -1;
1252 }
1253
1254 tooltip->tooltip_task.run = tooltip_func;
1255 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1256 EPOLLIN, &tooltip->tooltip_task);
1257 tooltip_timer_reset(tooltip);
1258
1259 return 0;
1260}
1261
1262static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263frame_resize_handler(struct widget *widget,
1264 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 struct frame *frame = data;
1267 struct widget *child = frame->child;
1268 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001270 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001271 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001272 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001274 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001276 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001277 decoration_width = (t->width + t->margin) * 2;
1278 decoration_height = t->width +
1279 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001280
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001281 allocation.x = t->width + t->margin;
1282 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001283 allocation.width = width - decoration_width;
1284 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001285
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001286 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001287
1288 wl_list_for_each(button, &frame->buttons_list, link)
1289 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001290 } else {
1291 decoration_width = 0;
1292 decoration_height = 0;
1293
1294 allocation.x = 0;
1295 allocation.y = 0;
1296 allocation.width = width;
1297 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001298 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001299
1300 wl_list_for_each(button, &frame->buttons_list, link)
1301 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001302 }
1303
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001304 widget_set_allocation(child, allocation.x, allocation.y,
1305 allocation.width, allocation.height);
1306
1307 if (child->resize_handler)
1308 child->resize_handler(child,
1309 allocation.width,
1310 allocation.height,
1311 child->user_data);
1312
Scott Moreauf7e498c2012-05-14 11:39:29 -06001313 width = child->allocation.width + decoration_width;
1314 height = child->allocation.height + decoration_height;
1315
Kristian Høgsberg023be102012-07-31 11:59:12 -04001316 if (widget->window->type != TYPE_FULLSCREEN) {
1317 widget->window->input_region =
1318 wl_compositor_create_region(display->compositor);
1319 wl_region_add(widget->window->input_region,
1320 t->margin, t->margin,
1321 width - 2 * t->margin,
1322 height - 2 * t->margin);
1323 }
1324
Scott Moreauf7e498c2012-05-14 11:39:29 -06001325 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001326
1327 if (child->opaque) {
1328 widget->window->opaque_region =
1329 wl_compositor_create_region(display->compositor);
1330 wl_region_add(widget->window->opaque_region,
1331 opaque_margin, opaque_margin,
1332 widget->allocation.width - 2 * opaque_margin,
1333 widget->allocation.height - 2 * opaque_margin);
1334 }
Martin Minarik1998b152012-05-10 02:04:35 +02001335
1336 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001337 x_r = frame->widget->allocation.width - t->width - t->margin;
1338 x_l = t->width + t->margin;
1339 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001340 wl_list_for_each(button, &frame->buttons_list, link) {
1341 const int button_padding = 4;
1342 w = cairo_image_surface_get_width(button->icon);
1343 h = cairo_image_surface_get_height(button->icon);
1344
1345 if (button->decoration == FRAME_BUTTON_FANCY)
1346 w += 10;
1347
1348 if (button->align == FRAME_BUTTON_LEFT) {
1349 widget_set_allocation(button->widget,
1350 x_l, y , w + 1, h + 1);
1351 x_l += w;
1352 x_l += button_padding;
1353 } else {
1354 x_r -= w;
1355 widget_set_allocation(button->widget,
1356 x_r, y , w + 1, h + 1);
1357 x_r -= button_padding;
1358 }
1359 }
1360}
1361
1362static int
1363frame_button_enter_handler(struct widget *widget,
1364 struct input *input, float x, float y, void *data)
1365{
1366 struct frame_button *frame_button = data;
1367
1368 widget_schedule_redraw(frame_button->widget);
1369 frame_button->state = FRAME_BUTTON_OVER;
1370
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001371 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001372}
1373
1374static void
1375frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1376{
1377 struct frame_button *frame_button = data;
1378
1379 widget_schedule_redraw(frame_button->widget);
1380 frame_button->state = FRAME_BUTTON_DEFAULT;
1381}
1382
1383static void
1384frame_button_button_handler(struct widget *widget,
1385 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001386 uint32_t button,
1387 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001388{
1389 struct frame_button *frame_button = data;
1390 struct window *window = widget->window;
1391
1392 if (button != BTN_LEFT)
1393 return;
1394
1395 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001396 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001397 frame_button->state = FRAME_BUTTON_ACTIVE;
1398 widget_schedule_redraw(frame_button->widget);
1399
1400 if (frame_button->type == FRAME_BUTTON_ICON)
1401 window_show_frame_menu(window, input, time);
1402 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001403 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001404 frame_button->state = FRAME_BUTTON_DEFAULT;
1405 widget_schedule_redraw(frame_button->widget);
1406 break;
1407 }
1408
1409 switch (frame_button->type) {
1410 case FRAME_BUTTON_CLOSE:
1411 if (window->close_handler)
1412 window->close_handler(window->parent,
1413 window->user_data);
1414 else
1415 display_exit(window->display);
1416 break;
1417 case FRAME_BUTTON_MINIMIZE:
1418 fprintf(stderr,"Minimize stub\n");
1419 break;
1420 case FRAME_BUTTON_MAXIMIZE:
1421 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1422 break;
1423 default:
1424 /* Unknown operation */
1425 break;
1426 }
1427}
1428
1429static void
1430frame_button_redraw_handler(struct widget *widget, void *data)
1431{
1432 struct frame_button *frame_button = data;
1433 cairo_t *cr;
1434 int width, height, x, y;
1435 struct window *window = widget->window;
1436
1437 x = widget->allocation.x;
1438 y = widget->allocation.y;
1439 width = widget->allocation.width;
1440 height = widget->allocation.height;
1441
1442 if (!width)
1443 return;
1444 if (!height)
1445 return;
1446 if (widget->opaque)
1447 return;
1448
1449 cr = cairo_create(window->cairo_surface);
1450
1451 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1452 cairo_set_line_width(cr, 1);
1453
1454 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1455 cairo_rectangle (cr, x, y, 25, 16);
1456
1457 cairo_stroke_preserve(cr);
1458
1459 switch (frame_button->state) {
1460 case FRAME_BUTTON_DEFAULT:
1461 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1462 break;
1463 case FRAME_BUTTON_OVER:
1464 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1465 break;
1466 case FRAME_BUTTON_ACTIVE:
1467 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1468 break;
1469 }
1470
1471 cairo_fill (cr);
1472
1473 x += 4;
1474 }
1475
1476 cairo_set_source_surface(cr, frame_button->icon, x, y);
1477 cairo_paint(cr);
1478
1479 cairo_destroy(cr);
1480}
1481
1482static struct widget *
1483frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1484 enum frame_button_align align, enum frame_button_decoration style)
1485{
1486 struct frame_button *frame_button;
1487 const char *icon = data;
1488
1489 frame_button = malloc (sizeof *frame_button);
1490 memset(frame_button, 0, sizeof *frame_button);
1491
1492 frame_button->icon = cairo_image_surface_create_from_png(icon);
1493 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1494 frame_button->frame = frame;
1495 frame_button->type = type;
1496 frame_button->align = align;
1497 frame_button->decoration = style;
1498
1499 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1500
1501 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1502 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1503 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1504 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1505 return frame_button->widget;
1506}
1507
1508static void
1509frame_button_destroy(struct frame_button *frame_button)
1510{
1511 widget_destroy(frame_button->widget);
1512 wl_list_remove(&frame_button->link);
1513 cairo_surface_destroy(frame_button->icon);
1514 free(frame_button);
1515
1516 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001517}
1518
1519static void
1520frame_redraw_handler(struct widget *widget, void *data)
1521{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001522 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001523 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001524 struct theme *t = window->display->theme;
1525 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001526
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001527 if (window->type == TYPE_FULLSCREEN)
1528 return;
1529
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001530 cr = cairo_create(window->cairo_surface);
1531
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001532 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001533 flags |= THEME_FRAME_ACTIVE;
1534 theme_render_frame(t, cr, widget->allocation.width,
1535 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001536
1537 cairo_destroy(cr);
1538}
1539
1540static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001543 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001544 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001545
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001546 location = theme_get_location(t, input->sx, input->sy,
1547 frame->widget->allocation.width,
1548 frame->widget->allocation.height);
1549
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001550 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001551 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001552 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001553 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001554 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001555 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001556 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001557 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001558 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001559 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001560 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001561 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001562 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001563 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001564 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001565 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001566 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001567 case THEME_LOCATION_EXTERIOR:
1568 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001569 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001570 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001571 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001572}
1573
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001574static void
1575frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001576{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001577 switch (index) {
1578 case 0: /* close */
1579 if (window->close_handler)
1580 window->close_handler(window->parent,
1581 window->user_data);
1582 else
1583 display_exit(window->display);
1584 break;
1585 case 1: /* fullscreen */
1586 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001587 if (window->fullscreen_handler)
1588 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590 }
1591}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001592
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001593void
1594window_show_frame_menu(struct window *window,
1595 struct input *input, uint32_t time)
1596{
1597 int32_t x, y;
1598
1599 static const char *entries[] = {
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001600 "Close", "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001601 };
1602
1603 input_get_position(input, &x, &y);
1604 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001605 x - 10, y - 10, frame_menu_func, entries,
1606 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001607}
1608
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609static int
1610frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001611 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612{
1613 return frame_get_pointer_image_for_location(data, input);
1614}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001615
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616static int
1617frame_motion_handler(struct widget *widget,
1618 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001619 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001620{
1621 return frame_get_pointer_image_for_location(data, input);
1622}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001623
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001624static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001625frame_button_handler(struct widget *widget,
1626 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001627 uint32_t button, enum wl_pointer_button_state state,
1628 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629
1630{
1631 struct frame *frame = data;
1632 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001633 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001634 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001635
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001636 location = theme_get_location(display->theme, input->sx, input->sy,
1637 frame->widget->allocation.width,
1638 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639
Daniel Stone4dbadb12012-05-30 16:31:51 +01001640 if (window->display->shell && button == BTN_LEFT &&
1641 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001643 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 if (!window->shell_surface)
1645 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001646 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001647 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001648 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001649 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001650 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001652 case THEME_LOCATION_RESIZING_TOP:
1653 case THEME_LOCATION_RESIZING_BOTTOM:
1654 case THEME_LOCATION_RESIZING_LEFT:
1655 case THEME_LOCATION_RESIZING_RIGHT:
1656 case THEME_LOCATION_RESIZING_TOP_LEFT:
1657 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1658 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1659 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660 if (!window->shell_surface)
1661 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001662 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001663
1664 if (!display->dpy) {
1665 /* If we're using shm, allocate a big
1666 pool to create buffers out of while
1667 we resize. We should probably base
1668 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001669 window->pool =
1670 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001671 }
1672
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001673 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001674 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001675 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001676 break;
1677 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001678 } else if (button == BTN_RIGHT &&
1679 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001680 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001681 }
1682}
1683
1684struct widget *
1685frame_create(struct window *window, void *data)
1686{
1687 struct frame *frame;
1688
1689 frame = malloc(sizeof *frame);
1690 memset(frame, 0, sizeof *frame);
1691
1692 frame->widget = window_add_widget(window, frame);
1693 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001694
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1696 widget_set_resize_handler(frame->widget, frame_resize_handler);
1697 widget_set_enter_handler(frame->widget, frame_enter_handler);
1698 widget_set_motion_handler(frame->widget, frame_motion_handler);
1699 widget_set_button_handler(frame->widget, frame_button_handler);
1700
Martin Minarik1998b152012-05-10 02:04:35 +02001701 /* Create empty list for frame buttons */
1702 wl_list_init(&frame->buttons_list);
1703
1704 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1705 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1706
1707 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1708 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1709
1710 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1711 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1712
1713 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1714 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1715
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001716 window->frame = frame;
1717
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001718 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001719}
1720
Kristian Høgsberga1627922012-06-20 17:30:03 -04001721void
1722frame_set_child_size(struct widget *widget, int child_width, int child_height)
1723{
1724 struct display *display = widget->window->display;
1725 struct theme *t = display->theme;
1726 int decoration_width, decoration_height;
1727 int width, height;
1728
1729 if (widget->window->type != TYPE_FULLSCREEN) {
1730 decoration_width = (t->width + t->margin) * 2;
1731 decoration_height = t->width +
1732 t->titlebar_height + t->margin * 2;
1733
1734 width = child_width + decoration_width;
1735 height = child_height + decoration_height;
1736 } else {
1737 width = child_width;
1738 height = child_height;
1739 }
1740
1741 window_schedule_resize(widget->window, width, height);
1742}
1743
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001744static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001745frame_destroy(struct frame *frame)
1746{
Martin Minarik1998b152012-05-10 02:04:35 +02001747 struct frame_button *button, *tmp;
1748
1749 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1750 frame_button_destroy(button);
1751
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001752 /* frame->child must be destroyed by the application */
1753 widget_destroy(frame->widget);
1754 free(frame);
1755}
1756
1757static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001758input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001759 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001760{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001762 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001763
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001764 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001765 return;
1766
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001767 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001768 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001769 widget = old;
1770 if (input->grab)
1771 widget = input->grab;
1772 if (widget->leave_handler)
1773 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001774 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001775 }
1776
1777 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 widget = focus;
1779 if (input->grab)
1780 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001781 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001782 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001783 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001784 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001785
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001786 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001787 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001788}
1789
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001790void
1791input_grab(struct input *input, struct widget *widget, uint32_t button)
1792{
1793 input->grab = widget;
1794 input->grab_button = button;
1795}
1796
1797void
1798input_ungrab(struct input *input)
1799{
1800 struct widget *widget;
1801
1802 input->grab = NULL;
1803 if (input->pointer_focus) {
1804 widget = widget_find_widget(input->pointer_focus->widget,
1805 input->sx, input->sy);
1806 input_set_focus_widget(input, widget, input->sx, input->sy);
1807 }
1808}
1809
1810static void
1811input_remove_pointer_focus(struct input *input)
1812{
1813 struct window *window = input->pointer_focus;
1814
1815 if (!window)
1816 return;
1817
1818 input_set_focus_widget(input, NULL, 0, 0);
1819
1820 input->pointer_focus = NULL;
1821 input->current_cursor = CURSOR_UNSET;
1822}
1823
1824static void
1825pointer_handle_enter(void *data, struct wl_pointer *pointer,
1826 uint32_t serial, struct wl_surface *surface,
1827 wl_fixed_t sx_w, wl_fixed_t sy_w)
1828{
1829 struct input *input = data;
1830 struct window *window;
1831 struct widget *widget;
1832 float sx = wl_fixed_to_double(sx_w);
1833 float sy = wl_fixed_to_double(sy_w);
1834
1835 if (!surface) {
1836 /* enter event for a window we've just destroyed */
1837 return;
1838 }
1839
1840 input->display->serial = serial;
1841 input->pointer_enter_serial = serial;
1842 input->pointer_focus = wl_surface_get_user_data(surface);
1843 window = input->pointer_focus;
1844
1845 if (window->pool) {
1846 shm_pool_destroy(window->pool);
1847 window->pool = NULL;
1848 /* Schedule a redraw to free the pool */
1849 window_schedule_redraw(window);
1850 }
1851
1852 input->sx = sx;
1853 input->sy = sy;
1854
1855 widget = widget_find_widget(window->widget, sx, sy);
1856 input_set_focus_widget(input, widget, sx, sy);
1857}
1858
1859static void
1860pointer_handle_leave(void *data, struct wl_pointer *pointer,
1861 uint32_t serial, struct wl_surface *surface)
1862{
1863 struct input *input = data;
1864
1865 input->display->serial = serial;
1866 input_remove_pointer_focus(input);
1867}
1868
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001869static void
Daniel Stone37816df2012-05-16 18:45:18 +01001870pointer_handle_motion(void *data, struct wl_pointer *pointer,
1871 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001872{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001873 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001874 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001875 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001876 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001877 float sx = wl_fixed_to_double(sx_w);
1878 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001879
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001880 input->sx = sx;
1881 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001882
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001883 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001884 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001886 }
1887
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001888 if (input->grab)
1889 widget = input->grab;
1890 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001891 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001892 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001893 cursor = widget->motion_handler(input->focus_widget,
1894 input, time, sx, sy,
1895 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001896
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001897 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001898}
1899
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001900static void
Daniel Stone37816df2012-05-16 18:45:18 +01001901pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001902 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001903{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001904 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001905 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001906 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001907
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001908 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001909 if (input->focus_widget && input->grab == NULL &&
1910 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001911 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001912
Neil Roberts6b28aad2012-01-23 19:11:18 +00001913 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001914 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001915 (*widget->button_handler)(widget,
1916 input, time,
1917 button, state,
1918 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001919
Daniel Stone4dbadb12012-05-30 16:31:51 +01001920 if (input->grab && input->grab_button == button &&
1921 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001922 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001923}
1924
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001925static void
Daniel Stone37816df2012-05-16 18:45:18 +01001926pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001927 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001928{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001929 struct input *input = data;
1930 struct widget *widget;
1931
1932 widget = input->focus_widget;
1933 if (input->grab)
1934 widget = input->grab;
1935 if (widget && widget->axis_handler)
1936 (*widget->axis_handler)(widget,
1937 input, time,
1938 axis, value,
1939 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06001940}
1941
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001942static const struct wl_pointer_listener pointer_listener = {
1943 pointer_handle_enter,
1944 pointer_handle_leave,
1945 pointer_handle_motion,
1946 pointer_handle_button,
1947 pointer_handle_axis,
1948};
1949
1950static void
1951input_remove_keyboard_focus(struct input *input)
1952{
1953 struct window *window = input->keyboard_focus;
1954 struct itimerspec its;
1955
1956 its.it_interval.tv_sec = 0;
1957 its.it_interval.tv_nsec = 0;
1958 its.it_value.tv_sec = 0;
1959 its.it_value.tv_nsec = 0;
1960 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1961
1962 if (!window)
1963 return;
1964
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001965 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001966 if (window->keyboard_focus_handler)
1967 (*window->keyboard_focus_handler)(window, NULL,
1968 window->user_data);
1969
1970 input->keyboard_focus = NULL;
1971}
1972
1973static void
1974keyboard_repeat_func(struct task *task, uint32_t events)
1975{
1976 struct input *input =
1977 container_of(task, struct input, repeat_task);
1978 struct window *window = input->keyboard_focus;
1979 uint64_t exp;
1980
1981 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1982 /* If we change the timer between the fd becoming
1983 * readable and getting here, there'll be nothing to
1984 * read and we get EAGAIN. */
1985 return;
1986
1987 if (window && window->key_handler) {
1988 (*window->key_handler)(window, input, input->repeat_time,
1989 input->repeat_key, input->repeat_sym,
1990 WL_KEYBOARD_KEY_STATE_PRESSED,
1991 window->user_data);
1992 }
1993}
1994
1995static void
1996keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1997 uint32_t format, int fd, uint32_t size)
1998{
1999 struct input *input = data;
2000 char *map_str;
2001
2002 if (!data) {
2003 close(fd);
2004 return;
2005 }
2006
2007 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2008 close(fd);
2009 return;
2010 }
2011
2012 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2013 if (map_str == MAP_FAILED) {
2014 close(fd);
2015 return;
2016 }
2017
2018 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2019 map_str,
2020 XKB_KEYMAP_FORMAT_TEXT_V1,
2021 0);
2022 munmap(map_str, size);
2023 close(fd);
2024
2025 if (!input->xkb.keymap) {
2026 fprintf(stderr, "failed to compile keymap\n");
2027 return;
2028 }
2029
2030 input->xkb.state = xkb_state_new(input->xkb.keymap);
2031 if (!input->xkb.state) {
2032 fprintf(stderr, "failed to create XKB state\n");
2033 xkb_map_unref(input->xkb.keymap);
2034 input->xkb.keymap = NULL;
2035 return;
2036 }
2037
2038 input->xkb.control_mask =
2039 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2040 input->xkb.alt_mask =
2041 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2042 input->xkb.shift_mask =
2043 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2044}
2045
2046static void
2047keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2048 uint32_t serial, struct wl_surface *surface,
2049 struct wl_array *keys)
2050{
2051 struct input *input = data;
2052 struct window *window;
2053
2054 input->display->serial = serial;
2055 input->keyboard_focus = wl_surface_get_user_data(surface);
2056
2057 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002058 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002059 if (window->keyboard_focus_handler)
2060 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002061 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002062}
2063
2064static void
2065keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2066 uint32_t serial, struct wl_surface *surface)
2067{
2068 struct input *input = data;
2069
2070 input->display->serial = serial;
2071 input_remove_keyboard_focus(input);
2072}
2073
Scott Moreau210d0792012-03-22 10:47:01 -06002074static void
Daniel Stone37816df2012-05-16 18:45:18 +01002075keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002076 uint32_t serial, uint32_t time, uint32_t key,
2077 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002078{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002079 struct input *input = data;
2080 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002081 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002082 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002083 const xkb_keysym_t *syms;
2084 xkb_keysym_t sym;
2085 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002086 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002087
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002088 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002089 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002090 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002091 return;
2092
Daniel Stone97f68542012-05-30 16:32:01 +01002093 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002094
Daniel Stone97f68542012-05-30 16:32:01 +01002095 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002096 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002097 XKB_STATE_LATCHED);
2098 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002099 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002100 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002101 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002102 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002103 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002104 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002105
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002106 sym = XKB_KEY_NoSymbol;
2107 if (num_syms == 1)
2108 sym = syms[0];
2109
2110 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002111 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002112 window_set_maximized(window,
2113 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002114 } else if (sym == XKB_KEY_F11 &&
2115 window->fullscreen_handler &&
2116 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2117 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002118 } else if (sym == XKB_KEY_F4 &&
2119 input->modifiers == MOD_ALT_MASK &&
2120 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2121 if (window->close_handler)
2122 window->close_handler(window->parent,
2123 window->user_data);
2124 else
2125 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002126 } else if (window->key_handler) {
2127 (*window->key_handler)(window, input, time, key,
2128 sym, state, window->user_data);
2129 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002130
2131 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2132 key == input->repeat_key) {
2133 its.it_interval.tv_sec = 0;
2134 its.it_interval.tv_nsec = 0;
2135 its.it_value.tv_sec = 0;
2136 its.it_value.tv_nsec = 0;
2137 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2138 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2139 input->repeat_sym = sym;
2140 input->repeat_key = key;
2141 input->repeat_time = time;
2142 its.it_interval.tv_sec = 0;
2143 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2144 its.it_value.tv_sec = 0;
2145 its.it_value.tv_nsec = 400 * 1000 * 1000;
2146 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2147 }
2148}
2149
2150static void
Daniel Stone351eb612012-05-31 15:27:47 -04002151keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2152 uint32_t serial, uint32_t mods_depressed,
2153 uint32_t mods_latched, uint32_t mods_locked,
2154 uint32_t group)
2155{
2156 struct input *input = data;
2157
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002158 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2159 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002160}
2161
Daniel Stone37816df2012-05-16 18:45:18 +01002162static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002163 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002164 keyboard_handle_enter,
2165 keyboard_handle_leave,
2166 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002167 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002168};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002169
2170static void
Daniel Stone37816df2012-05-16 18:45:18 +01002171seat_handle_capabilities(void *data, struct wl_seat *seat,
2172 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002173{
Daniel Stone37816df2012-05-16 18:45:18 +01002174 struct input *input = data;
2175
2176 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2177 input->pointer = wl_seat_get_pointer(seat);
2178 wl_pointer_set_user_data(input->pointer, input);
2179 wl_pointer_add_listener(input->pointer, &pointer_listener,
2180 input);
2181 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2182 wl_pointer_destroy(input->pointer);
2183 input->pointer = NULL;
2184 }
2185
2186 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2187 input->keyboard = wl_seat_get_keyboard(seat);
2188 wl_keyboard_set_user_data(input->keyboard, input);
2189 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2190 input);
2191 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2192 wl_keyboard_destroy(input->keyboard);
2193 input->keyboard = NULL;
2194 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002195}
2196
Daniel Stone37816df2012-05-16 18:45:18 +01002197static const struct wl_seat_listener seat_listener = {
2198 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002199};
2200
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002201void
2202input_get_position(struct input *input, int32_t *x, int32_t *y)
2203{
2204 *x = input->sx;
2205 *y = input->sy;
2206}
2207
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002208struct display *
2209input_get_display(struct input *input)
2210{
2211 return input->display;
2212}
2213
Daniel Stone37816df2012-05-16 18:45:18 +01002214struct wl_seat *
2215input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002216{
Daniel Stone37816df2012-05-16 18:45:18 +01002217 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002218}
2219
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002220uint32_t
2221input_get_modifiers(struct input *input)
2222{
2223 return input->modifiers;
2224}
2225
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002226struct widget *
2227input_get_focus_widget(struct input *input)
2228{
2229 return input->focus_widget;
2230}
2231
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002232struct data_offer {
2233 struct wl_data_offer *offer;
2234 struct input *input;
2235 struct wl_array types;
2236 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002237
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002238 struct task io_task;
2239 int fd;
2240 data_func_t func;
2241 int32_t x, y;
2242 void *user_data;
2243};
2244
2245static void
2246data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2247{
2248 struct data_offer *offer = data;
2249 char **p;
2250
2251 p = wl_array_add(&offer->types, sizeof *p);
2252 *p = strdup(type);
2253}
2254
2255static const struct wl_data_offer_listener data_offer_listener = {
2256 data_offer_offer,
2257};
2258
2259static void
2260data_offer_destroy(struct data_offer *offer)
2261{
2262 char **p;
2263
2264 offer->refcount--;
2265 if (offer->refcount == 0) {
2266 wl_data_offer_destroy(offer->offer);
2267 for (p = offer->types.data; *p; p++)
2268 free(*p);
2269 wl_array_release(&offer->types);
2270 free(offer);
2271 }
2272}
2273
2274static void
2275data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002276 struct wl_data_device *data_device,
2277 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002278{
2279 struct data_offer *offer;
2280
2281 offer = malloc(sizeof *offer);
2282
2283 wl_array_init(&offer->types);
2284 offer->refcount = 1;
2285 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002286 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002287 wl_data_offer_add_listener(offer->offer,
2288 &data_offer_listener, offer);
2289}
2290
2291static void
2292data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002293 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002294 wl_fixed_t x_w, wl_fixed_t y_w,
2295 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002296{
2297 struct input *input = data;
2298 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002299 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002300 float x = wl_fixed_to_double(x_w);
2301 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002302 char **p;
2303
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002304 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002305 window = wl_surface_get_user_data(surface);
2306 input->pointer_focus = window;
2307
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002308 if (offer) {
2309 input->drag_offer = wl_data_offer_get_user_data(offer);
2310
2311 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2312 *p = NULL;
2313
2314 types_data = input->drag_offer->types.data;
2315 } else {
2316 input->drag_offer = NULL;
2317 types_data = NULL;
2318 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002319
2320 window = input->pointer_focus;
2321 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002322 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002323 window->user_data);
2324}
2325
2326static void
2327data_device_leave(void *data, struct wl_data_device *data_device)
2328{
2329 struct input *input = data;
2330
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002331 if (input->drag_offer) {
2332 data_offer_destroy(input->drag_offer);
2333 input->drag_offer = NULL;
2334 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002335}
2336
2337static void
2338data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002339 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002340{
2341 struct input *input = data;
2342 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002343 float x = wl_fixed_to_double(x_w);
2344 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002345 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002346
2347 input->sx = x;
2348 input->sy = y;
2349
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002350 if (input->drag_offer)
2351 types_data = input->drag_offer->types.data;
2352 else
2353 types_data = NULL;
2354
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002355 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002356 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002357 window->user_data);
2358}
2359
2360static void
2361data_device_drop(void *data, struct wl_data_device *data_device)
2362{
2363 struct input *input = data;
2364 struct window *window = input->pointer_focus;
2365
2366 if (window->drop_handler)
2367 window->drop_handler(window, input,
2368 input->sx, input->sy, window->user_data);
2369}
2370
2371static void
2372data_device_selection(void *data,
2373 struct wl_data_device *wl_data_device,
2374 struct wl_data_offer *offer)
2375{
2376 struct input *input = data;
2377 char **p;
2378
2379 if (input->selection_offer)
2380 data_offer_destroy(input->selection_offer);
2381
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002382 if (offer) {
2383 input->selection_offer = wl_data_offer_get_user_data(offer);
2384 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2385 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002386 } else {
2387 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002388 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002389}
2390
2391static const struct wl_data_device_listener data_device_listener = {
2392 data_device_data_offer,
2393 data_device_enter,
2394 data_device_leave,
2395 data_device_motion,
2396 data_device_drop,
2397 data_device_selection
2398};
2399
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002400static void
2401input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002402{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002404 struct wl_cursor *cursor;
2405 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002406
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002407 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002408 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002409 return;
2410
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002411 if (index >= (int) cursor->image_count) {
2412 fprintf(stderr, "cursor index out of range\n");
2413 return;
2414 }
2415
2416 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002417 buffer = wl_cursor_image_get_buffer(image);
2418 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002419 return;
2420
Kristian Høgsbergae277372012-08-01 09:41:08 -04002421 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002422 input->pointer_surface,
2423 image->hotspot_x, image->hotspot_y);
2424 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2425 wl_surface_damage(input->pointer_surface, 0, 0,
2426 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002427}
2428
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002429static const struct wl_callback_listener pointer_surface_listener;
2430
2431static void
2432pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2433 uint32_t time)
2434{
2435 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002436 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002437 int i;
2438
2439 if (callback) {
2440 assert(callback == input->cursor_frame_cb);
2441 wl_callback_destroy(callback);
2442 input->cursor_frame_cb = NULL;
2443 }
2444
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002445 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002446 wl_pointer_set_cursor(input->pointer,
2447 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002448 NULL, 0, 0);
2449 return;
2450 }
2451
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002452 if (input->current_cursor == CURSOR_UNSET)
2453 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002454 cursor = input->display->cursors[input->current_cursor];
2455 if (!cursor)
2456 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002457
2458 /* FIXME We don't have the current time on the first call so we set
2459 * the animation start to the time of the first frame callback. */
2460 if (time == 0)
2461 input->cursor_anim_start = 0;
2462 else if (input->cursor_anim_start == 0)
2463 input->cursor_anim_start = time;
2464
2465 if (time == 0 || input->cursor_anim_start == 0)
2466 i = 0;
2467 else
2468 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2469
2470 input_set_pointer_image_index(input, i);
2471
2472 if (cursor->image_count == 1)
2473 return;
2474
2475 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2476 wl_callback_add_listener(input->cursor_frame_cb,
2477 &pointer_surface_listener, input);
2478}
2479
2480static const struct wl_callback_listener pointer_surface_listener = {
2481 pointer_surface_frame_callback
2482};
2483
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002484void
2485input_set_pointer_image(struct input *input, int pointer)
2486{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002487 int force = 0;
2488
2489 if (input->pointer_enter_serial > input->cursor_serial)
2490 force = 1;
2491
2492 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002493 return;
2494
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002495 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002496 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002497 if (!input->cursor_frame_cb)
2498 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002499 else if (force) {
2500 /* The current frame callback may be stuck if, for instance,
2501 * the set cursor request was processed by the server after
2502 * this client lost the focus. In this case the cursor surface
2503 * might not be mapped and the frame callback wouldn't ever
2504 * complete. Send a set_cursor and attach to try to map the
2505 * cursor surface again so that the callback will finish */
2506 input_set_pointer_image_index(input, 0);
2507 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002508}
2509
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002510struct wl_data_device *
2511input_get_data_device(struct input *input)
2512{
2513 return input->data_device;
2514}
2515
2516void
2517input_set_selection(struct input *input,
2518 struct wl_data_source *source, uint32_t time)
2519{
2520 wl_data_device_set_selection(input->data_device, source, time);
2521}
2522
2523void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002524input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002525{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002526 wl_data_offer_accept(input->drag_offer->offer,
2527 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002528}
2529
2530static void
2531offer_io_func(struct task *task, uint32_t events)
2532{
2533 struct data_offer *offer =
2534 container_of(task, struct data_offer, io_task);
2535 unsigned int len;
2536 char buffer[4096];
2537
2538 len = read(offer->fd, buffer, sizeof buffer);
2539 offer->func(buffer, len,
2540 offer->x, offer->y, offer->user_data);
2541
2542 if (len == 0) {
2543 close(offer->fd);
2544 data_offer_destroy(offer);
2545 }
2546}
2547
2548static void
2549data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2550 data_func_t func, void *user_data)
2551{
2552 int p[2];
2553
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002554 if (pipe2(p, O_CLOEXEC) == -1)
2555 return;
2556
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002557 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2558 close(p[1]);
2559
2560 offer->io_task.run = offer_io_func;
2561 offer->fd = p[0];
2562 offer->func = func;
2563 offer->refcount++;
2564 offer->user_data = user_data;
2565
2566 display_watch_fd(offer->input->display,
2567 offer->fd, EPOLLIN, &offer->io_task);
2568}
2569
2570void
2571input_receive_drag_data(struct input *input, const char *mime_type,
2572 data_func_t func, void *data)
2573{
2574 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2575 input->drag_offer->x = input->sx;
2576 input->drag_offer->y = input->sy;
2577}
2578
2579int
2580input_receive_selection_data(struct input *input, const char *mime_type,
2581 data_func_t func, void *data)
2582{
2583 char **p;
2584
2585 if (input->selection_offer == NULL)
2586 return -1;
2587
2588 for (p = input->selection_offer->types.data; *p; p++)
2589 if (strcmp(mime_type, *p) == 0)
2590 break;
2591
2592 if (*p == NULL)
2593 return -1;
2594
2595 data_offer_receive_data(input->selection_offer,
2596 mime_type, func, data);
2597 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002598}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002599
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002600int
2601input_receive_selection_data_to_fd(struct input *input,
2602 const char *mime_type, int fd)
2603{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002604 if (input->selection_offer)
2605 wl_data_offer_receive(input->selection_offer->offer,
2606 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002607
2608 return 0;
2609}
2610
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002611void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002612window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002613{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002614 if (!window->shell_surface)
2615 return;
2616
Daniel Stone37816df2012-05-16 18:45:18 +01002617 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002618}
2619
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002620static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002621idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002622{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002623 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002624
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002625 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002626 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002627 widget_set_allocation(widget,
2628 window->pending_allocation.x,
2629 window->pending_allocation.y,
2630 window->pending_allocation.width,
2631 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002632
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002633 if (window->input_region) {
2634 wl_region_destroy(window->input_region);
2635 window->input_region = NULL;
2636 }
2637
2638 if (window->opaque_region) {
2639 wl_region_destroy(window->opaque_region);
2640 window->opaque_region = NULL;
2641 }
2642
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002643 if (widget->resize_handler)
2644 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002645 widget->allocation.width,
2646 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002647 widget->user_data);
2648
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002649 if (window->allocation.width != widget->allocation.width ||
2650 window->allocation.height != widget->allocation.height) {
2651 window->allocation = widget->allocation;
2652 window_schedule_redraw(window);
2653 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002654}
2655
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002656void
2657window_schedule_resize(struct window *window, int width, int height)
2658{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002659 window->pending_allocation.x = 0;
2660 window->pending_allocation.y = 0;
2661 window->pending_allocation.width = width;
2662 window->pending_allocation.height = height;
2663
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002664 if (window->min_allocation.width == 0)
2665 window->min_allocation = window->pending_allocation;
2666 if (window->pending_allocation.width < window->min_allocation.width)
2667 window->pending_allocation.width = window->min_allocation.width;
2668 if (window->pending_allocation.height < window->min_allocation.height)
2669 window->pending_allocation.height = window->min_allocation.height;
2670
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002671 window->resize_needed = 1;
2672 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002673}
2674
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002675void
2676widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2677{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002678 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002679}
2680
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002681static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002682handle_ping(void *data, struct wl_shell_surface *shell_surface,
2683 uint32_t serial)
2684{
2685 wl_shell_surface_pong(shell_surface, serial);
2686}
2687
2688static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002689handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002690 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002691{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002692 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002693
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002694 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002695 return;
2696
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002697 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002698 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002699}
2700
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002701static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002702menu_destroy(struct menu *menu)
2703{
2704 widget_destroy(menu->widget);
2705 window_destroy(menu->window);
2706 free(menu);
2707}
2708
2709static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002710handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2711{
2712 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002713 struct menu *menu = window->widget->user_data;
2714
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002715 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002716 * device. Or just use wl_callback. And this really needs to
2717 * be a window vfunc that the menu can set. And we need the
2718 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002719
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002720 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002721 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002722 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002723}
2724
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002725static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002726 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002727 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002728 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002729};
2730
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002731void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002732window_get_allocation(struct window *window,
2733 struct rectangle *allocation)
2734{
2735 *allocation = window->allocation;
2736}
2737
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002738static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002739widget_redraw(struct widget *widget)
2740{
2741 struct widget *child;
2742
2743 if (widget->redraw_handler)
2744 widget->redraw_handler(widget, widget->user_data);
2745 wl_list_for_each(child, &widget->child_list, link)
2746 widget_redraw(child);
2747}
2748
2749static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002750frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2751{
2752 struct window *window = data;
2753
2754 wl_callback_destroy(callback);
2755 window->redraw_scheduled = 0;
2756 if (window->redraw_needed)
2757 window_schedule_redraw(window);
2758}
2759
2760static const struct wl_callback_listener listener = {
2761 frame_callback
2762};
2763
2764static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002765idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002766{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002767 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002768 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002769
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002770 if (window->resize_needed)
2771 idle_resize(window);
2772
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002773 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002774 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002775 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002776 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002777 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002778
2779 callback = wl_surface_frame(window->surface);
2780 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002781}
2782
2783void
2784window_schedule_redraw(struct window *window)
2785{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002786 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002787 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002788 window->redraw_task.run = idle_redraw;
2789 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002790 window->redraw_scheduled = 1;
2791 }
2792}
2793
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002794void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002795window_set_fullscreen(struct window *window, int fullscreen)
2796{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002797 if (!window->display->shell)
2798 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002799
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002800 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002801 return;
2802
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002803 if (fullscreen) {
2804 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002805 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002806 wl_shell_surface_set_fullscreen(window->shell_surface,
2807 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2808 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002809 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002810 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002811 wl_shell_surface_set_toplevel(window->shell_surface);
2812 window_schedule_resize(window,
2813 window->saved_allocation.width,
2814 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002815 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002816}
2817
2818void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002819window_set_maximized(struct window *window, int maximized)
2820{
2821 if (!window->display->shell)
2822 return;
2823
2824 if ((window->type == TYPE_MAXIMIZED) == maximized)
2825 return;
2826
2827 if (window->type == TYPE_TOPLEVEL) {
2828 window->saved_allocation = window->allocation;
2829 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2830 window->type = TYPE_MAXIMIZED;
2831 } else {
2832 wl_shell_surface_set_toplevel(window->shell_surface);
2833 window->type = TYPE_TOPLEVEL;
2834 window_schedule_resize(window,
2835 window->saved_allocation.width,
2836 window->saved_allocation.height);
2837 }
2838}
2839
2840void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002841window_set_user_data(struct window *window, void *data)
2842{
2843 window->user_data = data;
2844}
2845
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002846void *
2847window_get_user_data(struct window *window)
2848{
2849 return window->user_data;
2850}
2851
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002852void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002853window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002854 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002855{
2856 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002857}
2858
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002859void
2860window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002861 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002862{
2863 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002864}
2865
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002866void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002867window_set_data_handler(struct window *window, window_data_handler_t handler)
2868{
2869 window->data_handler = handler;
2870}
2871
2872void
2873window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2874{
2875 window->drop_handler = handler;
2876}
2877
2878void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002879window_set_close_handler(struct window *window,
2880 window_close_handler_t handler)
2881{
2882 window->close_handler = handler;
2883}
2884
2885void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002886window_set_fullscreen_handler(struct window *window,
2887 window_fullscreen_handler_t handler)
2888{
2889 window->fullscreen_handler = handler;
2890}
2891
2892void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002893window_set_title(struct window *window, const char *title)
2894{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002895 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002896 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002897 if (window->shell_surface)
2898 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002899}
2900
2901const char *
2902window_get_title(struct window *window)
2903{
2904 return window->title;
2905}
2906
2907void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002908window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2909{
2910 struct text_cursor_position *text_cursor_position =
2911 window->display->text_cursor_position;
2912
Scott Moreau9295ce02012-06-01 12:46:10 -06002913 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002914 return;
2915
2916 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002917 window->surface,
2918 wl_fixed_from_int(x),
2919 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002920}
2921
2922void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002923window_damage(struct window *window, int32_t x, int32_t y,
2924 int32_t width, int32_t height)
2925{
2926 wl_surface_damage(window->surface, x, y, width, height);
2927}
2928
Casey Dahlin9074db52012-04-19 22:50:09 -04002929static void
2930surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002931 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002932{
Rob Bradford7507b572012-05-15 17:55:34 +01002933 struct window *window = data;
2934 struct output *output;
2935 struct output *output_found = NULL;
2936 struct window_output *window_output;
2937
2938 wl_list_for_each(output, &window->display->output_list, link) {
2939 if (output->output == wl_output) {
2940 output_found = output;
2941 break;
2942 }
2943 }
2944
2945 if (!output_found)
2946 return;
2947
2948 window_output = malloc (sizeof *window_output);
2949 window_output->output = output_found;
2950
2951 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002952}
2953
2954static void
2955surface_leave(void *data,
2956 struct wl_surface *wl_surface, struct wl_output *output)
2957{
Rob Bradford7507b572012-05-15 17:55:34 +01002958 struct window *window = data;
2959 struct window_output *window_output;
2960 struct window_output *window_output_found = NULL;
2961
2962 wl_list_for_each(window_output, &window->window_output_list, link) {
2963 if (window_output->output->output == output) {
2964 window_output_found = window_output;
2965 break;
2966 }
2967 }
2968
2969 if (window_output_found) {
2970 wl_list_remove(&window_output_found->link);
2971 free(window_output_found);
2972 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002973}
2974
2975static const struct wl_surface_listener surface_listener = {
2976 surface_enter,
2977 surface_leave
2978};
2979
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002980static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002981window_create_internal(struct display *display,
2982 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002983{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002984 struct window *window;
2985
2986 window = malloc(sizeof *window);
2987 if (window == NULL)
2988 return NULL;
2989
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002990 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002991 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002992 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002993 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002994 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002995 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002996 window->shell_surface =
2997 wl_shell_get_shell_surface(display->shell,
2998 window->surface);
2999 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003000 window->allocation.x = 0;
3001 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003002 window->allocation.width = 0;
3003 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003004 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003005 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003006 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003007 window->input_region = NULL;
3008 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003009
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003010 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003011#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003012 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003013#else
3014 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3015#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003016 else
3017 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003018
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003019 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003020 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003021 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003022
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003023 if (window->shell_surface) {
3024 wl_shell_surface_set_user_data(window->shell_surface, window);
3025 wl_shell_surface_add_listener(window->shell_surface,
3026 &shell_surface_listener, window);
3027 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003028
Rob Bradford7507b572012-05-15 17:55:34 +01003029 wl_list_init (&window->window_output_list);
3030
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003031 return window;
3032}
3033
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003034struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003035window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003036{
3037 struct window *window;
3038
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003039 window = window_create_internal(display, NULL, TYPE_NONE);
3040 if (!window)
3041 return NULL;
3042
3043 return window;
3044}
3045
3046struct window *
3047window_create_custom(struct display *display)
3048{
3049 struct window *window;
3050
3051 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003052 if (!window)
3053 return NULL;
3054
3055 return window;
3056}
3057
3058struct window *
3059window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003060 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003061{
3062 struct window *window;
3063
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003064 window = window_create_internal(parent->display,
3065 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003066 if (!window)
3067 return NULL;
3068
3069 window->x = x;
3070 window->y = y;
3071
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003072 if (display->shell)
3073 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003074 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003075 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003076
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003077 return window;
3078}
3079
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003080static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003081menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003082{
3083 int next;
3084
3085 next = (sy - 8) / 20;
3086 if (menu->current != next) {
3087 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003088 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003089 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003090}
3091
3092static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003093menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003094 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003095 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003096{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003097 struct menu *menu = data;
3098
3099 if (widget == menu->widget)
3100 menu_set_item(data, y);
3101
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003102 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003103}
3104
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003105static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003106menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003107 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003108{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003109 struct menu *menu = data;
3110
3111 if (widget == menu->widget)
3112 menu_set_item(data, y);
3113
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003114 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003115}
3116
3117static void
3118menu_leave_handler(struct widget *widget, struct input *input, void *data)
3119{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003120 struct menu *menu = data;
3121
3122 if (widget == menu->widget)
3123 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003124}
3125
3126static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003127menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003128 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003129 uint32_t button, enum wl_pointer_button_state state,
3130 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003131
3132{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003133 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003134
Daniel Stone4dbadb12012-05-30 16:31:51 +01003135 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3136 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003137 /* Either relase after press-drag-release or
3138 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003139 menu->func(menu->window->parent,
3140 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003141 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003142 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003143 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003144}
3145
3146static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003147menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003148{
3149 cairo_t *cr;
3150 const int32_t r = 3, margin = 3;
3151 struct menu *menu = data;
3152 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003153 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003154
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003155 cr = cairo_create(window->cairo_surface);
3156 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3157 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3158 cairo_paint(cr);
3159
3160 width = window->allocation.width;
3161 height = window->allocation.height;
3162 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003163
3164 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003165 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3166 cairo_fill(cr);
3167
3168 for (i = 0; i < menu->count; i++) {
3169 if (i == menu->current) {
3170 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3171 cairo_rectangle(cr, margin, i * 20 + margin,
3172 width - 2 * margin, 20);
3173 cairo_fill(cr);
3174 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3175 cairo_move_to(cr, 10, i * 20 + 16);
3176 cairo_show_text(cr, menu->entries[i]);
3177 } else {
3178 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3179 cairo_move_to(cr, 10, i * 20 + 16);
3180 cairo_show_text(cr, menu->entries[i]);
3181 }
3182 }
3183
3184 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003185}
3186
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003187void
3188window_show_menu(struct display *display,
3189 struct input *input, uint32_t time, struct window *parent,
3190 int32_t x, int32_t y,
3191 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003192{
3193 struct window *window;
3194 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003195 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003196
3197 menu = malloc(sizeof *menu);
3198 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003199 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003200
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003201 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003202 if (!window) {
3203 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003204 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003205 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003206
3207 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003208 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003209 menu->entries = entries;
3210 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003211 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003212 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003213 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003214 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003215 window->type = TYPE_MENU;
3216 window->x = x;
3217 window->y = y;
3218
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003219 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003220 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003221 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003222 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003223 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003224
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003225 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003226 widget_set_enter_handler(menu->widget, menu_enter_handler);
3227 widget_set_leave_handler(menu->widget, menu_leave_handler);
3228 widget_set_motion_handler(menu->widget, menu_motion_handler);
3229 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003230
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003231 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003232 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003233}
3234
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003235void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003236window_set_buffer_type(struct window *window, enum window_buffer_type type)
3237{
3238 window->buffer_type = type;
3239}
3240
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003241
3242static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003243display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003244 struct wl_output *wl_output,
3245 int x, int y,
3246 int physical_width,
3247 int physical_height,
3248 int subpixel,
3249 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003250 const char *model,
3251 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003252{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003253 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003254
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003255 output->allocation.x = x;
3256 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003257}
3258
3259static void
3260display_handle_mode(void *data,
3261 struct wl_output *wl_output,
3262 uint32_t flags,
3263 int width,
3264 int height,
3265 int refresh)
3266{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003267 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003268 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003269
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003270 if (flags & WL_OUTPUT_MODE_CURRENT) {
3271 output->allocation.width = width;
3272 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003273 if (display->output_configure_handler)
3274 (*display->output_configure_handler)(
3275 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003276 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003277}
3278
3279static const struct wl_output_listener output_listener = {
3280 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003281 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003282};
3283
3284static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003285display_add_output(struct display *d, uint32_t id)
3286{
3287 struct output *output;
3288
3289 output = malloc(sizeof *output);
3290 if (output == NULL)
3291 return;
3292
3293 memset(output, 0, sizeof *output);
3294 output->display = d;
3295 output->output =
3296 wl_display_bind(d->display, id, &wl_output_interface);
3297 wl_list_insert(d->output_list.prev, &output->link);
3298
3299 wl_output_add_listener(output->output, &output_listener, output);
3300}
3301
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003302static void
3303output_destroy(struct output *output)
3304{
3305 if (output->destroy_handler)
3306 (*output->destroy_handler)(output, output->user_data);
3307
3308 wl_output_destroy(output->output);
3309 wl_list_remove(&output->link);
3310 free(output);
3311}
3312
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003313void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003314display_set_output_configure_handler(struct display *display,
3315 display_output_handler_t handler)
3316{
3317 struct output *output;
3318
3319 display->output_configure_handler = handler;
3320 if (!handler)
3321 return;
3322
3323 wl_list_for_each(output, &display->output_list, link)
3324 (*display->output_configure_handler)(output,
3325 display->user_data);
3326}
3327
3328void
3329output_set_user_data(struct output *output, void *data)
3330{
3331 output->user_data = data;
3332}
3333
3334void *
3335output_get_user_data(struct output *output)
3336{
3337 return output->user_data;
3338}
3339
3340void
3341output_set_destroy_handler(struct output *output,
3342 display_output_handler_t handler)
3343{
3344 output->destroy_handler = handler;
3345 /* FIXME: implement this, once we have way to remove outputs */
3346}
3347
3348void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003349output_get_allocation(struct output *output, struct rectangle *allocation)
3350{
3351 *allocation = output->allocation;
3352}
3353
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003354struct wl_output *
3355output_get_wl_output(struct output *output)
3356{
3357 return output->output;
3358}
3359
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003360static void
Daniel Stone97f68542012-05-30 16:32:01 +01003361fini_xkb(struct input *input)
3362{
3363 xkb_state_unref(input->xkb.state);
3364 xkb_map_unref(input->xkb.keymap);
3365}
3366
3367static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003368display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003369{
3370 struct input *input;
3371
3372 input = malloc(sizeof *input);
3373 if (input == NULL)
3374 return;
3375
3376 memset(input, 0, sizeof *input);
3377 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003378 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003379 input->pointer_focus = NULL;
3380 input->keyboard_focus = NULL;
3381 wl_list_insert(d->input_list.prev, &input->link);
3382
Daniel Stone37816df2012-05-16 18:45:18 +01003383 wl_seat_add_listener(input->seat, &seat_listener, input);
3384 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003385
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386 input->data_device =
3387 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003388 input->seat);
3389 wl_data_device_add_listener(input->data_device, &data_device_listener,
3390 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003391
3392 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003393
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003394 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3395 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003396 input->repeat_task.run = keyboard_repeat_func;
3397 display_watch_fd(d, input->repeat_timer_fd,
3398 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003399}
3400
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003401static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003402input_destroy(struct input *input)
3403{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003404 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003405 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003406
3407 if (input->drag_offer)
3408 data_offer_destroy(input->drag_offer);
3409
3410 if (input->selection_offer)
3411 data_offer_destroy(input->selection_offer);
3412
3413 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003414 fini_xkb(input);
3415
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003416 wl_surface_destroy(input->pointer_surface);
3417
Pekka Paalanene1207c72011-12-16 12:02:09 +02003418 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003419 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003420 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003421 free(input);
3422}
3423
3424static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003425display_handle_global(struct wl_display *display, uint32_t id,
3426 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003427{
3428 struct display *d = data;
3429
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003430 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003431 d->compositor =
3432 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003433 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003434 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003435 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003436 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003437 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003438 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003439 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003440 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003441 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3442 d->data_device_manager =
3443 wl_display_bind(display, id,
3444 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003445 } else if (strcmp(interface, "text_cursor_position") == 0) {
3446 d->text_cursor_position =
3447 wl_display_bind(display, id,
3448 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003449 }
3450}
3451
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003452#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003453static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003454init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003455{
3456 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003457 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003458
Rob Clark6396ed32012-03-11 19:48:41 -05003459#ifdef USE_CAIRO_GLESV2
3460# define GL_BIT EGL_OPENGL_ES2_BIT
3461#else
3462# define GL_BIT EGL_OPENGL_BIT
3463#endif
3464
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003465 static const EGLint argb_cfg_attribs[] = {
3466 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003467 EGL_RED_SIZE, 1,
3468 EGL_GREEN_SIZE, 1,
3469 EGL_BLUE_SIZE, 1,
3470 EGL_ALPHA_SIZE, 1,
3471 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003472 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003473 EGL_NONE
3474 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003475
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003476#ifdef USE_CAIRO_GLESV2
3477 static const EGLint context_attribs[] = {
3478 EGL_CONTEXT_CLIENT_VERSION, 2,
3479 EGL_NONE
3480 };
3481 EGLint api = EGL_OPENGL_ES_API;
3482#else
3483 EGLint *context_attribs = NULL;
3484 EGLint api = EGL_OPENGL_API;
3485#endif
3486
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003487 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003488 if (!eglInitialize(d->dpy, &major, &minor)) {
3489 fprintf(stderr, "failed to initialize display\n");
3490 return -1;
3491 }
3492
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003493 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003494 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3495 return -1;
3496 }
3497
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003498 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3499 &d->argb_config, 1, &n) || n != 1) {
3500 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003501 return -1;
3502 }
3503
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003504 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003505 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003506 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003507 fprintf(stderr, "failed to create context\n");
3508 return -1;
3509 }
3510
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003511 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003512 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003513 return -1;
3514 }
3515
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003516#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003517 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3518 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3519 fprintf(stderr, "failed to get cairo egl argb device\n");
3520 return -1;
3521 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003522#endif
3523
3524 return 0;
3525}
3526
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003527static void
3528fini_egl(struct display *display)
3529{
3530#ifdef HAVE_CAIRO_EGL
3531 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003532#endif
3533
3534 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3535 EGL_NO_CONTEXT);
3536
3537 eglTerminate(display->dpy);
3538 eglReleaseThread();
3539}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003540#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003541
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003542static int
3543event_mask_update(uint32_t mask, void *data)
3544{
3545 struct display *d = data;
3546
3547 d->mask = mask;
3548
3549 return 0;
3550}
3551
3552static void
3553handle_display_data(struct task *task, uint32_t events)
3554{
3555 struct display *display =
3556 container_of(task, struct display, display_task);
3557
3558 wl_display_iterate(display->display, display->mask);
3559}
3560
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003561struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003562display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003563{
3564 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003565
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003566 d = malloc(sizeof *d);
3567 if (d == NULL)
3568 return NULL;
3569
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003570 memset(d, 0, sizeof *d);
3571
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003572 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003573 if (d->display == NULL) {
3574 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003575 return NULL;
3576 }
3577
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003578 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003579 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3580 d->display_task.run = handle_display_data;
3581 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3582
3583 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003584 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003585 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003586
Daniel Stone97f68542012-05-30 16:32:01 +01003587 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003588 if (d->xkb_context == NULL) {
3589 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003590 return NULL;
3591 }
3592
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003593 /* Set up listener so we'll catch all events. */
3594 wl_display_add_global_listener(d->display,
3595 display_handle_global, d);
3596
3597 /* Process connection events. */
3598 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003599#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003600 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003601 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003602#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003603
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003604 d->image_target_texture_2d =
3605 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3606 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3607 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3608
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003609 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003610
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003611 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003612
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003613 wl_list_init(&d->window_list);
3614
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003615 return d;
3616}
3617
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003618static void
3619display_destroy_outputs(struct display *display)
3620{
3621 struct output *tmp;
3622 struct output *output;
3623
3624 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3625 output_destroy(output);
3626}
3627
Pekka Paalanene1207c72011-12-16 12:02:09 +02003628static void
3629display_destroy_inputs(struct display *display)
3630{
3631 struct input *tmp;
3632 struct input *input;
3633
3634 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3635 input_destroy(input);
3636}
3637
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003638void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003639display_destroy(struct display *display)
3640{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003641 if (!wl_list_empty(&display->window_list))
3642 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3643
3644 if (!wl_list_empty(&display->deferred_list))
3645 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3646
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003647 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003648 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003649
Daniel Stone97f68542012-05-30 16:32:01 +01003650 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003651
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003652 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003653 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003654
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003655#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003656 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003657#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003658
Pekka Paalanenc2052982011-12-16 11:41:32 +02003659 if (display->shell)
3660 wl_shell_destroy(display->shell);
3661
3662 if (display->shm)
3663 wl_shm_destroy(display->shm);
3664
3665 if (display->data_device_manager)
3666 wl_data_device_manager_destroy(display->data_device_manager);
3667
3668 wl_compositor_destroy(display->compositor);
3669
3670 close(display->epoll_fd);
3671
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003672 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003673 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003674 free(display);
3675}
3676
3677void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003678display_set_user_data(struct display *display, void *data)
3679{
3680 display->user_data = data;
3681}
3682
3683void *
3684display_get_user_data(struct display *display)
3685{
3686 return display->user_data;
3687}
3688
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003689struct wl_display *
3690display_get_display(struct display *display)
3691{
3692 return display->display;
3693}
3694
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003695struct output *
3696display_get_output(struct display *display)
3697{
3698 return container_of(display->output_list.next, struct output, link);
3699}
3700
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003701struct wl_compositor *
3702display_get_compositor(struct display *display)
3703{
3704 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003705}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003706
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003707uint32_t
3708display_get_serial(struct display *display)
3709{
3710 return display->serial;
3711}
3712
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003713EGLDisplay
3714display_get_egl_display(struct display *d)
3715{
3716 return d->dpy;
3717}
3718
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003719struct wl_data_source *
3720display_create_data_source(struct display *display)
3721{
3722 return wl_data_device_manager_create_data_source(display->data_device_manager);
3723}
3724
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003725EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003726display_get_argb_egl_config(struct display *d)
3727{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003728 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003729}
3730
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003731struct wl_shell *
3732display_get_shell(struct display *display)
3733{
3734 return display->shell;
3735}
3736
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003737int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003738display_acquire_window_surface(struct display *display,
3739 struct window *window,
3740 EGLContext ctx)
3741{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003742#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003743 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003744 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003745
3746 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003747 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003748 device = cairo_surface_get_device(window->cairo_surface);
3749 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003750 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003751
Benjamin Franzke0c991632011-09-27 21:57:31 +02003752 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003753 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003754 ctx = display->argb_ctx;
3755 else
3756 assert(0);
3757 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003758
3759 data = cairo_surface_get_user_data(window->cairo_surface,
3760 &surface_data_key);
3761
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003762 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003763 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003764 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3765 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003766
3767 return 0;
3768#else
3769 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003770#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003771}
3772
3773void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003774display_release_window_surface(struct display *display,
3775 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003776{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003777#ifdef HAVE_CAIRO_EGL
3778 cairo_device_t *device;
3779
3780 device = cairo_surface_get_device(window->cairo_surface);
3781 if (!device)
3782 return;
3783
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003784 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003785 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003786 cairo_device_release(device);
3787#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003788}
3789
3790void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003791display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003792{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003793 wl_list_insert(&display->deferred_list, &task->link);
3794}
3795
3796void
3797display_watch_fd(struct display *display,
3798 int fd, uint32_t events, struct task *task)
3799{
3800 struct epoll_event ep;
3801
3802 ep.events = events;
3803 ep.data.ptr = task;
3804 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3805}
3806
3807void
3808display_run(struct display *display)
3809{
3810 struct task *task;
3811 struct epoll_event ep[16];
3812 int i, count;
3813
Pekka Paalanen826d7952011-12-15 10:14:07 +02003814 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003815 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003816 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003817
3818 while (!wl_list_empty(&display->deferred_list)) {
3819 task = container_of(display->deferred_list.next,
3820 struct task, link);
3821 wl_list_remove(&task->link);
3822 task->run(task, 0);
3823 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003824
3825 if (!display->running)
3826 break;
3827
3828 wl_display_flush(display->display);
3829
3830 count = epoll_wait(display->epoll_fd,
3831 ep, ARRAY_LENGTH(ep), -1);
3832 for (i = 0; i < count; i++) {
3833 task = ep[i].data.ptr;
3834 task->run(task, ep[i].events);
3835 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003836 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003837}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003838
3839void
3840display_exit(struct display *display)
3841{
3842 display->running = 0;
3843}