blob: 7b35fae4095459f2fae3fabcc9448ccbcc24640b [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
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200632/*
633 * The following correspondences between file names and cursors was copied
634 * from: https://bugs.kde.org/attachment.cgi?id=67313
635 */
636
637static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300638 "bottom_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200639 "sw-resize"
640};
641
642static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300643 "bottom_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200644 "se-resize"
645};
646
647static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300648 "bottom_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200649 "s-resize"
650};
651
652static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200654 "closedhand",
655 "208530c400c041818281048008011002"
656};
657
658static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300659 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200660 "default",
661 "top_left_arrow",
662 "left-arrow"
663};
664
665static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300666 "left_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200667 "w-resize"
668};
669
670static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300671 "right_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200672 "e-resize"
673};
674
675static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 "top_left_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200677 "nw-resize"
678};
679
680static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300681 "top_right_corner",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200682 "ne-resize"
683};
684
685static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300686 "top_side",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200687 "n-resize"
688};
689
690static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300691 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200692 "ibeam",
693 "text"
694};
695
696static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300697 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200698 "pointer",
699 "pointing_hand",
700 "e29285e634086352946a0e7090d73106"
701};
702
703static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400704 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200705 "wait",
706 "0426c94ea35c87780ff01dc239897213"
707};
708
709struct cursor_alternatives {
710 const char **names;
711 size_t count;
712};
713
714static const struct cursor_alternatives cursors[] = {
715 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
716 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
717 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
718 {grabbings, ARRAY_LENGTH(grabbings)},
719 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
720 {left_sides, ARRAY_LENGTH(left_sides)},
721 {right_sides, ARRAY_LENGTH(right_sides)},
722 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
723 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
724 {top_sides, ARRAY_LENGTH(top_sides)},
725 {xterms, ARRAY_LENGTH(xterms)},
726 {hand1s, ARRAY_LENGTH(hand1s)},
727 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300728};
729
730static void
731create_cursors(struct display *display)
732{
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100733 char *config_file;
734 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200735 unsigned int i, j;
736 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100737 struct config_key shell_keys[] = {
738 { "cursor-theme", CONFIG_KEY_STRING, &theme },
739 };
740 struct config_section cs[] = {
741 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
742 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300743
Christopher Michaelac3e5f22012-08-11 15:12:09 +0100744 config_file = config_file_path("weston.ini");
745 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
746 free(config_file);
747
748 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300749 display->cursors =
750 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
751
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300752 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200753 cursor = NULL;
754 for (j = 0; !cursor && j < cursors[i].count; ++j)
755 cursor = wl_cursor_theme_get_cursor(
756 display->cursor_theme, cursors[i].names[j]);
757
758 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300759 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200760 cursors[i].names[0]);
761
762 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +0300763 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300764}
765
766static void
767destroy_cursors(struct display *display)
768{
769 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800770 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300771}
772
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300773struct wl_cursor_image *
774display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400775{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +0200776 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400777
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300778 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400779}
780
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400781static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200782window_get_resize_dx_dy(struct window *window, int *x, int *y)
783{
784 if (window->resize_edges & WINDOW_RESIZING_LEFT)
785 *x = window->server_allocation.width - window->allocation.width;
786 else
787 *x = 0;
788
789 if (window->resize_edges & WINDOW_RESIZING_TOP)
790 *y = window->server_allocation.height -
791 window->allocation.height;
792 else
793 *y = 0;
794
795 window->resize_edges = 0;
796}
797
798static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500799window_attach_surface(struct window *window)
800{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400801 struct display *display = window->display;
802 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000803#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100804 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000805#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500806 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100807
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400808 if (window->type == TYPE_NONE) {
809 window->type = TYPE_TOPLEVEL;
810 if (display->shell)
811 wl_shell_surface_set_toplevel(window->shell_surface);
812 }
813
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
817 data = cairo_surface_get_user_data(window->cairo_surface,
818 &surface_data_key);
819
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 cairo_gl_surface_swapbuffers(window->cairo_surface);
821 wl_egl_window_get_attached_size(data->window,
822 &window->server_allocation.width,
823 &window->server_allocation.height);
824 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 buffer =
828 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400829 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200831 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400833 wl_surface_damage(window->surface, 0, 0,
834 window->allocation.width,
835 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400837 cairo_surface_destroy(window->cairo_surface);
838 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000840 default:
841 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500843
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500844 if (window->input_region) {
845 wl_surface_set_input_region(window->surface,
846 window->input_region);
847 wl_region_destroy(window->input_region);
848 window->input_region = NULL;
849 }
850
851 if (window->opaque_region) {
852 wl_surface_set_opaque_region(window->surface,
853 window->opaque_region);
854 wl_region_destroy(window->opaque_region);
855 window->opaque_region = NULL;
856 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500857}
858
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400859int
860window_has_focus(struct window *window)
861{
862 return window->focus_count > 0;
863}
864
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400866window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500867{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400868 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100869 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500870}
871
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400872void
873window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500875 cairo_surface_reference(surface);
876
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400877 if (window->cairo_surface != NULL)
878 cairo_surface_destroy(window->cairo_surface);
879
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500880 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881}
882
Benjamin Franzke22d54812011-07-16 19:50:32 +0000883#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100884static void
885window_resize_cairo_window_surface(struct window *window)
886{
887 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200888 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889
890 data = cairo_surface_get_user_data(window->cairo_surface,
891 &surface_data_key);
892
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200893 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894 wl_egl_window_resize(data->window,
895 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200896 window->allocation.height,
897 x,y);
898
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899 cairo_gl_surface_set_size(window->cairo_surface,
900 window->allocation.width,
901 window->allocation.height);
902}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000903#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400905struct display *
906window_get_display(struct window *window)
907{
908 return window->display;
909}
910
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400911void
912window_create_surface(struct window *window)
913{
914 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400915 uint32_t flags = 0;
916
917 if (!window->transparent)
918 flags = SURFACE_OPAQUE;
919
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400920 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500921#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100922 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
923 if (window->cairo_surface) {
924 window_resize_cairo_window_surface(window);
925 return;
926 }
927 surface = display_create_surface(window->display,
928 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400929 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931#endif
932 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400933 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400934 &window->allocation,
935 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800937 default:
938 surface = NULL;
939 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400941
942 window_set_surface(window, surface);
943 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944}
945
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200946static void frame_destroy(struct frame *frame);
947
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400948void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500949window_destroy(struct window *window)
950{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200951 struct display *display = window->display;
952 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100953 struct window_output *window_output;
954 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200955
956 if (window->redraw_scheduled)
957 wl_list_remove(&window->redraw_task.link);
958
959 wl_list_for_each(input, &display->input_list, link) {
960 if (input->pointer_focus == window)
961 input->pointer_focus = NULL;
962 if (input->keyboard_focus == window)
963 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500964 if (input->focus_widget &&
965 input->focus_widget->window == window)
966 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200967 }
968
Rob Bradford7507b572012-05-15 17:55:34 +0100969 wl_list_for_each_safe(window_output, window_output_tmp,
970 &window->window_output_list, link) {
971 free (window_output);
972 }
973
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500974 if (window->input_region)
975 wl_region_destroy(window->input_region);
976 if (window->opaque_region)
977 wl_region_destroy(window->opaque_region);
978
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200979 if (window->frame)
980 frame_destroy(window->frame);
981
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200982 if (window->shell_surface)
983 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500984 wl_surface_destroy(window->surface);
985 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200986
987 if (window->cairo_surface != NULL)
988 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200989
990 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500991 free(window);
992}
993
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500995widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500997 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500999 wl_list_for_each(child, &widget->child_list, link) {
1000 target = widget_find_widget(child, x, y);
1001 if (target)
1002 return target;
1003 }
1004
1005 if (widget->allocation.x <= x &&
1006 x < widget->allocation.x + widget->allocation.width &&
1007 widget->allocation.y <= y &&
1008 y < widget->allocation.y + widget->allocation.height) {
1009 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010 }
1011
1012 return NULL;
1013}
1014
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015static struct widget *
1016widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001018 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001020 widget = malloc(sizeof *widget);
1021 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001022 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001024 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001025 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001026 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001027 widget->tooltip = NULL;
1028 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001029
1030 return widget;
1031}
1032
1033struct widget *
1034window_add_widget(struct window *window, void *data)
1035{
1036 window->widget = widget_create(window, data);
1037 wl_list_init(&window->widget->link);
1038
1039 return window->widget;
1040}
1041
1042struct widget *
1043widget_add_widget(struct widget *parent, void *data)
1044{
1045 struct widget *widget;
1046
1047 widget = widget_create(parent->window, data);
1048 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001049
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001050 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001051}
1052
1053void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001054widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001056 struct display *display = widget->window->display;
1057 struct input *input;
1058
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001059 if (widget->tooltip) {
1060 free(widget->tooltip);
1061 widget->tooltip = NULL;
1062 }
1063
Pekka Paalanene156fb62012-01-19 13:51:38 +02001064 wl_list_for_each(input, &display->input_list, link) {
1065 if (input->focus_widget == widget)
1066 input->focus_widget = NULL;
1067 }
1068
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001069 wl_list_remove(&widget->link);
1070 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071}
1072
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001076 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077}
1078
1079void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001080widget_set_size(struct widget *widget, int32_t width, int32_t height)
1081{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001082 widget->allocation.width = width;
1083 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001084}
1085
1086void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087widget_set_allocation(struct widget *widget,
1088 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090 widget->allocation.x = x;
1091 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001092 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001095void
1096widget_set_transparent(struct widget *widget, int transparent)
1097{
1098 widget->opaque = !transparent;
1099}
1100
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001104 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001105}
1106
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001107void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001108widget_set_resize_handler(struct widget *widget,
1109 widget_resize_handler_t handler)
1110{
1111 widget->resize_handler = handler;
1112}
1113
1114void
1115widget_set_redraw_handler(struct widget *widget,
1116 widget_redraw_handler_t handler)
1117{
1118 widget->redraw_handler = handler;
1119}
1120
1121void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001122widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001123{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001124 widget->enter_handler = handler;
1125}
1126
1127void
1128widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1129{
1130 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001131}
1132
1133void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001134widget_set_motion_handler(struct widget *widget,
1135 widget_motion_handler_t handler)
1136{
1137 widget->motion_handler = handler;
1138}
1139
1140void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001141widget_set_button_handler(struct widget *widget,
1142 widget_button_handler_t handler)
1143{
1144 widget->button_handler = handler;
1145}
1146
1147void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001148widget_set_axis_handler(struct widget *widget,
1149 widget_axis_handler_t handler)
1150{
1151 widget->axis_handler = handler;
1152}
1153
1154void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001155widget_schedule_redraw(struct widget *widget)
1156{
1157 window_schedule_redraw(widget->window);
1158}
1159
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001160cairo_surface_t *
1161window_get_surface(struct window *window)
1162{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001163 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001164}
1165
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001166struct wl_surface *
1167window_get_wl_surface(struct window *window)
1168{
1169 return window->surface;
1170}
1171
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001172struct wl_shell_surface *
1173window_get_wl_shell_surface(struct window *window)
1174{
1175 return window->shell_surface;
1176}
1177
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001178static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001179tooltip_redraw_handler(struct widget *widget, void *data)
1180{
1181 cairo_t *cr;
1182 const int32_t r = 3;
1183 struct tooltip *tooltip = data;
1184 int32_t width, height;
1185 struct window *window = widget->window;
1186
1187 cr = cairo_create(window->cairo_surface);
1188 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1189 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1190 cairo_paint(cr);
1191
1192 width = window->allocation.width;
1193 height = window->allocation.height;
1194 rounded_rect(cr, 0, 0, width, height, r);
1195
1196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1197 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1198 cairo_fill(cr);
1199
1200 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1201 cairo_move_to(cr, 10, 16);
1202 cairo_show_text(cr, tooltip->entry);
1203 cairo_destroy(cr);
1204}
1205
1206static cairo_text_extents_t
1207get_text_extents(struct tooltip *tooltip)
1208{
1209 struct window *window;
1210 cairo_t *cr;
1211 cairo_text_extents_t extents;
1212
1213 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1214 * created yet */
1215 window = tooltip->widget->window->parent;
1216 cr = cairo_create(window->cairo_surface);
1217 cairo_text_extents(cr, tooltip->entry, &extents);
1218 cairo_destroy(cr);
1219
1220 return extents;
1221}
1222
1223static int
1224window_create_tooltip(struct tooltip *tooltip)
1225{
1226 struct widget *parent = tooltip->parent;
1227 struct display *display = parent->window->display;
1228 struct window *window;
1229 const int offset_y = 27;
1230 const int margin = 3;
1231 cairo_text_extents_t extents;
1232
1233 if (tooltip->widget)
1234 return 0;
1235
1236 window = window_create_transient(display, parent->window, tooltip->x,
1237 tooltip->y + offset_y,
1238 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1239 if (!window)
1240 return -1;
1241
1242 tooltip->window = window;
1243 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1244
1245 extents = get_text_extents(tooltip);
1246 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1247 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1248
1249 return 0;
1250}
1251
1252void
1253widget_destroy_tooltip(struct widget *parent)
1254{
1255 struct tooltip *tooltip = parent->tooltip;
1256
1257 parent->tooltip_count = 0;
1258 if (!tooltip)
1259 return;
1260
1261 if (tooltip->widget) {
1262 widget_destroy(tooltip->widget);
1263 window_destroy(tooltip->window);
1264 tooltip->widget = NULL;
1265 tooltip->window = NULL;
1266 }
1267
1268 close(tooltip->tooltip_fd);
1269 free(tooltip->entry);
1270 free(tooltip);
1271 parent->tooltip = NULL;
1272}
1273
1274static void
1275tooltip_func(struct task *task, uint32_t events)
1276{
1277 struct tooltip *tooltip =
1278 container_of(task, struct tooltip, tooltip_task);
1279 uint64_t exp;
1280
Martin Olsson8df662a2012-07-08 03:03:47 +02001281 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1282 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001283 window_create_tooltip(tooltip);
1284}
1285
1286#define TOOLTIP_TIMEOUT 500
1287static int
1288tooltip_timer_reset(struct tooltip *tooltip)
1289{
1290 struct itimerspec its;
1291
1292 its.it_interval.tv_sec = 0;
1293 its.it_interval.tv_nsec = 0;
1294 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1295 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1296 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1297 fprintf(stderr, "could not set timerfd\n: %m");
1298 return -1;
1299 }
1300
1301 return 0;
1302}
1303
1304int
1305widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1306{
1307 struct tooltip *tooltip = parent->tooltip;
1308
1309 parent->tooltip_count++;
1310 if (tooltip) {
1311 tooltip->x = x;
1312 tooltip->y = y;
1313 tooltip_timer_reset(tooltip);
1314 return 0;
1315 }
1316
1317 /* the handler might be triggered too fast via input device motion, so
1318 * we need this check here to make sure tooltip is fully initialized */
1319 if (parent->tooltip_count > 1)
1320 return 0;
1321
1322 tooltip = malloc(sizeof *tooltip);
1323 if (!tooltip)
1324 return -1;
1325
1326 parent->tooltip = tooltip;
1327 tooltip->parent = parent;
1328 tooltip->widget = NULL;
1329 tooltip->window = NULL;
1330 tooltip->x = x;
1331 tooltip->y = y;
1332 tooltip->entry = strdup(entry);
1333 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1334 if (tooltip->tooltip_fd < 0) {
1335 fprintf(stderr, "could not create timerfd\n: %m");
1336 return -1;
1337 }
1338
1339 tooltip->tooltip_task.run = tooltip_func;
1340 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1341 EPOLLIN, &tooltip->tooltip_task);
1342 tooltip_timer_reset(tooltip);
1343
1344 return 0;
1345}
1346
1347static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348frame_resize_handler(struct widget *widget,
1349 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001350{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001351 struct frame *frame = data;
1352 struct widget *child = frame->child;
1353 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001354 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001355 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001356 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001357 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001358 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001359 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001360
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001361 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001362 decoration_width = (t->width + t->margin) * 2;
1363 decoration_height = t->width +
1364 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001365
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001366 allocation.x = t->width + t->margin;
1367 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001368 allocation.width = width - decoration_width;
1369 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001370
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001371 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001372
1373 wl_list_for_each(button, &frame->buttons_list, link)
1374 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001375 } else {
1376 decoration_width = 0;
1377 decoration_height = 0;
1378
1379 allocation.x = 0;
1380 allocation.y = 0;
1381 allocation.width = width;
1382 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001383 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001384
1385 wl_list_for_each(button, &frame->buttons_list, link)
1386 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001387 }
1388
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389 widget_set_allocation(child, allocation.x, allocation.y,
1390 allocation.width, allocation.height);
1391
1392 if (child->resize_handler)
1393 child->resize_handler(child,
1394 allocation.width,
1395 allocation.height,
1396 child->user_data);
1397
Scott Moreauf7e498c2012-05-14 11:39:29 -06001398 width = child->allocation.width + decoration_width;
1399 height = child->allocation.height + decoration_height;
1400
Kristian Høgsberg023be102012-07-31 11:59:12 -04001401 if (widget->window->type != TYPE_FULLSCREEN) {
1402 widget->window->input_region =
1403 wl_compositor_create_region(display->compositor);
1404 wl_region_add(widget->window->input_region,
1405 t->margin, t->margin,
1406 width - 2 * t->margin,
1407 height - 2 * t->margin);
1408 }
1409
Scott Moreauf7e498c2012-05-14 11:39:29 -06001410 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001411
1412 if (child->opaque) {
1413 widget->window->opaque_region =
1414 wl_compositor_create_region(display->compositor);
1415 wl_region_add(widget->window->opaque_region,
1416 opaque_margin, opaque_margin,
1417 widget->allocation.width - 2 * opaque_margin,
1418 widget->allocation.height - 2 * opaque_margin);
1419 }
Martin Minarik1998b152012-05-10 02:04:35 +02001420
1421 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001422 x_r = frame->widget->allocation.width - t->width - t->margin;
1423 x_l = t->width + t->margin;
1424 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001425 wl_list_for_each(button, &frame->buttons_list, link) {
1426 const int button_padding = 4;
1427 w = cairo_image_surface_get_width(button->icon);
1428 h = cairo_image_surface_get_height(button->icon);
1429
1430 if (button->decoration == FRAME_BUTTON_FANCY)
1431 w += 10;
1432
1433 if (button->align == FRAME_BUTTON_LEFT) {
1434 widget_set_allocation(button->widget,
1435 x_l, y , w + 1, h + 1);
1436 x_l += w;
1437 x_l += button_padding;
1438 } else {
1439 x_r -= w;
1440 widget_set_allocation(button->widget,
1441 x_r, y , w + 1, h + 1);
1442 x_r -= button_padding;
1443 }
1444 }
1445}
1446
1447static int
1448frame_button_enter_handler(struct widget *widget,
1449 struct input *input, float x, float y, void *data)
1450{
1451 struct frame_button *frame_button = data;
1452
1453 widget_schedule_redraw(frame_button->widget);
1454 frame_button->state = FRAME_BUTTON_OVER;
1455
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001456 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001457}
1458
1459static void
1460frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1461{
1462 struct frame_button *frame_button = data;
1463
1464 widget_schedule_redraw(frame_button->widget);
1465 frame_button->state = FRAME_BUTTON_DEFAULT;
1466}
1467
1468static void
1469frame_button_button_handler(struct widget *widget,
1470 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001471 uint32_t button,
1472 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001473{
1474 struct frame_button *frame_button = data;
1475 struct window *window = widget->window;
1476
1477 if (button != BTN_LEFT)
1478 return;
1479
1480 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001481 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001482 frame_button->state = FRAME_BUTTON_ACTIVE;
1483 widget_schedule_redraw(frame_button->widget);
1484
1485 if (frame_button->type == FRAME_BUTTON_ICON)
1486 window_show_frame_menu(window, input, time);
1487 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001488 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001489 frame_button->state = FRAME_BUTTON_DEFAULT;
1490 widget_schedule_redraw(frame_button->widget);
1491 break;
1492 }
1493
1494 switch (frame_button->type) {
1495 case FRAME_BUTTON_CLOSE:
1496 if (window->close_handler)
1497 window->close_handler(window->parent,
1498 window->user_data);
1499 else
1500 display_exit(window->display);
1501 break;
1502 case FRAME_BUTTON_MINIMIZE:
1503 fprintf(stderr,"Minimize stub\n");
1504 break;
1505 case FRAME_BUTTON_MAXIMIZE:
1506 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1507 break;
1508 default:
1509 /* Unknown operation */
1510 break;
1511 }
1512}
1513
1514static void
1515frame_button_redraw_handler(struct widget *widget, void *data)
1516{
1517 struct frame_button *frame_button = data;
1518 cairo_t *cr;
1519 int width, height, x, y;
1520 struct window *window = widget->window;
1521
1522 x = widget->allocation.x;
1523 y = widget->allocation.y;
1524 width = widget->allocation.width;
1525 height = widget->allocation.height;
1526
1527 if (!width)
1528 return;
1529 if (!height)
1530 return;
1531 if (widget->opaque)
1532 return;
1533
1534 cr = cairo_create(window->cairo_surface);
1535
1536 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1537 cairo_set_line_width(cr, 1);
1538
1539 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1540 cairo_rectangle (cr, x, y, 25, 16);
1541
1542 cairo_stroke_preserve(cr);
1543
1544 switch (frame_button->state) {
1545 case FRAME_BUTTON_DEFAULT:
1546 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1547 break;
1548 case FRAME_BUTTON_OVER:
1549 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1550 break;
1551 case FRAME_BUTTON_ACTIVE:
1552 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1553 break;
1554 }
1555
1556 cairo_fill (cr);
1557
1558 x += 4;
1559 }
1560
1561 cairo_set_source_surface(cr, frame_button->icon, x, y);
1562 cairo_paint(cr);
1563
1564 cairo_destroy(cr);
1565}
1566
1567static struct widget *
1568frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1569 enum frame_button_align align, enum frame_button_decoration style)
1570{
1571 struct frame_button *frame_button;
1572 const char *icon = data;
1573
1574 frame_button = malloc (sizeof *frame_button);
1575 memset(frame_button, 0, sizeof *frame_button);
1576
1577 frame_button->icon = cairo_image_surface_create_from_png(icon);
1578 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1579 frame_button->frame = frame;
1580 frame_button->type = type;
1581 frame_button->align = align;
1582 frame_button->decoration = style;
1583
1584 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1585
1586 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1587 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1588 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1589 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1590 return frame_button->widget;
1591}
1592
1593static void
1594frame_button_destroy(struct frame_button *frame_button)
1595{
1596 widget_destroy(frame_button->widget);
1597 wl_list_remove(&frame_button->link);
1598 cairo_surface_destroy(frame_button->icon);
1599 free(frame_button);
1600
1601 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602}
1603
1604static void
1605frame_redraw_handler(struct widget *widget, void *data)
1606{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001609 struct theme *t = window->display->theme;
1610 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001612 if (window->type == TYPE_FULLSCREEN)
1613 return;
1614
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 cr = cairo_create(window->cairo_surface);
1616
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001617 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001618 flags |= THEME_FRAME_ACTIVE;
1619 theme_render_frame(t, cr, widget->allocation.width,
1620 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621
1622 cairo_destroy(cr);
1623}
1624
1625static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001626frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001627{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001628 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001629 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001630
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001631 location = theme_get_location(t, input->sx, input->sy,
1632 frame->widget->allocation.width,
1633 frame->widget->allocation.height);
1634
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001635 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001636 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001637 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001638 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001639 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001640 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001641 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001642 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001643 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001644 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001645 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001646 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001647 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001648 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001649 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001650 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001651 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001652 case THEME_LOCATION_EXTERIOR:
1653 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001654 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001655 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001656 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001657}
1658
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001659static void
1660frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001661{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001662 switch (index) {
1663 case 0: /* close */
1664 if (window->close_handler)
1665 window->close_handler(window->parent,
1666 window->user_data);
1667 else
1668 display_exit(window->display);
1669 break;
1670 case 1: /* fullscreen */
1671 /* we don't have a way to get out of fullscreen for now */
Kristian Høgsberg67ace202012-07-23 21:56:31 -04001672 if (window->fullscreen_handler)
1673 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001674 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001675 }
1676}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001677
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001678void
1679window_show_frame_menu(struct window *window,
1680 struct input *input, uint32_t time)
1681{
1682 int32_t x, y;
1683
1684 static const char *entries[] = {
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001685 "Close", "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001686 };
1687
1688 input_get_position(input, &x, &y);
1689 window_show_menu(window->display, input, time, window,
Philipp Brüschweilerb8a8aff2012-08-14 12:26:54 +02001690 x - 10, y - 10, frame_menu_func, entries,
1691 ARRAY_LENGTH(entries));
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001692}
1693
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001694static int
1695frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001696 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697{
1698 return frame_get_pointer_image_for_location(data, input);
1699}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001700
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001701static int
1702frame_motion_handler(struct widget *widget,
1703 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001704 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001705{
1706 return frame_get_pointer_image_for_location(data, input);
1707}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001708
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001709static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001710frame_button_handler(struct widget *widget,
1711 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001712 uint32_t button, enum wl_pointer_button_state state,
1713 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001714
1715{
1716 struct frame *frame = data;
1717 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001718 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001719 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001720
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001721 location = theme_get_location(display->theme, input->sx, input->sy,
1722 frame->widget->allocation.width,
1723 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001724
Daniel Stone4dbadb12012-05-30 16:31:51 +01001725 if (window->display->shell && button == BTN_LEFT &&
1726 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001727 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001728 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001729 if (!window->shell_surface)
1730 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001731 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001732 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001733 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001734 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001735 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001736 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001737 case THEME_LOCATION_RESIZING_TOP:
1738 case THEME_LOCATION_RESIZING_BOTTOM:
1739 case THEME_LOCATION_RESIZING_LEFT:
1740 case THEME_LOCATION_RESIZING_RIGHT:
1741 case THEME_LOCATION_RESIZING_TOP_LEFT:
1742 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1743 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1744 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001745 if (!window->shell_surface)
1746 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001747 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001748
1749 if (!display->dpy) {
1750 /* If we're using shm, allocate a big
1751 pool to create buffers out of while
1752 we resize. We should probably base
1753 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001754 window->pool =
1755 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001756 }
1757
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001758 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001759 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001760 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001761 break;
1762 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001763 } else if (button == BTN_RIGHT &&
1764 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001765 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001766 }
1767}
1768
1769struct widget *
1770frame_create(struct window *window, void *data)
1771{
1772 struct frame *frame;
1773
1774 frame = malloc(sizeof *frame);
1775 memset(frame, 0, sizeof *frame);
1776
1777 frame->widget = window_add_widget(window, frame);
1778 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001779
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001780 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1781 widget_set_resize_handler(frame->widget, frame_resize_handler);
1782 widget_set_enter_handler(frame->widget, frame_enter_handler);
1783 widget_set_motion_handler(frame->widget, frame_motion_handler);
1784 widget_set_button_handler(frame->widget, frame_button_handler);
1785
Martin Minarik1998b152012-05-10 02:04:35 +02001786 /* Create empty list for frame buttons */
1787 wl_list_init(&frame->buttons_list);
1788
1789 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1790 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1791
1792 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1793 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1794
1795 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1796 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1797
1798 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1799 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1800
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001801 window->frame = frame;
1802
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001803 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001804}
1805
Kristian Høgsberga1627922012-06-20 17:30:03 -04001806void
1807frame_set_child_size(struct widget *widget, int child_width, int child_height)
1808{
1809 struct display *display = widget->window->display;
1810 struct theme *t = display->theme;
1811 int decoration_width, decoration_height;
1812 int width, height;
1813
1814 if (widget->window->type != TYPE_FULLSCREEN) {
1815 decoration_width = (t->width + t->margin) * 2;
1816 decoration_height = t->width +
1817 t->titlebar_height + t->margin * 2;
1818
1819 width = child_width + decoration_width;
1820 height = child_height + decoration_height;
1821 } else {
1822 width = child_width;
1823 height = child_height;
1824 }
1825
1826 window_schedule_resize(widget->window, width, height);
1827}
1828
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001829static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001830frame_destroy(struct frame *frame)
1831{
Martin Minarik1998b152012-05-10 02:04:35 +02001832 struct frame_button *button, *tmp;
1833
1834 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1835 frame_button_destroy(button);
1836
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001837 /* frame->child must be destroyed by the application */
1838 widget_destroy(frame->widget);
1839 free(frame);
1840}
1841
1842static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001843input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001844 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001845{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001846 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001847 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001848
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001849 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001850 return;
1851
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001852 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001853 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001854 widget = old;
1855 if (input->grab)
1856 widget = input->grab;
1857 if (widget->leave_handler)
1858 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001859 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001860 }
1861
1862 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001863 widget = focus;
1864 if (input->grab)
1865 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001866 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001867 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001868 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001869 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001870
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001871 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001872 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001873}
1874
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04001875void
1876input_grab(struct input *input, struct widget *widget, uint32_t button)
1877{
1878 input->grab = widget;
1879 input->grab_button = button;
1880}
1881
1882void
1883input_ungrab(struct input *input)
1884{
1885 struct widget *widget;
1886
1887 input->grab = NULL;
1888 if (input->pointer_focus) {
1889 widget = widget_find_widget(input->pointer_focus->widget,
1890 input->sx, input->sy);
1891 input_set_focus_widget(input, widget, input->sx, input->sy);
1892 }
1893}
1894
1895static void
1896input_remove_pointer_focus(struct input *input)
1897{
1898 struct window *window = input->pointer_focus;
1899
1900 if (!window)
1901 return;
1902
1903 input_set_focus_widget(input, NULL, 0, 0);
1904
1905 input->pointer_focus = NULL;
1906 input->current_cursor = CURSOR_UNSET;
1907}
1908
1909static void
1910pointer_handle_enter(void *data, struct wl_pointer *pointer,
1911 uint32_t serial, struct wl_surface *surface,
1912 wl_fixed_t sx_w, wl_fixed_t sy_w)
1913{
1914 struct input *input = data;
1915 struct window *window;
1916 struct widget *widget;
1917 float sx = wl_fixed_to_double(sx_w);
1918 float sy = wl_fixed_to_double(sy_w);
1919
1920 if (!surface) {
1921 /* enter event for a window we've just destroyed */
1922 return;
1923 }
1924
1925 input->display->serial = serial;
1926 input->pointer_enter_serial = serial;
1927 input->pointer_focus = wl_surface_get_user_data(surface);
1928 window = input->pointer_focus;
1929
1930 if (window->pool) {
1931 shm_pool_destroy(window->pool);
1932 window->pool = NULL;
1933 /* Schedule a redraw to free the pool */
1934 window_schedule_redraw(window);
1935 }
1936
1937 input->sx = sx;
1938 input->sy = sy;
1939
1940 widget = widget_find_widget(window->widget, sx, sy);
1941 input_set_focus_widget(input, widget, sx, sy);
1942}
1943
1944static void
1945pointer_handle_leave(void *data, struct wl_pointer *pointer,
1946 uint32_t serial, struct wl_surface *surface)
1947{
1948 struct input *input = data;
1949
1950 input->display->serial = serial;
1951 input_remove_pointer_focus(input);
1952}
1953
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001954static void
Daniel Stone37816df2012-05-16 18:45:18 +01001955pointer_handle_motion(void *data, struct wl_pointer *pointer,
1956 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001957{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001958 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001959 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001960 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001961 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001962 float sx = wl_fixed_to_double(sx_w);
1963 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001964
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001965 input->sx = sx;
1966 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001967
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001968 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001969 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001970 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001971 }
1972
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001973 if (input->grab)
1974 widget = input->grab;
1975 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001976 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001977 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001978 cursor = widget->motion_handler(input->focus_widget,
1979 input, time, sx, sy,
1980 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001981
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001982 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001983}
1984
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001985static void
Daniel Stone37816df2012-05-16 18:45:18 +01001986pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001987 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001988{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001989 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001990 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001991 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001992
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001993 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001994 if (input->focus_widget && input->grab == NULL &&
1995 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001996 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001997
Neil Roberts6b28aad2012-01-23 19:11:18 +00001998 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001999 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002000 (*widget->button_handler)(widget,
2001 input, time,
2002 button, state,
2003 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002004
Daniel Stone4dbadb12012-05-30 16:31:51 +01002005 if (input->grab && input->grab_button == button &&
2006 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002007 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002008}
2009
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002010static void
Daniel Stone37816df2012-05-16 18:45:18 +01002011pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002012 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002013{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002014 struct input *input = data;
2015 struct widget *widget;
2016
2017 widget = input->focus_widget;
2018 if (input->grab)
2019 widget = input->grab;
2020 if (widget && widget->axis_handler)
2021 (*widget->axis_handler)(widget,
2022 input, time,
2023 axis, value,
2024 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002025}
2026
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002027static const struct wl_pointer_listener pointer_listener = {
2028 pointer_handle_enter,
2029 pointer_handle_leave,
2030 pointer_handle_motion,
2031 pointer_handle_button,
2032 pointer_handle_axis,
2033};
2034
2035static void
2036input_remove_keyboard_focus(struct input *input)
2037{
2038 struct window *window = input->keyboard_focus;
2039 struct itimerspec its;
2040
2041 its.it_interval.tv_sec = 0;
2042 its.it_interval.tv_nsec = 0;
2043 its.it_value.tv_sec = 0;
2044 its.it_value.tv_nsec = 0;
2045 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2046
2047 if (!window)
2048 return;
2049
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002050 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002051 if (window->keyboard_focus_handler)
2052 (*window->keyboard_focus_handler)(window, NULL,
2053 window->user_data);
2054
2055 input->keyboard_focus = NULL;
2056}
2057
2058static void
2059keyboard_repeat_func(struct task *task, uint32_t events)
2060{
2061 struct input *input =
2062 container_of(task, struct input, repeat_task);
2063 struct window *window = input->keyboard_focus;
2064 uint64_t exp;
2065
2066 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2067 /* If we change the timer between the fd becoming
2068 * readable and getting here, there'll be nothing to
2069 * read and we get EAGAIN. */
2070 return;
2071
2072 if (window && window->key_handler) {
2073 (*window->key_handler)(window, input, input->repeat_time,
2074 input->repeat_key, input->repeat_sym,
2075 WL_KEYBOARD_KEY_STATE_PRESSED,
2076 window->user_data);
2077 }
2078}
2079
2080static void
2081keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2082 uint32_t format, int fd, uint32_t size)
2083{
2084 struct input *input = data;
2085 char *map_str;
2086
2087 if (!data) {
2088 close(fd);
2089 return;
2090 }
2091
2092 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2093 close(fd);
2094 return;
2095 }
2096
2097 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2098 if (map_str == MAP_FAILED) {
2099 close(fd);
2100 return;
2101 }
2102
2103 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2104 map_str,
2105 XKB_KEYMAP_FORMAT_TEXT_V1,
2106 0);
2107 munmap(map_str, size);
2108 close(fd);
2109
2110 if (!input->xkb.keymap) {
2111 fprintf(stderr, "failed to compile keymap\n");
2112 return;
2113 }
2114
2115 input->xkb.state = xkb_state_new(input->xkb.keymap);
2116 if (!input->xkb.state) {
2117 fprintf(stderr, "failed to create XKB state\n");
2118 xkb_map_unref(input->xkb.keymap);
2119 input->xkb.keymap = NULL;
2120 return;
2121 }
2122
2123 input->xkb.control_mask =
2124 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2125 input->xkb.alt_mask =
2126 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2127 input->xkb.shift_mask =
2128 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2129}
2130
2131static void
2132keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2133 uint32_t serial, struct wl_surface *surface,
2134 struct wl_array *keys)
2135{
2136 struct input *input = data;
2137 struct window *window;
2138
2139 input->display->serial = serial;
2140 input->keyboard_focus = wl_surface_get_user_data(surface);
2141
2142 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002143 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002144 if (window->keyboard_focus_handler)
2145 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002146 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002147}
2148
2149static void
2150keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2151 uint32_t serial, struct wl_surface *surface)
2152{
2153 struct input *input = data;
2154
2155 input->display->serial = serial;
2156 input_remove_keyboard_focus(input);
2157}
2158
Scott Moreau210d0792012-03-22 10:47:01 -06002159static void
Daniel Stone37816df2012-05-16 18:45:18 +01002160keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002161 uint32_t serial, uint32_t time, uint32_t key,
2162 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002163{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002164 struct input *input = data;
2165 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002166 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002167 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002168 const xkb_keysym_t *syms;
2169 xkb_keysym_t sym;
2170 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002171 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002172
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002173 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002174 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002175 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002176 return;
2177
Daniel Stone97f68542012-05-30 16:32:01 +01002178 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002179
Daniel Stone97f68542012-05-30 16:32:01 +01002180 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04002181 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04002182 XKB_STATE_LATCHED);
2183 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01002184 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002185 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002186 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002187 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01002188 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04002189 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002190
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002191 sym = XKB_KEY_NoSymbol;
2192 if (num_syms == 1)
2193 sym = syms[0];
2194
2195 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002196 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002197 window_set_maximized(window,
2198 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002199 } else if (sym == XKB_KEY_F11 &&
2200 window->fullscreen_handler &&
2201 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2202 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002203 } else if (sym == XKB_KEY_F4 &&
2204 input->modifiers == MOD_ALT_MASK &&
2205 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2206 if (window->close_handler)
2207 window->close_handler(window->parent,
2208 window->user_data);
2209 else
2210 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002211 } else if (window->key_handler) {
2212 (*window->key_handler)(window, input, time, key,
2213 sym, state, window->user_data);
2214 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002215
2216 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2217 key == input->repeat_key) {
2218 its.it_interval.tv_sec = 0;
2219 its.it_interval.tv_nsec = 0;
2220 its.it_value.tv_sec = 0;
2221 its.it_value.tv_nsec = 0;
2222 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2223 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2224 input->repeat_sym = sym;
2225 input->repeat_key = key;
2226 input->repeat_time = time;
2227 its.it_interval.tv_sec = 0;
2228 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2229 its.it_value.tv_sec = 0;
2230 its.it_value.tv_nsec = 400 * 1000 * 1000;
2231 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2232 }
2233}
2234
2235static void
Daniel Stone351eb612012-05-31 15:27:47 -04002236keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2237 uint32_t serial, uint32_t mods_depressed,
2238 uint32_t mods_latched, uint32_t mods_locked,
2239 uint32_t group)
2240{
2241 struct input *input = data;
2242
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002243 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2244 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04002245}
2246
Daniel Stone37816df2012-05-16 18:45:18 +01002247static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002248 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002249 keyboard_handle_enter,
2250 keyboard_handle_leave,
2251 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002252 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002253};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002254
2255static void
Daniel Stone37816df2012-05-16 18:45:18 +01002256seat_handle_capabilities(void *data, struct wl_seat *seat,
2257 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002258{
Daniel Stone37816df2012-05-16 18:45:18 +01002259 struct input *input = data;
2260
2261 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2262 input->pointer = wl_seat_get_pointer(seat);
2263 wl_pointer_set_user_data(input->pointer, input);
2264 wl_pointer_add_listener(input->pointer, &pointer_listener,
2265 input);
2266 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2267 wl_pointer_destroy(input->pointer);
2268 input->pointer = NULL;
2269 }
2270
2271 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2272 input->keyboard = wl_seat_get_keyboard(seat);
2273 wl_keyboard_set_user_data(input->keyboard, input);
2274 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2275 input);
2276 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2277 wl_keyboard_destroy(input->keyboard);
2278 input->keyboard = NULL;
2279 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002280}
2281
Daniel Stone37816df2012-05-16 18:45:18 +01002282static const struct wl_seat_listener seat_listener = {
2283 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002284};
2285
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002286void
2287input_get_position(struct input *input, int32_t *x, int32_t *y)
2288{
2289 *x = input->sx;
2290 *y = input->sy;
2291}
2292
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002293struct display *
2294input_get_display(struct input *input)
2295{
2296 return input->display;
2297}
2298
Daniel Stone37816df2012-05-16 18:45:18 +01002299struct wl_seat *
2300input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002301{
Daniel Stone37816df2012-05-16 18:45:18 +01002302 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002303}
2304
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002305uint32_t
2306input_get_modifiers(struct input *input)
2307{
2308 return input->modifiers;
2309}
2310
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002311struct widget *
2312input_get_focus_widget(struct input *input)
2313{
2314 return input->focus_widget;
2315}
2316
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002317struct data_offer {
2318 struct wl_data_offer *offer;
2319 struct input *input;
2320 struct wl_array types;
2321 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002322
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002323 struct task io_task;
2324 int fd;
2325 data_func_t func;
2326 int32_t x, y;
2327 void *user_data;
2328};
2329
2330static void
2331data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2332{
2333 struct data_offer *offer = data;
2334 char **p;
2335
2336 p = wl_array_add(&offer->types, sizeof *p);
2337 *p = strdup(type);
2338}
2339
2340static const struct wl_data_offer_listener data_offer_listener = {
2341 data_offer_offer,
2342};
2343
2344static void
2345data_offer_destroy(struct data_offer *offer)
2346{
2347 char **p;
2348
2349 offer->refcount--;
2350 if (offer->refcount == 0) {
2351 wl_data_offer_destroy(offer->offer);
2352 for (p = offer->types.data; *p; p++)
2353 free(*p);
2354 wl_array_release(&offer->types);
2355 free(offer);
2356 }
2357}
2358
2359static void
2360data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002361 struct wl_data_device *data_device,
2362 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002363{
2364 struct data_offer *offer;
2365
2366 offer = malloc(sizeof *offer);
2367
2368 wl_array_init(&offer->types);
2369 offer->refcount = 1;
2370 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002371 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002372 wl_data_offer_add_listener(offer->offer,
2373 &data_offer_listener, offer);
2374}
2375
2376static void
2377data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002378 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002379 wl_fixed_t x_w, wl_fixed_t y_w,
2380 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002381{
2382 struct input *input = data;
2383 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002384 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002385 float x = wl_fixed_to_double(x_w);
2386 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002387 char **p;
2388
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002389 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002390 window = wl_surface_get_user_data(surface);
2391 input->pointer_focus = window;
2392
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002393 if (offer) {
2394 input->drag_offer = wl_data_offer_get_user_data(offer);
2395
2396 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2397 *p = NULL;
2398
2399 types_data = input->drag_offer->types.data;
2400 } else {
2401 input->drag_offer = NULL;
2402 types_data = NULL;
2403 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002404
2405 window = input->pointer_focus;
2406 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002407 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002408 window->user_data);
2409}
2410
2411static void
2412data_device_leave(void *data, struct wl_data_device *data_device)
2413{
2414 struct input *input = data;
2415
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002416 if (input->drag_offer) {
2417 data_offer_destroy(input->drag_offer);
2418 input->drag_offer = NULL;
2419 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002420}
2421
2422static void
2423data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002424 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002425{
2426 struct input *input = data;
2427 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002428 float x = wl_fixed_to_double(x_w);
2429 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002430 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002431
2432 input->sx = x;
2433 input->sy = y;
2434
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002435 if (input->drag_offer)
2436 types_data = input->drag_offer->types.data;
2437 else
2438 types_data = NULL;
2439
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002440 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002441 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002442 window->user_data);
2443}
2444
2445static void
2446data_device_drop(void *data, struct wl_data_device *data_device)
2447{
2448 struct input *input = data;
2449 struct window *window = input->pointer_focus;
2450
2451 if (window->drop_handler)
2452 window->drop_handler(window, input,
2453 input->sx, input->sy, window->user_data);
2454}
2455
2456static void
2457data_device_selection(void *data,
2458 struct wl_data_device *wl_data_device,
2459 struct wl_data_offer *offer)
2460{
2461 struct input *input = data;
2462 char **p;
2463
2464 if (input->selection_offer)
2465 data_offer_destroy(input->selection_offer);
2466
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002467 if (offer) {
2468 input->selection_offer = wl_data_offer_get_user_data(offer);
2469 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2470 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002471 } else {
2472 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002473 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002474}
2475
2476static const struct wl_data_device_listener data_device_listener = {
2477 data_device_data_offer,
2478 data_device_enter,
2479 data_device_leave,
2480 data_device_motion,
2481 data_device_drop,
2482 data_device_selection
2483};
2484
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002485static void
2486input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002487{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002488 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002489 struct wl_cursor *cursor;
2490 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002491
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002492 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002493 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002494 return;
2495
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002496 if (index >= (int) cursor->image_count) {
2497 fprintf(stderr, "cursor index out of range\n");
2498 return;
2499 }
2500
2501 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002502 buffer = wl_cursor_image_get_buffer(image);
2503 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002504 return;
2505
Kristian Høgsbergae277372012-08-01 09:41:08 -04002506 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002507 input->pointer_surface,
2508 image->hotspot_x, image->hotspot_y);
2509 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2510 wl_surface_damage(input->pointer_surface, 0, 0,
2511 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512}
2513
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002514static const struct wl_callback_listener pointer_surface_listener;
2515
2516static void
2517pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2518 uint32_t time)
2519{
2520 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002521 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002522 int i;
2523
2524 if (callback) {
2525 assert(callback == input->cursor_frame_cb);
2526 wl_callback_destroy(callback);
2527 input->cursor_frame_cb = NULL;
2528 }
2529
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002530 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04002531 wl_pointer_set_cursor(input->pointer,
2532 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002533 NULL, 0, 0);
2534 return;
2535 }
2536
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002537 if (input->current_cursor == CURSOR_UNSET)
2538 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002539 cursor = input->display->cursors[input->current_cursor];
2540 if (!cursor)
2541 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002542
2543 /* FIXME We don't have the current time on the first call so we set
2544 * the animation start to the time of the first frame callback. */
2545 if (time == 0)
2546 input->cursor_anim_start = 0;
2547 else if (input->cursor_anim_start == 0)
2548 input->cursor_anim_start = time;
2549
2550 if (time == 0 || input->cursor_anim_start == 0)
2551 i = 0;
2552 else
2553 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2554
2555 input_set_pointer_image_index(input, i);
2556
2557 if (cursor->image_count == 1)
2558 return;
2559
2560 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2561 wl_callback_add_listener(input->cursor_frame_cb,
2562 &pointer_surface_listener, input);
2563}
2564
2565static const struct wl_callback_listener pointer_surface_listener = {
2566 pointer_surface_frame_callback
2567};
2568
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002569void
2570input_set_pointer_image(struct input *input, int pointer)
2571{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002572 int force = 0;
2573
2574 if (input->pointer_enter_serial > input->cursor_serial)
2575 force = 1;
2576
2577 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002578 return;
2579
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002580 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002581 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002582 if (!input->cursor_frame_cb)
2583 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002584 else if (force) {
2585 /* The current frame callback may be stuck if, for instance,
2586 * the set cursor request was processed by the server after
2587 * this client lost the focus. In this case the cursor surface
2588 * might not be mapped and the frame callback wouldn't ever
2589 * complete. Send a set_cursor and attach to try to map the
2590 * cursor surface again so that the callback will finish */
2591 input_set_pointer_image_index(input, 0);
2592 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002593}
2594
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002595struct wl_data_device *
2596input_get_data_device(struct input *input)
2597{
2598 return input->data_device;
2599}
2600
2601void
2602input_set_selection(struct input *input,
2603 struct wl_data_source *source, uint32_t time)
2604{
2605 wl_data_device_set_selection(input->data_device, source, time);
2606}
2607
2608void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002609input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002610{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002611 wl_data_offer_accept(input->drag_offer->offer,
2612 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002613}
2614
2615static void
2616offer_io_func(struct task *task, uint32_t events)
2617{
2618 struct data_offer *offer =
2619 container_of(task, struct data_offer, io_task);
2620 unsigned int len;
2621 char buffer[4096];
2622
2623 len = read(offer->fd, buffer, sizeof buffer);
2624 offer->func(buffer, len,
2625 offer->x, offer->y, offer->user_data);
2626
2627 if (len == 0) {
2628 close(offer->fd);
2629 data_offer_destroy(offer);
2630 }
2631}
2632
2633static void
2634data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2635 data_func_t func, void *user_data)
2636{
2637 int p[2];
2638
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002639 if (pipe2(p, O_CLOEXEC) == -1)
2640 return;
2641
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002642 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2643 close(p[1]);
2644
2645 offer->io_task.run = offer_io_func;
2646 offer->fd = p[0];
2647 offer->func = func;
2648 offer->refcount++;
2649 offer->user_data = user_data;
2650
2651 display_watch_fd(offer->input->display,
2652 offer->fd, EPOLLIN, &offer->io_task);
2653}
2654
2655void
2656input_receive_drag_data(struct input *input, const char *mime_type,
2657 data_func_t func, void *data)
2658{
2659 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2660 input->drag_offer->x = input->sx;
2661 input->drag_offer->y = input->sy;
2662}
2663
2664int
2665input_receive_selection_data(struct input *input, const char *mime_type,
2666 data_func_t func, void *data)
2667{
2668 char **p;
2669
2670 if (input->selection_offer == NULL)
2671 return -1;
2672
2673 for (p = input->selection_offer->types.data; *p; p++)
2674 if (strcmp(mime_type, *p) == 0)
2675 break;
2676
2677 if (*p == NULL)
2678 return -1;
2679
2680 data_offer_receive_data(input->selection_offer,
2681 mime_type, func, data);
2682 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002683}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002684
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002685int
2686input_receive_selection_data_to_fd(struct input *input,
2687 const char *mime_type, int fd)
2688{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002689 if (input->selection_offer)
2690 wl_data_offer_receive(input->selection_offer->offer,
2691 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002692
2693 return 0;
2694}
2695
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002696void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002697window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002698{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002699 if (!window->shell_surface)
2700 return;
2701
Daniel Stone37816df2012-05-16 18:45:18 +01002702 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002703}
2704
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002705static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002706idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002707{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002708 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002709
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002710 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002711 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002712 widget_set_allocation(widget,
2713 window->pending_allocation.x,
2714 window->pending_allocation.y,
2715 window->pending_allocation.width,
2716 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002717
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002718 if (window->input_region) {
2719 wl_region_destroy(window->input_region);
2720 window->input_region = NULL;
2721 }
2722
2723 if (window->opaque_region) {
2724 wl_region_destroy(window->opaque_region);
2725 window->opaque_region = NULL;
2726 }
2727
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002728 if (widget->resize_handler)
2729 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002730 widget->allocation.width,
2731 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002732 widget->user_data);
2733
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002734 if (window->allocation.width != widget->allocation.width ||
2735 window->allocation.height != widget->allocation.height) {
2736 window->allocation = widget->allocation;
2737 window_schedule_redraw(window);
2738 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002739}
2740
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002741void
2742window_schedule_resize(struct window *window, int width, int height)
2743{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002744 window->pending_allocation.x = 0;
2745 window->pending_allocation.y = 0;
2746 window->pending_allocation.width = width;
2747 window->pending_allocation.height = height;
2748
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002749 if (window->min_allocation.width == 0)
2750 window->min_allocation = window->pending_allocation;
2751 if (window->pending_allocation.width < window->min_allocation.width)
2752 window->pending_allocation.width = window->min_allocation.width;
2753 if (window->pending_allocation.height < window->min_allocation.height)
2754 window->pending_allocation.height = window->min_allocation.height;
2755
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002756 window->resize_needed = 1;
2757 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002758}
2759
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002760void
2761widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2762{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002763 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002764}
2765
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002766static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002767handle_ping(void *data, struct wl_shell_surface *shell_surface,
2768 uint32_t serial)
2769{
2770 wl_shell_surface_pong(shell_surface, serial);
2771}
2772
2773static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002774handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002775 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002776{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002777 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002778
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002779 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002780 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002781}
2782
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002783static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002784menu_destroy(struct menu *menu)
2785{
2786 widget_destroy(menu->widget);
2787 window_destroy(menu->window);
2788 free(menu);
2789}
2790
2791static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002792handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2793{
2794 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002795 struct menu *menu = window->widget->user_data;
2796
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002797 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002798 * device. Or just use wl_callback. And this really needs to
2799 * be a window vfunc that the menu can set. And we need the
2800 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002801
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002802 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002804 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002805}
2806
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002807static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002808 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002809 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002810 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002811};
2812
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002813void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002814window_get_allocation(struct window *window,
2815 struct rectangle *allocation)
2816{
2817 *allocation = window->allocation;
2818}
2819
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002820static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002821widget_redraw(struct widget *widget)
2822{
2823 struct widget *child;
2824
2825 if (widget->redraw_handler)
2826 widget->redraw_handler(widget, widget->user_data);
2827 wl_list_for_each(child, &widget->child_list, link)
2828 widget_redraw(child);
2829}
2830
2831static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002832frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2833{
2834 struct window *window = data;
2835
2836 wl_callback_destroy(callback);
2837 window->redraw_scheduled = 0;
2838 if (window->redraw_needed)
2839 window_schedule_redraw(window);
2840}
2841
2842static const struct wl_callback_listener listener = {
2843 frame_callback
2844};
2845
2846static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002847idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002848{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002849 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002850 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002851
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002852 if (window->resize_needed)
2853 idle_resize(window);
2854
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002855 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002856 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002857 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002858 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002859 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002860
2861 callback = wl_surface_frame(window->surface);
2862 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002863}
2864
2865void
2866window_schedule_redraw(struct window *window)
2867{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002868 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002869 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002870 window->redraw_task.run = idle_redraw;
2871 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002872 window->redraw_scheduled = 1;
2873 }
2874}
2875
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002876void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002877window_set_fullscreen(struct window *window, int fullscreen)
2878{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002879 if (!window->display->shell)
2880 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002881
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002882 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002883 return;
2884
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002885 if (fullscreen) {
2886 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002887 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002888 wl_shell_surface_set_fullscreen(window->shell_surface,
2889 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2890 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002891 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002892 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002893 wl_shell_surface_set_toplevel(window->shell_surface);
2894 window_schedule_resize(window,
2895 window->saved_allocation.width,
2896 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002897 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002898}
2899
2900void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002901window_set_maximized(struct window *window, int maximized)
2902{
2903 if (!window->display->shell)
2904 return;
2905
2906 if ((window->type == TYPE_MAXIMIZED) == maximized)
2907 return;
2908
2909 if (window->type == TYPE_TOPLEVEL) {
2910 window->saved_allocation = window->allocation;
2911 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2912 window->type = TYPE_MAXIMIZED;
2913 } else {
2914 wl_shell_surface_set_toplevel(window->shell_surface);
2915 window->type = TYPE_TOPLEVEL;
2916 window_schedule_resize(window,
2917 window->saved_allocation.width,
2918 window->saved_allocation.height);
2919 }
2920}
2921
2922void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002923window_set_user_data(struct window *window, void *data)
2924{
2925 window->user_data = data;
2926}
2927
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002928void *
2929window_get_user_data(struct window *window)
2930{
2931 return window->user_data;
2932}
2933
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002934void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002935window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002936 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002937{
2938 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002939}
2940
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002941void
2942window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002943 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002944{
2945 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002946}
2947
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002948void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002949window_set_data_handler(struct window *window, window_data_handler_t handler)
2950{
2951 window->data_handler = handler;
2952}
2953
2954void
2955window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2956{
2957 window->drop_handler = handler;
2958}
2959
2960void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002961window_set_close_handler(struct window *window,
2962 window_close_handler_t handler)
2963{
2964 window->close_handler = handler;
2965}
2966
2967void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002968window_set_fullscreen_handler(struct window *window,
2969 window_fullscreen_handler_t handler)
2970{
2971 window->fullscreen_handler = handler;
2972}
2973
2974void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002975window_set_title(struct window *window, const char *title)
2976{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002977 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002978 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002979 if (window->shell_surface)
2980 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002981}
2982
2983const char *
2984window_get_title(struct window *window)
2985{
2986 return window->title;
2987}
2988
2989void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002990window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2991{
2992 struct text_cursor_position *text_cursor_position =
2993 window->display->text_cursor_position;
2994
Scott Moreau9295ce02012-06-01 12:46:10 -06002995 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002996 return;
2997
2998 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002999 window->surface,
3000 wl_fixed_from_int(x),
3001 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003002}
3003
3004void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003005window_damage(struct window *window, int32_t x, int32_t y,
3006 int32_t width, int32_t height)
3007{
3008 wl_surface_damage(window->surface, x, y, width, height);
3009}
3010
Casey Dahlin9074db52012-04-19 22:50:09 -04003011static void
3012surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01003013 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04003014{
Rob Bradford7507b572012-05-15 17:55:34 +01003015 struct window *window = data;
3016 struct output *output;
3017 struct output *output_found = NULL;
3018 struct window_output *window_output;
3019
3020 wl_list_for_each(output, &window->display->output_list, link) {
3021 if (output->output == wl_output) {
3022 output_found = output;
3023 break;
3024 }
3025 }
3026
3027 if (!output_found)
3028 return;
3029
3030 window_output = malloc (sizeof *window_output);
3031 window_output->output = output_found;
3032
3033 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04003034}
3035
3036static void
3037surface_leave(void *data,
3038 struct wl_surface *wl_surface, struct wl_output *output)
3039{
Rob Bradford7507b572012-05-15 17:55:34 +01003040 struct window *window = data;
3041 struct window_output *window_output;
3042 struct window_output *window_output_found = NULL;
3043
3044 wl_list_for_each(window_output, &window->window_output_list, link) {
3045 if (window_output->output->output == output) {
3046 window_output_found = window_output;
3047 break;
3048 }
3049 }
3050
3051 if (window_output_found) {
3052 wl_list_remove(&window_output_found->link);
3053 free(window_output_found);
3054 }
Casey Dahlin9074db52012-04-19 22:50:09 -04003055}
3056
3057static const struct wl_surface_listener surface_listener = {
3058 surface_enter,
3059 surface_leave
3060};
3061
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003062static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003063window_create_internal(struct display *display,
3064 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003065{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05003066 struct window *window;
3067
3068 window = malloc(sizeof *window);
3069 if (window == NULL)
3070 return NULL;
3071
Kristian Høgsberg78231c82008-11-08 15:06:01 -05003072 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05003073 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003074 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003075 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04003076 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003077 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003078 window->shell_surface =
3079 wl_shell_get_shell_surface(display->shell,
3080 window->surface);
3081 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003082 window->allocation.x = 0;
3083 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003084 window->allocation.width = 0;
3085 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003086 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04003087 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003088 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003089 window->input_region = NULL;
3090 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05003091
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003092 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00003093#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003094 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003095#else
3096 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3097#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05003098 else
3099 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003100
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003101 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003102 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04003103 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003104
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003105 if (window->shell_surface) {
3106 wl_shell_surface_set_user_data(window->shell_surface, window);
3107 wl_shell_surface_add_listener(window->shell_surface,
3108 &shell_surface_listener, window);
3109 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003110
Rob Bradford7507b572012-05-15 17:55:34 +01003111 wl_list_init (&window->window_output_list);
3112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003113 return window;
3114}
3115
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003116struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003117window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003118{
3119 struct window *window;
3120
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003121 window = window_create_internal(display, NULL, TYPE_NONE);
3122 if (!window)
3123 return NULL;
3124
3125 return window;
3126}
3127
3128struct window *
3129window_create_custom(struct display *display)
3130{
3131 struct window *window;
3132
3133 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003134 if (!window)
3135 return NULL;
3136
3137 return window;
3138}
3139
3140struct window *
3141window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003142 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003143{
3144 struct window *window;
3145
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003146 window = window_create_internal(parent->display,
3147 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003148 if (!window)
3149 return NULL;
3150
3151 window->x = x;
3152 window->y = y;
3153
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003154 if (display->shell)
3155 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003156 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003157 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003158
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003159 return window;
3160}
3161
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003162static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003163menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003164{
3165 int next;
3166
3167 next = (sy - 8) / 20;
3168 if (menu->current != next) {
3169 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003170 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003171 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003172}
3173
3174static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003175menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003176 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003177 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003178{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003179 struct menu *menu = data;
3180
3181 if (widget == menu->widget)
3182 menu_set_item(data, y);
3183
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003184 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003185}
3186
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003187static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003188menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003189 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003190{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003191 struct menu *menu = data;
3192
3193 if (widget == menu->widget)
3194 menu_set_item(data, y);
3195
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003196 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003197}
3198
3199static void
3200menu_leave_handler(struct widget *widget, struct input *input, void *data)
3201{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003202 struct menu *menu = data;
3203
3204 if (widget == menu->widget)
3205 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003206}
3207
3208static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003209menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003210 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003211 uint32_t button, enum wl_pointer_button_state state,
3212 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003213
3214{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003215 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003216
Daniel Stone4dbadb12012-05-30 16:31:51 +01003217 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3218 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003219 /* Either relase after press-drag-release or
3220 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003221 menu->func(menu->window->parent,
3222 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003223 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003224 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003225 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003226}
3227
3228static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003229menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003230{
3231 cairo_t *cr;
3232 const int32_t r = 3, margin = 3;
3233 struct menu *menu = data;
3234 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003235 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003236
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003237 cr = cairo_create(window->cairo_surface);
3238 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3239 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3240 cairo_paint(cr);
3241
3242 width = window->allocation.width;
3243 height = window->allocation.height;
3244 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003245
3246 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003247 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3248 cairo_fill(cr);
3249
3250 for (i = 0; i < menu->count; i++) {
3251 if (i == menu->current) {
3252 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3253 cairo_rectangle(cr, margin, i * 20 + margin,
3254 width - 2 * margin, 20);
3255 cairo_fill(cr);
3256 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3257 cairo_move_to(cr, 10, i * 20 + 16);
3258 cairo_show_text(cr, menu->entries[i]);
3259 } else {
3260 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3261 cairo_move_to(cr, 10, i * 20 + 16);
3262 cairo_show_text(cr, menu->entries[i]);
3263 }
3264 }
3265
3266 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003267}
3268
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003269void
3270window_show_menu(struct display *display,
3271 struct input *input, uint32_t time, struct window *parent,
3272 int32_t x, int32_t y,
3273 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003274{
3275 struct window *window;
3276 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003277 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003278
3279 menu = malloc(sizeof *menu);
3280 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003281 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003282
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003283 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003284 if (!window) {
3285 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003286 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003287 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003288
3289 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003290 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003291 menu->entries = entries;
3292 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003293 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003294 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003295 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003296 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003297 window->type = TYPE_MENU;
3298 window->x = x;
3299 window->y = y;
3300
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003301 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003302 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003303 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003304 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003305 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003306
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003307 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003308 widget_set_enter_handler(menu->widget, menu_enter_handler);
3309 widget_set_leave_handler(menu->widget, menu_leave_handler);
3310 widget_set_motion_handler(menu->widget, menu_motion_handler);
3311 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003312
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003313 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003314 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003315}
3316
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003317void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003318window_set_buffer_type(struct window *window, enum window_buffer_type type)
3319{
3320 window->buffer_type = type;
3321}
3322
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003323
3324static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003325display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003326 struct wl_output *wl_output,
3327 int x, int y,
3328 int physical_width,
3329 int physical_height,
3330 int subpixel,
3331 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003332 const char *model,
3333 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003334{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003335 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003336
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003337 output->allocation.x = x;
3338 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003339}
3340
3341static void
3342display_handle_mode(void *data,
3343 struct wl_output *wl_output,
3344 uint32_t flags,
3345 int width,
3346 int height,
3347 int refresh)
3348{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003349 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003350 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003351
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003352 if (flags & WL_OUTPUT_MODE_CURRENT) {
3353 output->allocation.width = width;
3354 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003355 if (display->output_configure_handler)
3356 (*display->output_configure_handler)(
3357 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003358 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003359}
3360
3361static const struct wl_output_listener output_listener = {
3362 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003363 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003364};
3365
3366static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003367display_add_output(struct display *d, uint32_t id)
3368{
3369 struct output *output;
3370
3371 output = malloc(sizeof *output);
3372 if (output == NULL)
3373 return;
3374
3375 memset(output, 0, sizeof *output);
3376 output->display = d;
3377 output->output =
3378 wl_display_bind(d->display, id, &wl_output_interface);
3379 wl_list_insert(d->output_list.prev, &output->link);
3380
3381 wl_output_add_listener(output->output, &output_listener, output);
3382}
3383
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003384static void
3385output_destroy(struct output *output)
3386{
3387 if (output->destroy_handler)
3388 (*output->destroy_handler)(output, output->user_data);
3389
3390 wl_output_destroy(output->output);
3391 wl_list_remove(&output->link);
3392 free(output);
3393}
3394
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003395void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003396display_set_output_configure_handler(struct display *display,
3397 display_output_handler_t handler)
3398{
3399 struct output *output;
3400
3401 display->output_configure_handler = handler;
3402 if (!handler)
3403 return;
3404
3405 wl_list_for_each(output, &display->output_list, link)
3406 (*display->output_configure_handler)(output,
3407 display->user_data);
3408}
3409
3410void
3411output_set_user_data(struct output *output, void *data)
3412{
3413 output->user_data = data;
3414}
3415
3416void *
3417output_get_user_data(struct output *output)
3418{
3419 return output->user_data;
3420}
3421
3422void
3423output_set_destroy_handler(struct output *output,
3424 display_output_handler_t handler)
3425{
3426 output->destroy_handler = handler;
3427 /* FIXME: implement this, once we have way to remove outputs */
3428}
3429
3430void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003431output_get_allocation(struct output *output, struct rectangle *allocation)
3432{
3433 *allocation = output->allocation;
3434}
3435
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003436struct wl_output *
3437output_get_wl_output(struct output *output)
3438{
3439 return output->output;
3440}
3441
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003442static void
Daniel Stone97f68542012-05-30 16:32:01 +01003443fini_xkb(struct input *input)
3444{
3445 xkb_state_unref(input->xkb.state);
3446 xkb_map_unref(input->xkb.keymap);
3447}
3448
3449static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003450display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003451{
3452 struct input *input;
3453
3454 input = malloc(sizeof *input);
3455 if (input == NULL)
3456 return;
3457
3458 memset(input, 0, sizeof *input);
3459 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003460 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003461 input->pointer_focus = NULL;
3462 input->keyboard_focus = NULL;
3463 wl_list_insert(d->input_list.prev, &input->link);
3464
Daniel Stone37816df2012-05-16 18:45:18 +01003465 wl_seat_add_listener(input->seat, &seat_listener, input);
3466 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003467
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003468 input->data_device =
3469 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003470 input->seat);
3471 wl_data_device_add_listener(input->data_device, &data_device_listener,
3472 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003473
3474 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003475
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003476 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3477 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003478 input->repeat_task.run = keyboard_repeat_func;
3479 display_watch_fd(d, input->repeat_timer_fd,
3480 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003481}
3482
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003483static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003484input_destroy(struct input *input)
3485{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003486 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003487 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003488
3489 if (input->drag_offer)
3490 data_offer_destroy(input->drag_offer);
3491
3492 if (input->selection_offer)
3493 data_offer_destroy(input->selection_offer);
3494
3495 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003496 fini_xkb(input);
3497
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003498 wl_surface_destroy(input->pointer_surface);
3499
Pekka Paalanene1207c72011-12-16 12:02:09 +02003500 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003501 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003502 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003503 free(input);
3504}
3505
3506static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003507display_handle_global(struct wl_display *display, uint32_t id,
3508 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003509{
3510 struct display *d = data;
3511
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003512 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003513 d->compositor =
3514 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003515 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003516 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003517 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003518 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003519 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003520 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003521 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003522 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3524 d->data_device_manager =
3525 wl_display_bind(display, id,
3526 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003527 } else if (strcmp(interface, "text_cursor_position") == 0) {
3528 d->text_cursor_position =
3529 wl_display_bind(display, id,
3530 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003531 }
3532}
3533
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003534#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003535static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003536init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003537{
3538 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003539 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003540
Rob Clark6396ed32012-03-11 19:48:41 -05003541#ifdef USE_CAIRO_GLESV2
3542# define GL_BIT EGL_OPENGL_ES2_BIT
3543#else
3544# define GL_BIT EGL_OPENGL_BIT
3545#endif
3546
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003547 static const EGLint argb_cfg_attribs[] = {
3548 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003549 EGL_RED_SIZE, 1,
3550 EGL_GREEN_SIZE, 1,
3551 EGL_BLUE_SIZE, 1,
3552 EGL_ALPHA_SIZE, 1,
3553 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003554 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003555 EGL_NONE
3556 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003557
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003558#ifdef USE_CAIRO_GLESV2
3559 static const EGLint context_attribs[] = {
3560 EGL_CONTEXT_CLIENT_VERSION, 2,
3561 EGL_NONE
3562 };
3563 EGLint api = EGL_OPENGL_ES_API;
3564#else
3565 EGLint *context_attribs = NULL;
3566 EGLint api = EGL_OPENGL_API;
3567#endif
3568
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003569 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003570 if (!eglInitialize(d->dpy, &major, &minor)) {
3571 fprintf(stderr, "failed to initialize display\n");
3572 return -1;
3573 }
3574
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003575 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003576 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3577 return -1;
3578 }
3579
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003580 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3581 &d->argb_config, 1, &n) || n != 1) {
3582 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003583 return -1;
3584 }
3585
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003586 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003587 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003588 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003589 fprintf(stderr, "failed to create context\n");
3590 return -1;
3591 }
3592
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003593 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003594 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003595 return -1;
3596 }
3597
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003598#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003599 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3600 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3601 fprintf(stderr, "failed to get cairo egl argb device\n");
3602 return -1;
3603 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003604#endif
3605
3606 return 0;
3607}
3608
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003609static void
3610fini_egl(struct display *display)
3611{
3612#ifdef HAVE_CAIRO_EGL
3613 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003614#endif
3615
3616 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3617 EGL_NO_CONTEXT);
3618
3619 eglTerminate(display->dpy);
3620 eglReleaseThread();
3621}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003622#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003623
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003624static int
3625event_mask_update(uint32_t mask, void *data)
3626{
3627 struct display *d = data;
3628
3629 d->mask = mask;
3630
3631 return 0;
3632}
3633
3634static void
3635handle_display_data(struct task *task, uint32_t events)
3636{
3637 struct display *display =
3638 container_of(task, struct display, display_task);
3639
3640 wl_display_iterate(display->display, display->mask);
3641}
3642
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003643struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003644display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003645{
3646 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003647
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003648 d = malloc(sizeof *d);
3649 if (d == NULL)
3650 return NULL;
3651
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003652 memset(d, 0, sizeof *d);
3653
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003654 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003655 if (d->display == NULL) {
3656 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003657 return NULL;
3658 }
3659
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003660 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003661 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3662 d->display_task.run = handle_display_data;
3663 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3664
3665 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003666 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003667 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003668
Daniel Stone97f68542012-05-30 16:32:01 +01003669 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003670 if (d->xkb_context == NULL) {
3671 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003672 return NULL;
3673 }
3674
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003675 /* Set up listener so we'll catch all events. */
3676 wl_display_add_global_listener(d->display,
3677 display_handle_global, d);
3678
3679 /* Process connection events. */
3680 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003681#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003682 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003683 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003684#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003685
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003686 d->image_target_texture_2d =
3687 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3688 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3689 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3690
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003691 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003692
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003693 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003694
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003695 wl_list_init(&d->window_list);
3696
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003697 return d;
3698}
3699
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003700static void
3701display_destroy_outputs(struct display *display)
3702{
3703 struct output *tmp;
3704 struct output *output;
3705
3706 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3707 output_destroy(output);
3708}
3709
Pekka Paalanene1207c72011-12-16 12:02:09 +02003710static void
3711display_destroy_inputs(struct display *display)
3712{
3713 struct input *tmp;
3714 struct input *input;
3715
3716 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3717 input_destroy(input);
3718}
3719
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003720void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003721display_destroy(struct display *display)
3722{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003723 if (!wl_list_empty(&display->window_list))
3724 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3725
3726 if (!wl_list_empty(&display->deferred_list))
3727 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3728
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003729 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003730 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003731
Daniel Stone97f68542012-05-30 16:32:01 +01003732 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003733
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003734 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003735 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003736
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003737#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003738 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003739#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003740
Pekka Paalanenc2052982011-12-16 11:41:32 +02003741 if (display->shell)
3742 wl_shell_destroy(display->shell);
3743
3744 if (display->shm)
3745 wl_shm_destroy(display->shm);
3746
3747 if (display->data_device_manager)
3748 wl_data_device_manager_destroy(display->data_device_manager);
3749
3750 wl_compositor_destroy(display->compositor);
3751
3752 close(display->epoll_fd);
3753
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003754 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003755 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003756 free(display);
3757}
3758
3759void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003760display_set_user_data(struct display *display, void *data)
3761{
3762 display->user_data = data;
3763}
3764
3765void *
3766display_get_user_data(struct display *display)
3767{
3768 return display->user_data;
3769}
3770
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003771struct wl_display *
3772display_get_display(struct display *display)
3773{
3774 return display->display;
3775}
3776
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003777struct output *
3778display_get_output(struct display *display)
3779{
3780 return container_of(display->output_list.next, struct output, link);
3781}
3782
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003783struct wl_compositor *
3784display_get_compositor(struct display *display)
3785{
3786 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003787}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003788
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003789uint32_t
3790display_get_serial(struct display *display)
3791{
3792 return display->serial;
3793}
3794
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003795EGLDisplay
3796display_get_egl_display(struct display *d)
3797{
3798 return d->dpy;
3799}
3800
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003801struct wl_data_source *
3802display_create_data_source(struct display *display)
3803{
3804 return wl_data_device_manager_create_data_source(display->data_device_manager);
3805}
3806
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003807EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003808display_get_argb_egl_config(struct display *d)
3809{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003810 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003811}
3812
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003813struct wl_shell *
3814display_get_shell(struct display *display)
3815{
3816 return display->shell;
3817}
3818
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003819int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003820display_acquire_window_surface(struct display *display,
3821 struct window *window,
3822 EGLContext ctx)
3823{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003824#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003825 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003826 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003827
3828 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003829 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003830 device = cairo_surface_get_device(window->cairo_surface);
3831 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003832 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003833
Benjamin Franzke0c991632011-09-27 21:57:31 +02003834 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003835 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003836 ctx = display->argb_ctx;
3837 else
3838 assert(0);
3839 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003840
3841 data = cairo_surface_get_user_data(window->cairo_surface,
3842 &surface_data_key);
3843
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003844 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003845 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003846 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3847 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003848
3849 return 0;
3850#else
3851 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003852#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003853}
3854
3855void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003856display_release_window_surface(struct display *display,
3857 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003858{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003859#ifdef HAVE_CAIRO_EGL
3860 cairo_device_t *device;
3861
3862 device = cairo_surface_get_device(window->cairo_surface);
3863 if (!device)
3864 return;
3865
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003866 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003867 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003868 cairo_device_release(device);
3869#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003870}
3871
3872void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003873display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003874{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003875 wl_list_insert(&display->deferred_list, &task->link);
3876}
3877
3878void
3879display_watch_fd(struct display *display,
3880 int fd, uint32_t events, struct task *task)
3881{
3882 struct epoll_event ep;
3883
3884 ep.events = events;
3885 ep.data.ptr = task;
3886 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3887}
3888
3889void
3890display_run(struct display *display)
3891{
3892 struct task *task;
3893 struct epoll_event ep[16];
3894 int i, count;
3895
Pekka Paalanen826d7952011-12-15 10:14:07 +02003896 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003897 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003898 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003899
3900 while (!wl_list_empty(&display->deferred_list)) {
3901 task = container_of(display->deferred_list.next,
3902 struct task, link);
3903 wl_list_remove(&task->link);
3904 task->run(task, 0);
3905 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003906
3907 if (!display->running)
3908 break;
3909
3910 wl_display_flush(display->display);
3911
3912 count = epoll_wait(display->epoll_fd,
3913 ep, ARRAY_LENGTH(ep), -1);
3914 for (i = 0; i < count; i++) {
3915 task = ep[i].data.ptr;
3916 task->run(task, ep[i].events);
3917 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003918 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003919}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003920
3921void
3922display_exit(struct display *display)
3923{
3924 display->running = 0;
3925}