blob: d374ab9bde7b7a0a7d1c8a47026477767763b657 [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øgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400160
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200161 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500162 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500164 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400165 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500166};
167
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500168struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500169 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300170 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500171 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400172 struct wl_list link;
173 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500174 widget_resize_handler_t resize_handler;
175 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500176 widget_enter_handler_t enter_handler;
177 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500178 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500179 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400180 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500181 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300182 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183};
184
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185struct input {
186 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100187 struct wl_seat *seat;
188 struct wl_pointer *pointer;
189 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300193 uint32_t cursor_anim_start;
194 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300195 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400197 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400198 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400199 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400200 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500202 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500203 struct widget *grab;
204 uint32_t grab_button;
205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400206 struct wl_data_device *data_device;
207 struct data_offer *drag_offer;
208 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100209
210 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100211 struct xkb_keymap *keymap;
212 struct xkb_state *state;
213 xkb_mod_mask_t control_mask;
214 xkb_mod_mask_t alt_mask;
215 xkb_mod_mask_t shift_mask;
216 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400217
218 struct task repeat_task;
219 int repeat_timer_fd;
220 uint32_t repeat_sym;
221 uint32_t repeat_key;
222 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400223};
224
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500225struct output {
226 struct display *display;
227 struct wl_output *output;
228 struct rectangle allocation;
229 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200230
231 display_output_handler_t destroy_handler;
232 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500233};
234
Martin Minarik1998b152012-05-10 02:04:35 +0200235enum frame_button_action {
236 FRAME_BUTTON_NULL = 0,
237 FRAME_BUTTON_ICON = 1,
238 FRAME_BUTTON_CLOSE = 2,
239 FRAME_BUTTON_MINIMIZE = 3,
240 FRAME_BUTTON_MAXIMIZE = 4,
241};
242
243enum frame_button_pointer {
244 FRAME_BUTTON_DEFAULT = 0,
245 FRAME_BUTTON_OVER = 1,
246 FRAME_BUTTON_ACTIVE = 2,
247};
248
249enum frame_button_align {
250 FRAME_BUTTON_RIGHT = 0,
251 FRAME_BUTTON_LEFT = 1,
252};
253
254enum frame_button_decoration {
255 FRAME_BUTTON_NONE = 0,
256 FRAME_BUTTON_FANCY = 1,
257};
258
259struct frame_button {
260 struct widget *widget;
261 struct frame *frame;
262 cairo_surface_t *icon;
263 enum frame_button_action type;
264 enum frame_button_pointer state;
265 struct wl_list link; /* buttons_list */
266 enum frame_button_align align;
267 enum frame_button_decoration decoration;
268};
269
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500270struct frame {
271 struct widget *widget;
272 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200273 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500274};
275
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500276struct menu {
277 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500278 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500279 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500280 const char **entries;
281 uint32_t time;
282 int current;
283 int count;
284 menu_func_t func;
285};
286
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300287struct tooltip {
288 struct widget *parent;
289 struct window *window;
290 struct widget *widget;
291 char *entry;
292 struct task tooltip_task;
293 int tooltip_fd;
294 float x, y;
295};
296
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700297struct shm_pool {
298 struct wl_shm_pool *pool;
299 size_t size;
300 size_t used;
301 void *data;
302};
303
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400304enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300305 CURSOR_DEFAULT = 100,
306 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400307};
308
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500309enum window_location {
310 WINDOW_INTERIOR = 0,
311 WINDOW_RESIZING_TOP = 1,
312 WINDOW_RESIZING_BOTTOM = 2,
313 WINDOW_RESIZING_LEFT = 4,
314 WINDOW_RESIZING_TOP_LEFT = 5,
315 WINDOW_RESIZING_BOTTOM_LEFT = 6,
316 WINDOW_RESIZING_RIGHT = 8,
317 WINDOW_RESIZING_TOP_RIGHT = 9,
318 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
319 WINDOW_RESIZING_MASK = 15,
320 WINDOW_EXTERIOR = 16,
321 WINDOW_TITLEBAR = 17,
322 WINDOW_CLIENT_AREA = 18,
323};
324
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400325const char *option_xkb_layout = "us";
326const char *option_xkb_variant = "";
327const char *option_xkb_options = "";
328
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400329static const struct weston_option xkb_options[] = {
330 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
331 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
332 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400333};
334
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335static const cairo_user_data_key_t surface_data_key;
336struct surface_data {
337 struct wl_buffer *buffer;
338};
339
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500340#define MULT(_d,c,a,t) \
341 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
342
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500343#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400344
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100345struct egl_window_surface_data {
346 struct display *display;
347 struct wl_surface *surface;
348 struct wl_egl_window *window;
349 EGLSurface surf;
350};
351
352static void
353egl_window_surface_data_destroy(void *p)
354{
355 struct egl_window_surface_data *data = p;
356 struct display *d = data->display;
357
358 eglDestroySurface(d->dpy, data->surf);
359 wl_egl_window_destroy(data->window);
360 data->surface = NULL;
361
362 free(p);
363}
364
365static cairo_surface_t *
366display_create_egl_window_surface(struct display *display,
367 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400368 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100369 struct rectangle *rectangle)
370{
371 cairo_surface_t *cairo_surface;
372 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100375
376 data = malloc(sizeof *data);
377 if (data == NULL)
378 return NULL;
379
380 data->display = display;
381 data->surface = surface;
382
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500383 config = display->argb_config;
384 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100387 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400388 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100389
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400390 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500391 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100394 data->surf,
395 rectangle->width,
396 rectangle->height);
397
398 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
399 data, egl_window_surface_data_destroy);
400
401 return cairo_surface;
402}
403
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404#endif
405
406struct wl_buffer *
407display_get_buffer_for_surface(struct display *display,
408 cairo_surface_t *surface)
409{
410 struct surface_data *data;
411
412 data = cairo_surface_get_user_data (surface, &surface_data_key);
413
414 return data->buffer;
415}
416
417struct shm_surface_data {
418 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700419 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420};
421
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500422static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700423shm_pool_destroy(struct shm_pool *pool);
424
425static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426shm_surface_data_destroy(void *p)
427{
428 struct shm_surface_data *data = p;
429
430 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700431 if (data->pool)
432 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300433
434 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435}
436
Kristian Høgsberg16626282012-04-03 11:21:27 -0400437static struct wl_shm_pool *
438make_shm_pool(struct display *display, int size, void **data)
439{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400440 struct wl_shm_pool *pool;
441 int fd;
442
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300443 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400444 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300445 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
446 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400447 return NULL;
448 }
449
450 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400451 if (*data == MAP_FAILED) {
452 fprintf(stderr, "mmap failed: %m\n");
453 close(fd);
454 return NULL;
455 }
456
457 pool = wl_shm_create_pool(display->shm, fd, size);
458
459 close(fd);
460
461 return pool;
462}
463
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700464static struct shm_pool *
465shm_pool_create(struct display *display, size_t size)
466{
467 struct shm_pool *pool = malloc(sizeof *pool);
468
469 if (!pool)
470 return NULL;
471
472 pool->pool = make_shm_pool(display, size, &pool->data);
473 if (!pool->pool) {
474 free(pool);
475 return NULL;
476 }
477
478 pool->size = size;
479 pool->used = 0;
480
481 return pool;
482}
483
484static void *
485shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
486{
487 if (pool->used + size > pool->size)
488 return NULL;
489
490 *offset = pool->used;
491 pool->used += size;
492
493 return (char *) pool->data + *offset;
494}
495
496/* destroy the pool. this does not unmap the memory though */
497static void
498shm_pool_destroy(struct shm_pool *pool)
499{
500 munmap(pool->data, pool->size);
501 wl_shm_pool_destroy(pool->pool);
502 free(pool);
503}
504
505/* Start allocating from the beginning of the pool again */
506static void
507shm_pool_reset(struct shm_pool *pool)
508{
509 pool->used = 0;
510}
511
512static int
513data_length_for_shm_surface(struct rectangle *rect)
514{
515 int stride;
516
517 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 rect->width);
519 return stride * rect->height;
520}
521
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500522static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700523display_create_shm_surface_from_pool(struct display *display,
524 struct rectangle *rectangle,
525 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526{
527 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400528 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400529 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700530 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400531 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532
533 data = malloc(sizeof *data);
534 if (data == NULL)
535 return NULL;
536
537 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
538 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700539 length = stride * rectangle->height;
540 data->pool = NULL;
541 map = shm_pool_allocate(pool, length, &offset);
542
543 if (!map) {
544 free(data);
545 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400546 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 CAIRO_FORMAT_ARGB32,
550 rectangle->width,
551 rectangle->height,
552 stride);
553
554 cairo_surface_set_user_data (surface, &surface_data_key,
555 data, shm_surface_data_destroy);
556
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500558 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400559 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500560 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400561
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700562 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400563 rectangle->width,
564 rectangle->height,
565 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700567 return surface;
568}
569
570static cairo_surface_t *
571display_create_shm_surface(struct display *display,
572 struct rectangle *rectangle, uint32_t flags,
573 struct window *window)
574{
575 struct shm_surface_data *data;
576 struct shm_pool *pool;
577 cairo_surface_t *surface;
578
579 if (window && window->pool) {
580 shm_pool_reset(window->pool);
581 surface = display_create_shm_surface_from_pool(display,
582 rectangle,
583 flags,
584 window->pool);
585 if (surface)
586 return surface;
587 }
588
589 pool = shm_pool_create(display,
590 data_length_for_shm_surface(rectangle));
591 if (!pool)
592 return NULL;
593
594 surface =
595 display_create_shm_surface_from_pool(display, rectangle,
596 flags, pool);
597
598 if (!surface) {
599 shm_pool_destroy(pool);
600 return NULL;
601 }
602
603 /* make sure we destroy the pool when the surface is destroyed */
604 data = cairo_surface_get_user_data(surface, &surface_data_key);
605 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606
607 return surface;
608}
609
nobled7b87cb02011-02-01 18:51:47 +0000610static int
611check_size(struct rectangle *rect)
612{
613 if (rect->width && rect->height)
614 return 0;
615
616 fprintf(stderr, "tried to create surface of "
617 "width: %d, height: %d\n", rect->width, rect->height);
618 return -1;
619}
620
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621cairo_surface_t *
622display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400624 struct rectangle *rectangle,
625 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400626{
nobled7b87cb02011-02-01 18:51:47 +0000627 if (check_size(rectangle) < 0)
628 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500629#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300630 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400631 return display_create_egl_window_surface(display,
632 surface,
633 flags,
634 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400636 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637}
638
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300639static const char *cursors[] = {
640 "bottom_left_corner",
641 "bottom_right_corner",
642 "bottom_side",
643 "grabbing",
644 "left_ptr",
645 "left_side",
646 "right_side",
647 "top_left_corner",
648 "top_right_corner",
649 "top_side",
650 "xterm",
651 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400652 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300653};
654
655static void
656create_cursors(struct display *display)
657{
658 unsigned int i;
659
660 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
661 display->cursors =
662 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
663
664 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
665 display->cursors[i] =
666 wl_cursor_theme_get_cursor(display->cursor_theme,
667 cursors[i]);
668}
669
670static void
671destroy_cursors(struct display *display)
672{
673 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800674 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300675}
676
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300677struct wl_cursor_image *
678display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400679{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300680 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300681 wl_cursor_theme_get_cursor(display->cursor_theme,
682 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400683
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300684 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400685}
686
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400687static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200688window_get_resize_dx_dy(struct window *window, int *x, int *y)
689{
690 if (window->resize_edges & WINDOW_RESIZING_LEFT)
691 *x = window->server_allocation.width - window->allocation.width;
692 else
693 *x = 0;
694
695 if (window->resize_edges & WINDOW_RESIZING_TOP)
696 *y = window->server_allocation.height -
697 window->allocation.height;
698 else
699 *y = 0;
700
701 window->resize_edges = 0;
702}
703
704static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500705window_attach_surface(struct window *window)
706{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400707 struct display *display = window->display;
708 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000709#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100710 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000711#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500712 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400714 if (window->type == TYPE_NONE) {
715 window->type = TYPE_TOPLEVEL;
716 if (display->shell)
717 wl_shell_surface_set_toplevel(window->shell_surface);
718 }
719
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000721#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100722 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
723 data = cairo_surface_get_user_data(window->cairo_surface,
724 &surface_data_key);
725
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 cairo_gl_surface_swapbuffers(window->cairo_surface);
727 wl_egl_window_get_attached_size(data->window,
728 &window->server_allocation.width,
729 &window->server_allocation.height);
730 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000731#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 buffer =
734 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400735 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200737 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400739 wl_surface_damage(window->surface, 0, 0,
740 window->allocation.width,
741 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400743 cairo_surface_destroy(window->cairo_surface);
744 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000746 default:
747 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100748 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500749
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500750 if (window->input_region) {
751 wl_surface_set_input_region(window->surface,
752 window->input_region);
753 wl_region_destroy(window->input_region);
754 window->input_region = NULL;
755 }
756
757 if (window->opaque_region) {
758 wl_surface_set_opaque_region(window->surface,
759 window->opaque_region);
760 wl_region_destroy(window->opaque_region);
761 window->opaque_region = NULL;
762 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500763}
764
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500765void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400766window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500767{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400768 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100769 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500770}
771
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772void
773window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400774{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500775 cairo_surface_reference(surface);
776
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400777 if (window->cairo_surface != NULL)
778 cairo_surface_destroy(window->cairo_surface);
779
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500780 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400781}
782
Benjamin Franzke22d54812011-07-16 19:50:32 +0000783#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784static void
785window_resize_cairo_window_surface(struct window *window)
786{
787 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789
790 data = cairo_surface_get_user_data(window->cairo_surface,
791 &surface_data_key);
792
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200793 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 wl_egl_window_resize(data->window,
795 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200796 window->allocation.height,
797 x,y);
798
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799 cairo_gl_surface_set_size(window->cairo_surface,
800 window->allocation.width,
801 window->allocation.height);
802}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000803#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100804
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400805struct display *
806window_get_display(struct window *window)
807{
808 return window->display;
809}
810
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400811void
812window_create_surface(struct window *window)
813{
814 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400815 uint32_t flags = 0;
816
817 if (!window->transparent)
818 flags = SURFACE_OPAQUE;
819
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500821#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
823 if (window->cairo_surface) {
824 window_resize_cairo_window_surface(window);
825 return;
826 }
827 surface = display_create_surface(window->display,
828 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831#endif
832 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400833 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400834 &window->allocation,
835 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400836 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800837 default:
838 surface = NULL;
839 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400840 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841
842 window_set_surface(window, surface);
843 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400844}
845
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200846static void frame_destroy(struct frame *frame);
847
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400848void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500849window_destroy(struct window *window)
850{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200851 struct display *display = window->display;
852 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100853 struct window_output *window_output;
854 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200855
856 if (window->redraw_scheduled)
857 wl_list_remove(&window->redraw_task.link);
858
859 wl_list_for_each(input, &display->input_list, link) {
860 if (input->pointer_focus == window)
861 input->pointer_focus = NULL;
862 if (input->keyboard_focus == window)
863 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500864 if (input->focus_widget &&
865 input->focus_widget->window == window)
866 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200867 }
868
Rob Bradford7507b572012-05-15 17:55:34 +0100869 wl_list_for_each_safe(window_output, window_output_tmp,
870 &window->window_output_list, link) {
871 free (window_output);
872 }
873
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500874 if (window->input_region)
875 wl_region_destroy(window->input_region);
876 if (window->opaque_region)
877 wl_region_destroy(window->opaque_region);
878
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200879 if (window->frame)
880 frame_destroy(window->frame);
881
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200882 if (window->shell_surface)
883 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500884 wl_surface_destroy(window->surface);
885 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200886
887 if (window->cairo_surface != NULL)
888 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200889
890 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500891 free(window);
892}
893
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500894static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500895widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400896{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500897 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400898
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500899 wl_list_for_each(child, &widget->child_list, link) {
900 target = widget_find_widget(child, x, y);
901 if (target)
902 return target;
903 }
904
905 if (widget->allocation.x <= x &&
906 x < widget->allocation.x + widget->allocation.width &&
907 widget->allocation.y <= y &&
908 y < widget->allocation.y + widget->allocation.height) {
909 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910 }
911
912 return NULL;
913}
914
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500915static struct widget *
916widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400917{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400919
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500920 widget = malloc(sizeof *widget);
921 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500922 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500923 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500924 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500925 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500926 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300927 widget->tooltip = NULL;
928 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500929
930 return widget;
931}
932
933struct widget *
934window_add_widget(struct window *window, void *data)
935{
936 window->widget = widget_create(window, data);
937 wl_list_init(&window->widget->link);
938
939 return window->widget;
940}
941
942struct widget *
943widget_add_widget(struct widget *parent, void *data)
944{
945 struct widget *widget;
946
947 widget = widget_create(parent->window, data);
948 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500950 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400951}
952
953void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500954widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400955{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200956 struct display *display = widget->window->display;
957 struct input *input;
958
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300959 if (widget->tooltip) {
960 free(widget->tooltip);
961 widget->tooltip = NULL;
962 }
963
Pekka Paalanene156fb62012-01-19 13:51:38 +0200964 wl_list_for_each(input, &display->input_list, link) {
965 if (input->focus_widget == widget)
966 input->focus_widget = NULL;
967 }
968
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500969 wl_list_remove(&widget->link);
970 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400971}
972
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400973void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500974widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400975{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500976 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977}
978
979void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500980widget_set_size(struct widget *widget, int32_t width, int32_t height)
981{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500982 widget->allocation.width = width;
983 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500984}
985
986void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500987widget_set_allocation(struct widget *widget,
988 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500990 widget->allocation.x = x;
991 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200992 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993}
994
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500995void
996widget_set_transparent(struct widget *widget, int transparent)
997{
998 widget->opaque = !transparent;
999}
1000
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001004 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005}
1006
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001007void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001008widget_set_resize_handler(struct widget *widget,
1009 widget_resize_handler_t handler)
1010{
1011 widget->resize_handler = handler;
1012}
1013
1014void
1015widget_set_redraw_handler(struct widget *widget,
1016 widget_redraw_handler_t handler)
1017{
1018 widget->redraw_handler = handler;
1019}
1020
1021void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001022widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001023{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001024 widget->enter_handler = handler;
1025}
1026
1027void
1028widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1029{
1030 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001031}
1032
1033void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001034widget_set_motion_handler(struct widget *widget,
1035 widget_motion_handler_t handler)
1036{
1037 widget->motion_handler = handler;
1038}
1039
1040void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001041widget_set_button_handler(struct widget *widget,
1042 widget_button_handler_t handler)
1043{
1044 widget->button_handler = handler;
1045}
1046
1047void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001048widget_schedule_redraw(struct widget *widget)
1049{
1050 window_schedule_redraw(widget->window);
1051}
1052
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001053cairo_surface_t *
1054window_get_surface(struct window *window)
1055{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001056 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001057}
1058
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001059struct wl_surface *
1060window_get_wl_surface(struct window *window)
1061{
1062 return window->surface;
1063}
1064
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001065struct wl_shell_surface *
1066window_get_wl_shell_surface(struct window *window)
1067{
1068 return window->shell_surface;
1069}
1070
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001071static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001072tooltip_redraw_handler(struct widget *widget, void *data)
1073{
1074 cairo_t *cr;
1075 const int32_t r = 3;
1076 struct tooltip *tooltip = data;
1077 int32_t width, height;
1078 struct window *window = widget->window;
1079
1080 cr = cairo_create(window->cairo_surface);
1081 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1082 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1083 cairo_paint(cr);
1084
1085 width = window->allocation.width;
1086 height = window->allocation.height;
1087 rounded_rect(cr, 0, 0, width, height, r);
1088
1089 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1090 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1091 cairo_fill(cr);
1092
1093 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1094 cairo_move_to(cr, 10, 16);
1095 cairo_show_text(cr, tooltip->entry);
1096 cairo_destroy(cr);
1097}
1098
1099static cairo_text_extents_t
1100get_text_extents(struct tooltip *tooltip)
1101{
1102 struct window *window;
1103 cairo_t *cr;
1104 cairo_text_extents_t extents;
1105
1106 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1107 * created yet */
1108 window = tooltip->widget->window->parent;
1109 cr = cairo_create(window->cairo_surface);
1110 cairo_text_extents(cr, tooltip->entry, &extents);
1111 cairo_destroy(cr);
1112
1113 return extents;
1114}
1115
1116static int
1117window_create_tooltip(struct tooltip *tooltip)
1118{
1119 struct widget *parent = tooltip->parent;
1120 struct display *display = parent->window->display;
1121 struct window *window;
1122 const int offset_y = 27;
1123 const int margin = 3;
1124 cairo_text_extents_t extents;
1125
1126 if (tooltip->widget)
1127 return 0;
1128
1129 window = window_create_transient(display, parent->window, tooltip->x,
1130 tooltip->y + offset_y,
1131 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1132 if (!window)
1133 return -1;
1134
1135 tooltip->window = window;
1136 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1137
1138 extents = get_text_extents(tooltip);
1139 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1140 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1141
1142 return 0;
1143}
1144
1145void
1146widget_destroy_tooltip(struct widget *parent)
1147{
1148 struct tooltip *tooltip = parent->tooltip;
1149
1150 parent->tooltip_count = 0;
1151 if (!tooltip)
1152 return;
1153
1154 if (tooltip->widget) {
1155 widget_destroy(tooltip->widget);
1156 window_destroy(tooltip->window);
1157 tooltip->widget = NULL;
1158 tooltip->window = NULL;
1159 }
1160
1161 close(tooltip->tooltip_fd);
1162 free(tooltip->entry);
1163 free(tooltip);
1164 parent->tooltip = NULL;
1165}
1166
1167static void
1168tooltip_func(struct task *task, uint32_t events)
1169{
1170 struct tooltip *tooltip =
1171 container_of(task, struct tooltip, tooltip_task);
1172 uint64_t exp;
1173
Martin Olsson8df662a2012-07-08 03:03:47 +02001174 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1175 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001176 window_create_tooltip(tooltip);
1177}
1178
1179#define TOOLTIP_TIMEOUT 500
1180static int
1181tooltip_timer_reset(struct tooltip *tooltip)
1182{
1183 struct itimerspec its;
1184
1185 its.it_interval.tv_sec = 0;
1186 its.it_interval.tv_nsec = 0;
1187 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1188 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1189 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1190 fprintf(stderr, "could not set timerfd\n: %m");
1191 return -1;
1192 }
1193
1194 return 0;
1195}
1196
1197int
1198widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1199{
1200 struct tooltip *tooltip = parent->tooltip;
1201
1202 parent->tooltip_count++;
1203 if (tooltip) {
1204 tooltip->x = x;
1205 tooltip->y = y;
1206 tooltip_timer_reset(tooltip);
1207 return 0;
1208 }
1209
1210 /* the handler might be triggered too fast via input device motion, so
1211 * we need this check here to make sure tooltip is fully initialized */
1212 if (parent->tooltip_count > 1)
1213 return 0;
1214
1215 tooltip = malloc(sizeof *tooltip);
1216 if (!tooltip)
1217 return -1;
1218
1219 parent->tooltip = tooltip;
1220 tooltip->parent = parent;
1221 tooltip->widget = NULL;
1222 tooltip->window = NULL;
1223 tooltip->x = x;
1224 tooltip->y = y;
1225 tooltip->entry = strdup(entry);
1226 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1227 if (tooltip->tooltip_fd < 0) {
1228 fprintf(stderr, "could not create timerfd\n: %m");
1229 return -1;
1230 }
1231
1232 tooltip->tooltip_task.run = tooltip_func;
1233 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1234 EPOLLIN, &tooltip->tooltip_task);
1235 tooltip_timer_reset(tooltip);
1236
1237 return 0;
1238}
1239
1240static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241frame_resize_handler(struct widget *widget,
1242 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 struct frame *frame = data;
1245 struct widget *child = frame->child;
1246 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001247 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001248 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001249 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001250 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001252 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001254 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001255 decoration_width = (t->width + t->margin) * 2;
1256 decoration_height = t->width +
1257 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001259 allocation.x = t->width + t->margin;
1260 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001261 allocation.width = width - decoration_width;
1262 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001263
Kristian Høgsbergdd263e52012-07-09 22:22:37 -04001264 widget->window->input_region =
1265 wl_compositor_create_region(display->compositor);
1266 wl_region_add(widget->window->input_region,
1267 t->margin, t->margin,
1268 width - 2 * t->margin,
1269 height - 2 * t->margin);
1270
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001271 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001272
1273 wl_list_for_each(button, &frame->buttons_list, link)
1274 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001275 } else {
1276 decoration_width = 0;
1277 decoration_height = 0;
1278
1279 allocation.x = 0;
1280 allocation.y = 0;
1281 allocation.width = width;
1282 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001283 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001284
1285 wl_list_for_each(button, &frame->buttons_list, link)
1286 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001287 }
1288
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289 widget_set_allocation(child, allocation.x, allocation.y,
1290 allocation.width, allocation.height);
1291
1292 if (child->resize_handler)
1293 child->resize_handler(child,
1294 allocation.width,
1295 allocation.height,
1296 child->user_data);
1297
Scott Moreauf7e498c2012-05-14 11:39:29 -06001298 width = child->allocation.width + decoration_width;
1299 height = child->allocation.height + decoration_height;
1300
Scott Moreauf7e498c2012-05-14 11:39:29 -06001301 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001302
1303 if (child->opaque) {
1304 widget->window->opaque_region =
1305 wl_compositor_create_region(display->compositor);
1306 wl_region_add(widget->window->opaque_region,
1307 opaque_margin, opaque_margin,
1308 widget->allocation.width - 2 * opaque_margin,
1309 widget->allocation.height - 2 * opaque_margin);
1310 }
Martin Minarik1998b152012-05-10 02:04:35 +02001311
1312 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001313 x_r = frame->widget->allocation.width - t->width - t->margin;
1314 x_l = t->width + t->margin;
1315 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001316 wl_list_for_each(button, &frame->buttons_list, link) {
1317 const int button_padding = 4;
1318 w = cairo_image_surface_get_width(button->icon);
1319 h = cairo_image_surface_get_height(button->icon);
1320
1321 if (button->decoration == FRAME_BUTTON_FANCY)
1322 w += 10;
1323
1324 if (button->align == FRAME_BUTTON_LEFT) {
1325 widget_set_allocation(button->widget,
1326 x_l, y , w + 1, h + 1);
1327 x_l += w;
1328 x_l += button_padding;
1329 } else {
1330 x_r -= w;
1331 widget_set_allocation(button->widget,
1332 x_r, y , w + 1, h + 1);
1333 x_r -= button_padding;
1334 }
1335 }
1336}
1337
1338static int
1339frame_button_enter_handler(struct widget *widget,
1340 struct input *input, float x, float y, void *data)
1341{
1342 struct frame_button *frame_button = data;
1343
1344 widget_schedule_redraw(frame_button->widget);
1345 frame_button->state = FRAME_BUTTON_OVER;
1346
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001347 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001348}
1349
1350static void
1351frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1352{
1353 struct frame_button *frame_button = data;
1354
1355 widget_schedule_redraw(frame_button->widget);
1356 frame_button->state = FRAME_BUTTON_DEFAULT;
1357}
1358
1359static void
1360frame_button_button_handler(struct widget *widget,
1361 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001362 uint32_t button,
1363 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001364{
1365 struct frame_button *frame_button = data;
1366 struct window *window = widget->window;
1367
1368 if (button != BTN_LEFT)
1369 return;
1370
1371 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001372 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001373 frame_button->state = FRAME_BUTTON_ACTIVE;
1374 widget_schedule_redraw(frame_button->widget);
1375
1376 if (frame_button->type == FRAME_BUTTON_ICON)
1377 window_show_frame_menu(window, input, time);
1378 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001379 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001380 frame_button->state = FRAME_BUTTON_DEFAULT;
1381 widget_schedule_redraw(frame_button->widget);
1382 break;
1383 }
1384
1385 switch (frame_button->type) {
1386 case FRAME_BUTTON_CLOSE:
1387 if (window->close_handler)
1388 window->close_handler(window->parent,
1389 window->user_data);
1390 else
1391 display_exit(window->display);
1392 break;
1393 case FRAME_BUTTON_MINIMIZE:
1394 fprintf(stderr,"Minimize stub\n");
1395 break;
1396 case FRAME_BUTTON_MAXIMIZE:
1397 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1398 break;
1399 default:
1400 /* Unknown operation */
1401 break;
1402 }
1403}
1404
1405static void
1406frame_button_redraw_handler(struct widget *widget, void *data)
1407{
1408 struct frame_button *frame_button = data;
1409 cairo_t *cr;
1410 int width, height, x, y;
1411 struct window *window = widget->window;
1412
1413 x = widget->allocation.x;
1414 y = widget->allocation.y;
1415 width = widget->allocation.width;
1416 height = widget->allocation.height;
1417
1418 if (!width)
1419 return;
1420 if (!height)
1421 return;
1422 if (widget->opaque)
1423 return;
1424
1425 cr = cairo_create(window->cairo_surface);
1426
1427 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1428 cairo_set_line_width(cr, 1);
1429
1430 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1431 cairo_rectangle (cr, x, y, 25, 16);
1432
1433 cairo_stroke_preserve(cr);
1434
1435 switch (frame_button->state) {
1436 case FRAME_BUTTON_DEFAULT:
1437 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1438 break;
1439 case FRAME_BUTTON_OVER:
1440 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1441 break;
1442 case FRAME_BUTTON_ACTIVE:
1443 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1444 break;
1445 }
1446
1447 cairo_fill (cr);
1448
1449 x += 4;
1450 }
1451
1452 cairo_set_source_surface(cr, frame_button->icon, x, y);
1453 cairo_paint(cr);
1454
1455 cairo_destroy(cr);
1456}
1457
1458static struct widget *
1459frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1460 enum frame_button_align align, enum frame_button_decoration style)
1461{
1462 struct frame_button *frame_button;
1463 const char *icon = data;
1464
1465 frame_button = malloc (sizeof *frame_button);
1466 memset(frame_button, 0, sizeof *frame_button);
1467
1468 frame_button->icon = cairo_image_surface_create_from_png(icon);
1469 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1470 frame_button->frame = frame;
1471 frame_button->type = type;
1472 frame_button->align = align;
1473 frame_button->decoration = style;
1474
1475 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1476
1477 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1478 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1479 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1480 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1481 return frame_button->widget;
1482}
1483
1484static void
1485frame_button_destroy(struct frame_button *frame_button)
1486{
1487 widget_destroy(frame_button->widget);
1488 wl_list_remove(&frame_button->link);
1489 cairo_surface_destroy(frame_button->icon);
1490 free(frame_button);
1491
1492 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493}
1494
1495static void
1496frame_redraw_handler(struct widget *widget, void *data)
1497{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001498 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001500 struct theme *t = window->display->theme;
1501 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001502
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001503 if (window->type == TYPE_FULLSCREEN)
1504 return;
1505
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506 cr = cairo_create(window->cairo_surface);
1507
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001508 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001509 flags |= THEME_FRAME_ACTIVE;
1510 theme_render_frame(t, cr, widget->allocation.width,
1511 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001512
1513 cairo_destroy(cr);
1514}
1515
1516static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001517frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001520 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001521
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 location = theme_get_location(t, input->sx, input->sy,
1523 frame->widget->allocation.width,
1524 frame->widget->allocation.height);
1525
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001526 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001533 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001536 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001537 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001538 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001539 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001540 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001541 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001542 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001543 case THEME_LOCATION_EXTERIOR:
1544 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001545 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001546 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001547 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001548}
1549
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001550static void
1551frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001552{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001553 switch (index) {
1554 case 0: /* close */
1555 if (window->close_handler)
1556 window->close_handler(window->parent,
1557 window->user_data);
1558 else
1559 display_exit(window->display);
1560 break;
1561 case 1: /* fullscreen */
1562 /* we don't have a way to get out of fullscreen for now */
1563 window_set_fullscreen(window, 1);
1564 break;
1565 case 2: /* rotate */
1566 case 3: /* scale */
1567 break;
1568 }
1569}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001570
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001571void
1572window_show_frame_menu(struct window *window,
1573 struct input *input, uint32_t time)
1574{
1575 int32_t x, y;
1576
1577 static const char *entries[] = {
1578 "Close", "Fullscreen", "Rotate", "Scale"
1579 };
1580
1581 input_get_position(input, &x, &y);
1582 window_show_menu(window->display, input, time, window,
1583 x - 10, y - 10, frame_menu_func, entries, 4);
1584}
1585
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586static int
1587frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001588 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001589{
1590 return frame_get_pointer_image_for_location(data, input);
1591}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593static int
1594frame_motion_handler(struct widget *widget,
1595 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001596 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597{
1598 return frame_get_pointer_image_for_location(data, input);
1599}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001600
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602frame_button_handler(struct widget *widget,
1603 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001604 uint32_t button, enum wl_pointer_button_state state,
1605 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001606
1607{
1608 struct frame *frame = data;
1609 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001610 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001612
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001613 location = theme_get_location(display->theme, input->sx, input->sy,
1614 frame->widget->allocation.width,
1615 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001616
Daniel Stone4dbadb12012-05-30 16:31:51 +01001617 if (window->display->shell && button == BTN_LEFT &&
1618 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001620 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621 if (!window->shell_surface)
1622 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001623 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001624 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001625 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001626 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001627 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001629 case THEME_LOCATION_RESIZING_TOP:
1630 case THEME_LOCATION_RESIZING_BOTTOM:
1631 case THEME_LOCATION_RESIZING_LEFT:
1632 case THEME_LOCATION_RESIZING_RIGHT:
1633 case THEME_LOCATION_RESIZING_TOP_LEFT:
1634 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1635 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1636 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001637 if (!window->shell_surface)
1638 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001640
1641 if (!display->dpy) {
1642 /* If we're using shm, allocate a big
1643 pool to create buffers out of while
1644 we resize. We should probably base
1645 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001646 window->pool =
1647 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001648 }
1649
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001650 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001651 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001652 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001653 break;
1654 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001655 } else if (button == BTN_RIGHT &&
1656 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001657 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001658 }
1659}
1660
1661struct widget *
1662frame_create(struct window *window, void *data)
1663{
1664 struct frame *frame;
1665
1666 frame = malloc(sizeof *frame);
1667 memset(frame, 0, sizeof *frame);
1668
1669 frame->widget = window_add_widget(window, frame);
1670 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001671
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001672 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1673 widget_set_resize_handler(frame->widget, frame_resize_handler);
1674 widget_set_enter_handler(frame->widget, frame_enter_handler);
1675 widget_set_motion_handler(frame->widget, frame_motion_handler);
1676 widget_set_button_handler(frame->widget, frame_button_handler);
1677
Martin Minarik1998b152012-05-10 02:04:35 +02001678 /* Create empty list for frame buttons */
1679 wl_list_init(&frame->buttons_list);
1680
1681 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1682 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1683
1684 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1685 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686
1687 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1688 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1689
1690 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1691 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1692
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001693 window->frame = frame;
1694
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001695 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001696}
1697
Kristian Høgsberga1627922012-06-20 17:30:03 -04001698void
1699frame_set_child_size(struct widget *widget, int child_width, int child_height)
1700{
1701 struct display *display = widget->window->display;
1702 struct theme *t = display->theme;
1703 int decoration_width, decoration_height;
1704 int width, height;
1705
1706 if (widget->window->type != TYPE_FULLSCREEN) {
1707 decoration_width = (t->width + t->margin) * 2;
1708 decoration_height = t->width +
1709 t->titlebar_height + t->margin * 2;
1710
1711 width = child_width + decoration_width;
1712 height = child_height + decoration_height;
1713 } else {
1714 width = child_width;
1715 height = child_height;
1716 }
1717
1718 window_schedule_resize(widget->window, width, height);
1719}
1720
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001721static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001722frame_destroy(struct frame *frame)
1723{
Martin Minarik1998b152012-05-10 02:04:35 +02001724 struct frame_button *button, *tmp;
1725
1726 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1727 frame_button_destroy(button);
1728
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001729 /* frame->child must be destroyed by the application */
1730 widget_destroy(frame->widget);
1731 free(frame);
1732}
1733
1734static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001735input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001736 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001737{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001738 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001739 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001741 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001742 return;
1743
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001744 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001745 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001746 widget = old;
1747 if (input->grab)
1748 widget = input->grab;
1749 if (widget->leave_handler)
1750 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001751 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001752 }
1753
1754 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001755 widget = focus;
1756 if (input->grab)
1757 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001758 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001760 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001762
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001763 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001764 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001765}
1766
1767static void
Daniel Stone37816df2012-05-16 18:45:18 +01001768pointer_handle_motion(void *data, struct wl_pointer *pointer,
1769 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001770{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001771 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001772 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001773 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001774 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001775 float sx = wl_fixed_to_double(sx_w);
1776 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001777
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001778 input->sx = sx;
1779 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001780
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001781 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001782 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001783 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001784 }
1785
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001786 if (input->grab)
1787 widget = input->grab;
1788 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001789 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001790 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001791 cursor = widget->motion_handler(input->focus_widget,
1792 input, time, sx, sy,
1793 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001794
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001795 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001796}
1797
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001798void
1799input_grab(struct input *input, struct widget *widget, uint32_t button)
1800{
1801 input->grab = widget;
1802 input->grab_button = button;
1803}
1804
1805void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001807{
1808 struct widget *widget;
1809
1810 input->grab = NULL;
1811 if (input->pointer_focus) {
1812 widget = widget_find_widget(input->pointer_focus->widget,
1813 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001814 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001815 }
1816}
1817
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001818static void
Daniel Stone37816df2012-05-16 18:45:18 +01001819pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001820 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001821{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001822 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001823 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001824 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001825
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001826 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001827 if (input->focus_widget && input->grab == NULL &&
1828 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001829 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001830
Neil Roberts6b28aad2012-01-23 19:11:18 +00001831 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001832 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001833 (*widget->button_handler)(widget,
1834 input, time,
1835 button, state,
1836 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001837
Daniel Stone4dbadb12012-05-30 16:31:51 +01001838 if (input->grab && input->grab_button == button &&
1839 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001840 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001841}
1842
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001843static void
Daniel Stone37816df2012-05-16 18:45:18 +01001844pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001845 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001846{
1847}
1848
1849static void
Daniel Stone37816df2012-05-16 18:45:18 +01001850keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001851 uint32_t serial, uint32_t time, uint32_t key,
1852 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001853{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001854 struct input *input = data;
1855 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001857 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001858 const xkb_keysym_t *syms;
1859 xkb_keysym_t sym;
1860 xkb_mod_mask_t mask;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001861 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001862
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001863 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001864 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001865 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001866 return;
1867
Daniel Stone97f68542012-05-30 16:32:01 +01001868 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001869
Daniel Stone97f68542012-05-30 16:32:01 +01001870 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001871 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001872 XKB_STATE_LATCHED);
1873 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001874 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001875 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001876 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001877 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001878 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001879 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001880
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001881 sym = XKB_KEY_NoSymbol;
1882 if (num_syms == 1)
1883 sym = syms[0];
1884
1885 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001886 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001887 window_set_maximized(window,
1888 window->type != TYPE_MAXIMIZED);
1889 } else if (window->key_handler) {
1890 (*window->key_handler)(window, input, time, key,
1891 sym, state, window->user_data);
1892 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001893
1894 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1895 key == input->repeat_key) {
1896 its.it_interval.tv_sec = 0;
1897 its.it_interval.tv_nsec = 0;
1898 its.it_value.tv_sec = 0;
1899 its.it_value.tv_nsec = 0;
1900 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1901 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1902 input->repeat_sym = sym;
1903 input->repeat_key = key;
1904 input->repeat_time = time;
1905 its.it_interval.tv_sec = 0;
1906 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1907 its.it_value.tv_sec = 0;
1908 its.it_value.tv_nsec = 400 * 1000 * 1000;
1909 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1910 }
1911}
1912
1913static void
1914keyboard_repeat_func(struct task *task, uint32_t events)
1915{
1916 struct input *input =
1917 container_of(task, struct input, repeat_task);
1918 struct window *window = input->keyboard_focus;
1919 uint64_t exp;
1920
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04001921 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1922 /* If we change the timer between the fd becoming
1923 * readable and getting here, there'll be nothing to
1924 * read and we get EAGAIN. */
1925 return;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001926
Kristian Høgsbergbd0cd762012-06-20 15:17:18 -04001927 if (window && window->key_handler) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04001928 (*window->key_handler)(window, input, input->repeat_time,
1929 input->repeat_key, input->repeat_sym,
1930 WL_KEYBOARD_KEY_STATE_PRESSED,
1931 window->user_data);
1932 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001933}
1934
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001935static void
Daniel Stone351eb612012-05-31 15:27:47 -04001936keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1937 uint32_t serial, uint32_t mods_depressed,
1938 uint32_t mods_latched, uint32_t mods_locked,
1939 uint32_t group)
1940{
1941 struct input *input = data;
1942
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001943 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1944 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001945}
1946
1947static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001948input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001949{
1950 struct window *window = input->pointer_focus;
1951
1952 if (!window)
1953 return;
1954
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001955 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001956
Pekka Paalanene1207c72011-12-16 12:02:09 +02001957 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001958 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001959}
1960
1961static void
Daniel Stone37816df2012-05-16 18:45:18 +01001962pointer_handle_enter(void *data, struct wl_pointer *pointer,
1963 uint32_t serial, struct wl_surface *surface,
1964 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001965{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001966 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001967 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001968 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001969 float sx = wl_fixed_to_double(sx_w);
1970 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001971
Martin Minarik1998b152012-05-10 02:04:35 +02001972 if (!surface) {
1973 /* enter event for a window we've just destroyed */
1974 return;
1975 }
1976
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001977 input->display->serial = serial;
1978 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001979 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001980 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001981
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001982 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001983 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001984 window->pool = NULL;
1985 /* Schedule a redraw to free the pool */
1986 window_schedule_redraw(window);
1987 }
1988
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001989 input->sx = sx;
1990 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001991
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001992 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001993 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001994}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001995
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001996static void
Daniel Stone37816df2012-05-16 18:45:18 +01001997pointer_handle_leave(void *data, struct wl_pointer *pointer,
1998 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001999{
2000 struct input *input = data;
2001
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002002 input->display->serial = serial;
2003 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002004}
2005
2006static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002007input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02002008{
2009 struct window *window = input->keyboard_focus;
Kristian Høgsbergd3c69c22012-06-20 22:41:59 -04002010 struct itimerspec its;
2011
2012 its.it_interval.tv_sec = 0;
2013 its.it_interval.tv_nsec = 0;
2014 its.it_value.tv_sec = 0;
2015 its.it_value.tv_nsec = 0;
2016 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002017
2018 if (!window)
2019 return;
2020
2021 window->keyboard_device = NULL;
2022 if (window->keyboard_focus_handler)
2023 (*window->keyboard_focus_handler)(window, NULL,
2024 window->user_data);
2025
2026 input->keyboard_focus = NULL;
2027}
2028
2029static void
Daniel Stone37816df2012-05-16 18:45:18 +01002030keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2031 uint32_t serial, struct wl_surface *surface,
2032 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002033{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002034 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02002035 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002036
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002037 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002038 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002039
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002040 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002041 window->keyboard_device = input;
2042 if (window->keyboard_focus_handler)
2043 (*window->keyboard_focus_handler)(window,
2044 window->keyboard_device,
2045 window->user_data);
2046}
2047
2048static void
Daniel Stone37816df2012-05-16 18:45:18 +01002049keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2050 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002051{
2052 struct input *input = data;
2053
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002054 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002055 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002056}
2057
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002058static void
2059keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2060 uint32_t format, int fd, uint32_t size)
2061{
2062 struct input *input = data;
2063 char *map_str;
2064
2065 if (!data) {
2066 close(fd);
2067 return;
2068 }
2069
2070 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2071 close(fd);
2072 return;
2073 }
2074
2075 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2076 if (map_str == MAP_FAILED) {
2077 close(fd);
2078 return;
2079 }
2080
2081 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2082 map_str,
2083 XKB_KEYMAP_FORMAT_TEXT_V1,
2084 0);
2085 munmap(map_str, size);
2086 close(fd);
2087
2088 if (!input->xkb.keymap) {
2089 fprintf(stderr, "failed to compile keymap\n");
2090 return;
2091 }
2092
2093 input->xkb.state = xkb_state_new(input->xkb.keymap);
2094 if (!input->xkb.state) {
2095 fprintf(stderr, "failed to create XKB state\n");
2096 xkb_map_unref(input->xkb.keymap);
2097 input->xkb.keymap = NULL;
2098 return;
2099 }
2100
2101 input->xkb.control_mask =
2102 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2103 input->xkb.alt_mask =
2104 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2105 input->xkb.shift_mask =
2106 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2107}
2108
Daniel Stone37816df2012-05-16 18:45:18 +01002109static const struct wl_pointer_listener pointer_listener = {
2110 pointer_handle_enter,
2111 pointer_handle_leave,
2112 pointer_handle_motion,
2113 pointer_handle_button,
2114 pointer_handle_axis,
2115};
2116
2117static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002118 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002119 keyboard_handle_enter,
2120 keyboard_handle_leave,
2121 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002122 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002123};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002124
2125static void
Daniel Stone37816df2012-05-16 18:45:18 +01002126seat_handle_capabilities(void *data, struct wl_seat *seat,
2127 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002128{
Daniel Stone37816df2012-05-16 18:45:18 +01002129 struct input *input = data;
2130
2131 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2132 input->pointer = wl_seat_get_pointer(seat);
2133 wl_pointer_set_user_data(input->pointer, input);
2134 wl_pointer_add_listener(input->pointer, &pointer_listener,
2135 input);
2136 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2137 wl_pointer_destroy(input->pointer);
2138 input->pointer = NULL;
2139 }
2140
2141 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2142 input->keyboard = wl_seat_get_keyboard(seat);
2143 wl_keyboard_set_user_data(input->keyboard, input);
2144 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2145 input);
2146 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2147 wl_keyboard_destroy(input->keyboard);
2148 input->keyboard = NULL;
2149 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002150}
2151
Daniel Stone37816df2012-05-16 18:45:18 +01002152static const struct wl_seat_listener seat_listener = {
2153 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002154};
2155
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002156void
2157input_get_position(struct input *input, int32_t *x, int32_t *y)
2158{
2159 *x = input->sx;
2160 *y = input->sy;
2161}
2162
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002163struct display *
2164input_get_display(struct input *input)
2165{
2166 return input->display;
2167}
2168
Daniel Stone37816df2012-05-16 18:45:18 +01002169struct wl_seat *
2170input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002171{
Daniel Stone37816df2012-05-16 18:45:18 +01002172 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002173}
2174
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002175uint32_t
2176input_get_modifiers(struct input *input)
2177{
2178 return input->modifiers;
2179}
2180
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002181struct widget *
2182input_get_focus_widget(struct input *input)
2183{
2184 return input->focus_widget;
2185}
2186
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002187struct data_offer {
2188 struct wl_data_offer *offer;
2189 struct input *input;
2190 struct wl_array types;
2191 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002192
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002193 struct task io_task;
2194 int fd;
2195 data_func_t func;
2196 int32_t x, y;
2197 void *user_data;
2198};
2199
2200static void
2201data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2202{
2203 struct data_offer *offer = data;
2204 char **p;
2205
2206 p = wl_array_add(&offer->types, sizeof *p);
2207 *p = strdup(type);
2208}
2209
2210static const struct wl_data_offer_listener data_offer_listener = {
2211 data_offer_offer,
2212};
2213
2214static void
2215data_offer_destroy(struct data_offer *offer)
2216{
2217 char **p;
2218
2219 offer->refcount--;
2220 if (offer->refcount == 0) {
2221 wl_data_offer_destroy(offer->offer);
2222 for (p = offer->types.data; *p; p++)
2223 free(*p);
2224 wl_array_release(&offer->types);
2225 free(offer);
2226 }
2227}
2228
2229static void
2230data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002231 struct wl_data_device *data_device,
2232 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002233{
2234 struct data_offer *offer;
2235
2236 offer = malloc(sizeof *offer);
2237
2238 wl_array_init(&offer->types);
2239 offer->refcount = 1;
2240 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04002241 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002242 wl_data_offer_add_listener(offer->offer,
2243 &data_offer_listener, offer);
2244}
2245
2246static void
2247data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002248 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002249 wl_fixed_t x_w, wl_fixed_t y_w,
2250 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002251{
2252 struct input *input = data;
2253 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002254 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002255 float x = wl_fixed_to_double(x_w);
2256 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257 char **p;
2258
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002259 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002260 window = wl_surface_get_user_data(surface);
2261 input->pointer_focus = window;
2262
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002263 if (offer) {
2264 input->drag_offer = wl_data_offer_get_user_data(offer);
2265
2266 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2267 *p = NULL;
2268
2269 types_data = input->drag_offer->types.data;
2270 } else {
2271 input->drag_offer = NULL;
2272 types_data = NULL;
2273 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002274
2275 window = input->pointer_focus;
2276 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002277 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002278 window->user_data);
2279}
2280
2281static void
2282data_device_leave(void *data, struct wl_data_device *data_device)
2283{
2284 struct input *input = data;
2285
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002286 if (input->drag_offer) {
2287 data_offer_destroy(input->drag_offer);
2288 input->drag_offer = NULL;
2289 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002290}
2291
2292static void
2293data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002294 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002295{
2296 struct input *input = data;
2297 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002298 float x = wl_fixed_to_double(x_w);
2299 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002300 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002301
2302 input->sx = x;
2303 input->sy = y;
2304
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002305 if (input->drag_offer)
2306 types_data = input->drag_offer->types.data;
2307 else
2308 types_data = NULL;
2309
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002310 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002311 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002312 window->user_data);
2313}
2314
2315static void
2316data_device_drop(void *data, struct wl_data_device *data_device)
2317{
2318 struct input *input = data;
2319 struct window *window = input->pointer_focus;
2320
2321 if (window->drop_handler)
2322 window->drop_handler(window, input,
2323 input->sx, input->sy, window->user_data);
2324}
2325
2326static void
2327data_device_selection(void *data,
2328 struct wl_data_device *wl_data_device,
2329 struct wl_data_offer *offer)
2330{
2331 struct input *input = data;
2332 char **p;
2333
2334 if (input->selection_offer)
2335 data_offer_destroy(input->selection_offer);
2336
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002337 if (offer) {
2338 input->selection_offer = wl_data_offer_get_user_data(offer);
2339 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2340 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002341 } else {
2342 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002343 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002344}
2345
2346static const struct wl_data_device_listener data_device_listener = {
2347 data_device_data_offer,
2348 data_device_enter,
2349 data_device_leave,
2350 data_device_motion,
2351 data_device_drop,
2352 data_device_selection
2353};
2354
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002355static void
2356input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002357{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002359 struct wl_cursor *cursor;
2360 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002362 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002363 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002364 return;
2365
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002366 if (index >= (int) cursor->image_count) {
2367 fprintf(stderr, "cursor index out of range\n");
2368 return;
2369 }
2370
2371 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002372 buffer = wl_cursor_image_get_buffer(image);
2373 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002374 return;
2375
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002376 wl_pointer_set_cursor(input->pointer, input->display->serial,
2377 input->pointer_surface,
2378 image->hotspot_x, image->hotspot_y);
2379 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2380 wl_surface_damage(input->pointer_surface, 0, 0,
2381 image->width, image->height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002382}
2383
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002384static const struct wl_callback_listener pointer_surface_listener;
2385
2386static void
2387pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2388 uint32_t time)
2389{
2390 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002391 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002392 int i;
2393
2394 if (callback) {
2395 assert(callback == input->cursor_frame_cb);
2396 wl_callback_destroy(callback);
2397 input->cursor_frame_cb = NULL;
2398 }
2399
Kristian Høgsbergf3370522012-06-20 23:04:41 -04002400 if (input->current_cursor == CURSOR_BLANK) {
2401 wl_pointer_set_cursor(input->pointer, input->display->serial,
2402 NULL, 0, 0);
2403 return;
2404 }
2405
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002406 if (input->current_cursor == CURSOR_UNSET)
2407 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01002408 cursor = input->display->cursors[input->current_cursor];
2409 if (!cursor)
2410 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002411
2412 /* FIXME We don't have the current time on the first call so we set
2413 * the animation start to the time of the first frame callback. */
2414 if (time == 0)
2415 input->cursor_anim_start = 0;
2416 else if (input->cursor_anim_start == 0)
2417 input->cursor_anim_start = time;
2418
2419 if (time == 0 || input->cursor_anim_start == 0)
2420 i = 0;
2421 else
2422 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2423
2424 input_set_pointer_image_index(input, i);
2425
2426 if (cursor->image_count == 1)
2427 return;
2428
2429 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2430 wl_callback_add_listener(input->cursor_frame_cb,
2431 &pointer_surface_listener, input);
2432}
2433
2434static const struct wl_callback_listener pointer_surface_listener = {
2435 pointer_surface_frame_callback
2436};
2437
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002438void
2439input_set_pointer_image(struct input *input, int pointer)
2440{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002441 int force = 0;
2442
2443 if (input->pointer_enter_serial > input->cursor_serial)
2444 force = 1;
2445
2446 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002447 return;
2448
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002449 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04002450 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03002451 if (!input->cursor_frame_cb)
2452 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03002453 else if (force) {
2454 /* The current frame callback may be stuck if, for instance,
2455 * the set cursor request was processed by the server after
2456 * this client lost the focus. In this case the cursor surface
2457 * might not be mapped and the frame callback wouldn't ever
2458 * complete. Send a set_cursor and attach to try to map the
2459 * cursor surface again so that the callback will finish */
2460 input_set_pointer_image_index(input, 0);
2461 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002462}
2463
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002464struct wl_data_device *
2465input_get_data_device(struct input *input)
2466{
2467 return input->data_device;
2468}
2469
2470void
2471input_set_selection(struct input *input,
2472 struct wl_data_source *source, uint32_t time)
2473{
2474 wl_data_device_set_selection(input->data_device, source, time);
2475}
2476
2477void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002478input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002479{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002480 wl_data_offer_accept(input->drag_offer->offer,
2481 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002482}
2483
2484static void
2485offer_io_func(struct task *task, uint32_t events)
2486{
2487 struct data_offer *offer =
2488 container_of(task, struct data_offer, io_task);
2489 unsigned int len;
2490 char buffer[4096];
2491
2492 len = read(offer->fd, buffer, sizeof buffer);
2493 offer->func(buffer, len,
2494 offer->x, offer->y, offer->user_data);
2495
2496 if (len == 0) {
2497 close(offer->fd);
2498 data_offer_destroy(offer);
2499 }
2500}
2501
2502static void
2503data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2504 data_func_t func, void *user_data)
2505{
2506 int p[2];
2507
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002508 if (pipe2(p, O_CLOEXEC) == -1)
2509 return;
2510
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002511 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2512 close(p[1]);
2513
2514 offer->io_task.run = offer_io_func;
2515 offer->fd = p[0];
2516 offer->func = func;
2517 offer->refcount++;
2518 offer->user_data = user_data;
2519
2520 display_watch_fd(offer->input->display,
2521 offer->fd, EPOLLIN, &offer->io_task);
2522}
2523
2524void
2525input_receive_drag_data(struct input *input, const char *mime_type,
2526 data_func_t func, void *data)
2527{
2528 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2529 input->drag_offer->x = input->sx;
2530 input->drag_offer->y = input->sy;
2531}
2532
2533int
2534input_receive_selection_data(struct input *input, const char *mime_type,
2535 data_func_t func, void *data)
2536{
2537 char **p;
2538
2539 if (input->selection_offer == NULL)
2540 return -1;
2541
2542 for (p = input->selection_offer->types.data; *p; p++)
2543 if (strcmp(mime_type, *p) == 0)
2544 break;
2545
2546 if (*p == NULL)
2547 return -1;
2548
2549 data_offer_receive_data(input->selection_offer,
2550 mime_type, func, data);
2551 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002552}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002553
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002554int
2555input_receive_selection_data_to_fd(struct input *input,
2556 const char *mime_type, int fd)
2557{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002558 if (input->selection_offer)
2559 wl_data_offer_receive(input->selection_offer->offer,
2560 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002561
2562 return 0;
2563}
2564
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002565void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002566window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002567{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002568 if (!window->shell_surface)
2569 return;
2570
Daniel Stone37816df2012-05-16 18:45:18 +01002571 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002572}
2573
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002574static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002575idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002576{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002577 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002578
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002579 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002580 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002581 widget_set_allocation(widget,
2582 window->pending_allocation.x,
2583 window->pending_allocation.y,
2584 window->pending_allocation.width,
2585 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002586
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002587 if (window->input_region) {
2588 wl_region_destroy(window->input_region);
2589 window->input_region = NULL;
2590 }
2591
2592 if (window->opaque_region) {
2593 wl_region_destroy(window->opaque_region);
2594 window->opaque_region = NULL;
2595 }
2596
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002597 if (widget->resize_handler)
2598 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002599 widget->allocation.width,
2600 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002601 widget->user_data);
2602
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002603 if (window->allocation.width != widget->allocation.width ||
2604 window->allocation.height != widget->allocation.height) {
2605 window->allocation = widget->allocation;
2606 window_schedule_redraw(window);
2607 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002608}
2609
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002610void
2611window_schedule_resize(struct window *window, int width, int height)
2612{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002613 window->pending_allocation.x = 0;
2614 window->pending_allocation.y = 0;
2615 window->pending_allocation.width = width;
2616 window->pending_allocation.height = height;
2617
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002618 if (window->min_allocation.width == 0)
2619 window->min_allocation = window->pending_allocation;
2620 if (window->pending_allocation.width < window->min_allocation.width)
2621 window->pending_allocation.width = window->min_allocation.width;
2622 if (window->pending_allocation.height < window->min_allocation.height)
2623 window->pending_allocation.height = window->min_allocation.height;
2624
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002625 window->resize_needed = 1;
2626 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002627}
2628
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002629void
2630widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2631{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002632 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002633}
2634
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002635static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002636handle_ping(void *data, struct wl_shell_surface *shell_surface,
2637 uint32_t serial)
2638{
2639 wl_shell_surface_pong(shell_surface, serial);
2640}
2641
2642static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002643handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002644 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002645{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002646 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002647
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002648 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002649 return;
2650
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002651 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002652 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002653}
2654
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002655static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002656menu_destroy(struct menu *menu)
2657{
2658 widget_destroy(menu->widget);
2659 window_destroy(menu->window);
2660 free(menu);
2661}
2662
2663static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002664handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2665{
2666 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002667 struct menu *menu = window->widget->user_data;
2668
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002669 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002670 * device. Or just use wl_callback. And this really needs to
2671 * be a window vfunc that the menu can set. And we need the
2672 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002673
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002674 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002675 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002676 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002677}
2678
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002679static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002680 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002681 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002682 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002683};
2684
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002685void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002686window_get_allocation(struct window *window,
2687 struct rectangle *allocation)
2688{
2689 *allocation = window->allocation;
2690}
2691
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002692static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002693widget_redraw(struct widget *widget)
2694{
2695 struct widget *child;
2696
2697 if (widget->redraw_handler)
2698 widget->redraw_handler(widget, widget->user_data);
2699 wl_list_for_each(child, &widget->child_list, link)
2700 widget_redraw(child);
2701}
2702
2703static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002704frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2705{
2706 struct window *window = data;
2707
2708 wl_callback_destroy(callback);
2709 window->redraw_scheduled = 0;
2710 if (window->redraw_needed)
2711 window_schedule_redraw(window);
2712}
2713
2714static const struct wl_callback_listener listener = {
2715 frame_callback
2716};
2717
2718static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002719idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002720{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002721 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002722 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002723
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002724 if (window->resize_needed)
2725 idle_resize(window);
2726
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002727 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002728 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002729 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002730 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002731 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002732
2733 callback = wl_surface_frame(window->surface);
2734 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002735}
2736
2737void
2738window_schedule_redraw(struct window *window)
2739{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002740 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002741 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002742 window->redraw_task.run = idle_redraw;
2743 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002744 window->redraw_scheduled = 1;
2745 }
2746}
2747
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002748void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002749window_set_fullscreen(struct window *window, int fullscreen)
2750{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002751 if (!window->display->shell)
2752 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002753
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002754 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002755 return;
2756
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002757 if (fullscreen) {
2758 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002759 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002760 wl_shell_surface_set_fullscreen(window->shell_surface,
2761 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2762 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002763 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002764 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002765 wl_shell_surface_set_toplevel(window->shell_surface);
2766 window_schedule_resize(window,
2767 window->saved_allocation.width,
2768 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002769 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002770}
2771
2772void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002773window_set_maximized(struct window *window, int maximized)
2774{
2775 if (!window->display->shell)
2776 return;
2777
2778 if ((window->type == TYPE_MAXIMIZED) == maximized)
2779 return;
2780
2781 if (window->type == TYPE_TOPLEVEL) {
2782 window->saved_allocation = window->allocation;
2783 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2784 window->type = TYPE_MAXIMIZED;
2785 } else {
2786 wl_shell_surface_set_toplevel(window->shell_surface);
2787 window->type = TYPE_TOPLEVEL;
2788 window_schedule_resize(window,
2789 window->saved_allocation.width,
2790 window->saved_allocation.height);
2791 }
2792}
2793
2794void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002795window_set_user_data(struct window *window, void *data)
2796{
2797 window->user_data = data;
2798}
2799
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002800void *
2801window_get_user_data(struct window *window)
2802{
2803 return window->user_data;
2804}
2805
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002806void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002807window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002808 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002809{
2810 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002811}
2812
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002813void
2814window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002815 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002816{
2817 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002818}
2819
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002820void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002821window_set_data_handler(struct window *window, window_data_handler_t handler)
2822{
2823 window->data_handler = handler;
2824}
2825
2826void
2827window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2828{
2829 window->drop_handler = handler;
2830}
2831
2832void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002833window_set_close_handler(struct window *window,
2834 window_close_handler_t handler)
2835{
2836 window->close_handler = handler;
2837}
2838
2839void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002840window_set_title(struct window *window, const char *title)
2841{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002842 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002843 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002844 if (window->shell_surface)
2845 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002846}
2847
2848const char *
2849window_get_title(struct window *window)
2850{
2851 return window->title;
2852}
2853
2854void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002855window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2856{
2857 struct text_cursor_position *text_cursor_position =
2858 window->display->text_cursor_position;
2859
Scott Moreau9295ce02012-06-01 12:46:10 -06002860 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002861 return;
2862
2863 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002864 window->surface,
2865 wl_fixed_from_int(x),
2866 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002867}
2868
2869void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002870window_damage(struct window *window, int32_t x, int32_t y,
2871 int32_t width, int32_t height)
2872{
2873 wl_surface_damage(window->surface, x, y, width, height);
2874}
2875
Casey Dahlin9074db52012-04-19 22:50:09 -04002876static void
2877surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002878 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002879{
Rob Bradford7507b572012-05-15 17:55:34 +01002880 struct window *window = data;
2881 struct output *output;
2882 struct output *output_found = NULL;
2883 struct window_output *window_output;
2884
2885 wl_list_for_each(output, &window->display->output_list, link) {
2886 if (output->output == wl_output) {
2887 output_found = output;
2888 break;
2889 }
2890 }
2891
2892 if (!output_found)
2893 return;
2894
2895 window_output = malloc (sizeof *window_output);
2896 window_output->output = output_found;
2897
2898 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002899}
2900
2901static void
2902surface_leave(void *data,
2903 struct wl_surface *wl_surface, struct wl_output *output)
2904{
Rob Bradford7507b572012-05-15 17:55:34 +01002905 struct window *window = data;
2906 struct window_output *window_output;
2907 struct window_output *window_output_found = NULL;
2908
2909 wl_list_for_each(window_output, &window->window_output_list, link) {
2910 if (window_output->output->output == output) {
2911 window_output_found = window_output;
2912 break;
2913 }
2914 }
2915
2916 if (window_output_found) {
2917 wl_list_remove(&window_output_found->link);
2918 free(window_output_found);
2919 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002920}
2921
2922static const struct wl_surface_listener surface_listener = {
2923 surface_enter,
2924 surface_leave
2925};
2926
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002927static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002928window_create_internal(struct display *display,
2929 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002930{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002931 struct window *window;
2932
2933 window = malloc(sizeof *window);
2934 if (window == NULL)
2935 return NULL;
2936
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002937 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002938 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002939 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002940 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002941 wl_surface_add_listener(window->surface, &surface_listener, window);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002942 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002943 window->shell_surface =
2944 wl_shell_get_shell_surface(display->shell,
2945 window->surface);
2946 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002947 window->allocation.x = 0;
2948 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002949 window->allocation.width = 0;
2950 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002951 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002952 window->transparent = 1;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002953 window->type = type;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002954 window->input_region = NULL;
2955 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002956
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002957 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002958#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002959 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002960#else
2961 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2962#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002963 else
2964 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002965
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002966 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002967 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002968 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002969
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002970 if (window->shell_surface) {
2971 wl_shell_surface_set_user_data(window->shell_surface, window);
2972 wl_shell_surface_add_listener(window->shell_surface,
2973 &shell_surface_listener, window);
2974 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975
Rob Bradford7507b572012-05-15 17:55:34 +01002976 wl_list_init (&window->window_output_list);
2977
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002978 return window;
2979}
2980
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002981struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002982window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002983{
2984 struct window *window;
2985
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002986 window = window_create_internal(display, NULL, TYPE_NONE);
2987 if (!window)
2988 return NULL;
2989
2990 return window;
2991}
2992
2993struct window *
2994window_create_custom(struct display *display)
2995{
2996 struct window *window;
2997
2998 window = window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002999 if (!window)
3000 return NULL;
3001
3002 return window;
3003}
3004
3005struct window *
3006window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03003007 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003008{
3009 struct window *window;
3010
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003011 window = window_create_internal(parent->display,
3012 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003013 if (!window)
3014 return NULL;
3015
3016 window->x = x;
3017 window->y = y;
3018
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003019 if (display->shell)
3020 wl_shell_surface_set_transient(window->shell_surface,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003021 window->parent->surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03003022 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003023
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003024 return window;
3025}
3026
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003027static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05003028menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003029{
3030 int next;
3031
3032 next = (sy - 8) / 20;
3033 if (menu->current != next) {
3034 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003035 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003036 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003037}
3038
3039static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05003040menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003041 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003042 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003043{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003044 struct menu *menu = data;
3045
3046 if (widget == menu->widget)
3047 menu_set_item(data, y);
3048
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003049 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003050}
3051
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05003052static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003053menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003054 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003055{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003056 struct menu *menu = data;
3057
3058 if (widget == menu->widget)
3059 menu_set_item(data, y);
3060
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03003061 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003062}
3063
3064static void
3065menu_leave_handler(struct widget *widget, struct input *input, void *data)
3066{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003067 struct menu *menu = data;
3068
3069 if (widget == menu->widget)
3070 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003071}
3072
3073static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05003074menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003075 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003076 uint32_t button, enum wl_pointer_button_state state,
3077 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003078
3079{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003080 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003081
Daniel Stone4dbadb12012-05-30 16:31:51 +01003082 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3083 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003084 /* Either relase after press-drag-release or
3085 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003086 menu->func(menu->window->parent,
3087 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003088 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003089 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003090 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003091}
3092
3093static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003094menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003095{
3096 cairo_t *cr;
3097 const int32_t r = 3, margin = 3;
3098 struct menu *menu = data;
3099 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003100 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003101
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003102 cr = cairo_create(window->cairo_surface);
3103 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3104 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3105 cairo_paint(cr);
3106
3107 width = window->allocation.width;
3108 height = window->allocation.height;
3109 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05003110
3111 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003112 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3113 cairo_fill(cr);
3114
3115 for (i = 0; i < menu->count; i++) {
3116 if (i == menu->current) {
3117 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3118 cairo_rectangle(cr, margin, i * 20 + margin,
3119 width - 2 * margin, 20);
3120 cairo_fill(cr);
3121 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3122 cairo_move_to(cr, 10, i * 20 + 16);
3123 cairo_show_text(cr, menu->entries[i]);
3124 } else {
3125 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3126 cairo_move_to(cr, 10, i * 20 + 16);
3127 cairo_show_text(cr, menu->entries[i]);
3128 }
3129 }
3130
3131 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003132}
3133
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003134void
3135window_show_menu(struct display *display,
3136 struct input *input, uint32_t time, struct window *parent,
3137 int32_t x, int32_t y,
3138 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003139{
3140 struct window *window;
3141 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003142 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003143
3144 menu = malloc(sizeof *menu);
3145 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003146 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003147
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003148 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02003149 if (!window) {
3150 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003151 return;
Martin Olsson444799a2012-07-08 03:03:40 +02003152 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003153
3154 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003155 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003156 menu->entries = entries;
3157 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003158 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003159 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05003160 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003161 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003162 window->type = TYPE_MENU;
3163 window->x = x;
3164 window->y = y;
3165
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003166 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003167 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003168 display_get_serial(window->display),
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003169 window->parent->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003170 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003171
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003172 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003173 widget_set_enter_handler(menu->widget, menu_enter_handler);
3174 widget_set_leave_handler(menu->widget, menu_leave_handler);
3175 widget_set_motion_handler(menu->widget, menu_motion_handler);
3176 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003177
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003178 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003179 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003180}
3181
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003182void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003183window_set_buffer_type(struct window *window, enum window_buffer_type type)
3184{
3185 window->buffer_type = type;
3186}
3187
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003188
3189static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003190display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003191 struct wl_output *wl_output,
3192 int x, int y,
3193 int physical_width,
3194 int physical_height,
3195 int subpixel,
3196 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003197 const char *model,
3198 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003199{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003200 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003201
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003202 output->allocation.x = x;
3203 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003204}
3205
3206static void
3207display_handle_mode(void *data,
3208 struct wl_output *wl_output,
3209 uint32_t flags,
3210 int width,
3211 int height,
3212 int refresh)
3213{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003214 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003215 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003216
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003217 if (flags & WL_OUTPUT_MODE_CURRENT) {
3218 output->allocation.width = width;
3219 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003220 if (display->output_configure_handler)
3221 (*display->output_configure_handler)(
3222 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003223 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003224}
3225
3226static const struct wl_output_listener output_listener = {
3227 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003228 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003229};
3230
3231static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003232display_add_output(struct display *d, uint32_t id)
3233{
3234 struct output *output;
3235
3236 output = malloc(sizeof *output);
3237 if (output == NULL)
3238 return;
3239
3240 memset(output, 0, sizeof *output);
3241 output->display = d;
3242 output->output =
3243 wl_display_bind(d->display, id, &wl_output_interface);
3244 wl_list_insert(d->output_list.prev, &output->link);
3245
3246 wl_output_add_listener(output->output, &output_listener, output);
3247}
3248
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003249static void
3250output_destroy(struct output *output)
3251{
3252 if (output->destroy_handler)
3253 (*output->destroy_handler)(output, output->user_data);
3254
3255 wl_output_destroy(output->output);
3256 wl_list_remove(&output->link);
3257 free(output);
3258}
3259
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003260void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003261display_set_output_configure_handler(struct display *display,
3262 display_output_handler_t handler)
3263{
3264 struct output *output;
3265
3266 display->output_configure_handler = handler;
3267 if (!handler)
3268 return;
3269
3270 wl_list_for_each(output, &display->output_list, link)
3271 (*display->output_configure_handler)(output,
3272 display->user_data);
3273}
3274
3275void
3276output_set_user_data(struct output *output, void *data)
3277{
3278 output->user_data = data;
3279}
3280
3281void *
3282output_get_user_data(struct output *output)
3283{
3284 return output->user_data;
3285}
3286
3287void
3288output_set_destroy_handler(struct output *output,
3289 display_output_handler_t handler)
3290{
3291 output->destroy_handler = handler;
3292 /* FIXME: implement this, once we have way to remove outputs */
3293}
3294
3295void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003296output_get_allocation(struct output *output, struct rectangle *allocation)
3297{
3298 *allocation = output->allocation;
3299}
3300
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003301struct wl_output *
3302output_get_wl_output(struct output *output)
3303{
3304 return output->output;
3305}
3306
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003307static void
Daniel Stone97f68542012-05-30 16:32:01 +01003308fini_xkb(struct input *input)
3309{
3310 xkb_state_unref(input->xkb.state);
3311 xkb_map_unref(input->xkb.keymap);
3312}
3313
3314static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003315display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003316{
3317 struct input *input;
3318
3319 input = malloc(sizeof *input);
3320 if (input == NULL)
3321 return;
3322
3323 memset(input, 0, sizeof *input);
3324 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003325 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003326 input->pointer_focus = NULL;
3327 input->keyboard_focus = NULL;
3328 wl_list_insert(d->input_list.prev, &input->link);
3329
Daniel Stone37816df2012-05-16 18:45:18 +01003330 wl_seat_add_listener(input->seat, &seat_listener, input);
3331 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003332
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003333 input->data_device =
3334 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003335 input->seat);
3336 wl_data_device_add_listener(input->data_device, &data_device_listener,
3337 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003338
3339 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003340
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04003341 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3342 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003343 input->repeat_task.run = keyboard_repeat_func;
3344 display_watch_fd(d, input->repeat_timer_fd,
3345 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003346}
3347
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003348static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003349input_destroy(struct input *input)
3350{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003351 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003352 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003353
3354 if (input->drag_offer)
3355 data_offer_destroy(input->drag_offer);
3356
3357 if (input->selection_offer)
3358 data_offer_destroy(input->selection_offer);
3359
3360 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003361 fini_xkb(input);
3362
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003363 wl_surface_destroy(input->pointer_surface);
3364
Pekka Paalanene1207c72011-12-16 12:02:09 +02003365 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003366 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003367 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003368 free(input);
3369}
3370
3371static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003372display_handle_global(struct wl_display *display, uint32_t id,
3373 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003374{
3375 struct display *d = data;
3376
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003377 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003378 d->compositor =
3379 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003380 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003381 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003382 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003383 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003384 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003385 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003386 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003387 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3389 d->data_device_manager =
3390 wl_display_bind(display, id,
3391 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003392 } else if (strcmp(interface, "text_cursor_position") == 0) {
3393 d->text_cursor_position =
3394 wl_display_bind(display, id,
3395 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003396 }
3397}
3398
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003399#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003400static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003401init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003402{
3403 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003404 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003405
Rob Clark6396ed32012-03-11 19:48:41 -05003406#ifdef USE_CAIRO_GLESV2
3407# define GL_BIT EGL_OPENGL_ES2_BIT
3408#else
3409# define GL_BIT EGL_OPENGL_BIT
3410#endif
3411
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003412 static const EGLint argb_cfg_attribs[] = {
3413 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003414 EGL_RED_SIZE, 1,
3415 EGL_GREEN_SIZE, 1,
3416 EGL_BLUE_SIZE, 1,
3417 EGL_ALPHA_SIZE, 1,
3418 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003419 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003420 EGL_NONE
3421 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003422
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003423#ifdef USE_CAIRO_GLESV2
3424 static const EGLint context_attribs[] = {
3425 EGL_CONTEXT_CLIENT_VERSION, 2,
3426 EGL_NONE
3427 };
3428 EGLint api = EGL_OPENGL_ES_API;
3429#else
3430 EGLint *context_attribs = NULL;
3431 EGLint api = EGL_OPENGL_API;
3432#endif
3433
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003434 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003435 if (!eglInitialize(d->dpy, &major, &minor)) {
3436 fprintf(stderr, "failed to initialize display\n");
3437 return -1;
3438 }
3439
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003440 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003441 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3442 return -1;
3443 }
3444
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003445 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3446 &d->argb_config, 1, &n) || n != 1) {
3447 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003448 return -1;
3449 }
3450
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003451 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003452 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003453 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003454 fprintf(stderr, "failed to create context\n");
3455 return -1;
3456 }
3457
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003458 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003459 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003460 return -1;
3461 }
3462
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003463#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003464 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3465 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3466 fprintf(stderr, "failed to get cairo egl argb device\n");
3467 return -1;
3468 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003469#endif
3470
3471 return 0;
3472}
3473
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003474static void
3475fini_egl(struct display *display)
3476{
3477#ifdef HAVE_CAIRO_EGL
3478 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003479#endif
3480
3481 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3482 EGL_NO_CONTEXT);
3483
3484 eglTerminate(display->dpy);
3485 eglReleaseThread();
3486}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003487#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003488
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003489static int
3490event_mask_update(uint32_t mask, void *data)
3491{
3492 struct display *d = data;
3493
3494 d->mask = mask;
3495
3496 return 0;
3497}
3498
3499static void
3500handle_display_data(struct task *task, uint32_t events)
3501{
3502 struct display *display =
3503 container_of(task, struct display, display_task);
3504
3505 wl_display_iterate(display->display, display->mask);
3506}
3507
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003508struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003509display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003510{
3511 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003512
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003513 argc = parse_options(xkb_options,
3514 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003515
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003516 d = malloc(sizeof *d);
3517 if (d == NULL)
3518 return NULL;
3519
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003520 memset(d, 0, sizeof *d);
3521
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003522 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003523 if (d->display == NULL) {
3524 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003525 return NULL;
3526 }
3527
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003528 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003529 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3530 d->display_task.run = handle_display_data;
3531 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3532
3533 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003534 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003535 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003536
Daniel Stone97f68542012-05-30 16:32:01 +01003537 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003538 if (d->xkb_context == NULL) {
3539 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003540 return NULL;
3541 }
3542
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003543 /* Set up listener so we'll catch all events. */
3544 wl_display_add_global_listener(d->display,
3545 display_handle_global, d);
3546
3547 /* Process connection events. */
3548 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003549#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003550 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003551 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003552#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003553
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003554 d->image_target_texture_2d =
3555 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3556 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3557 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3558
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003559 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003560
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003561 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003562
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003563 wl_list_init(&d->window_list);
3564
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003565 return d;
3566}
3567
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003568static void
3569display_destroy_outputs(struct display *display)
3570{
3571 struct output *tmp;
3572 struct output *output;
3573
3574 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3575 output_destroy(output);
3576}
3577
Pekka Paalanene1207c72011-12-16 12:02:09 +02003578static void
3579display_destroy_inputs(struct display *display)
3580{
3581 struct input *tmp;
3582 struct input *input;
3583
3584 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3585 input_destroy(input);
3586}
3587
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003588void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003589display_destroy(struct display *display)
3590{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003591 if (!wl_list_empty(&display->window_list))
3592 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3593
3594 if (!wl_list_empty(&display->deferred_list))
3595 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3596
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003597 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003598 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003599
Daniel Stone97f68542012-05-30 16:32:01 +01003600 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003601
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003602 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003603 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003604
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003605#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003606 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003607#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003608
Pekka Paalanenc2052982011-12-16 11:41:32 +02003609 if (display->shell)
3610 wl_shell_destroy(display->shell);
3611
3612 if (display->shm)
3613 wl_shm_destroy(display->shm);
3614
3615 if (display->data_device_manager)
3616 wl_data_device_manager_destroy(display->data_device_manager);
3617
3618 wl_compositor_destroy(display->compositor);
3619
3620 close(display->epoll_fd);
3621
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003622 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003623 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003624 free(display);
3625}
3626
3627void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003628display_set_user_data(struct display *display, void *data)
3629{
3630 display->user_data = data;
3631}
3632
3633void *
3634display_get_user_data(struct display *display)
3635{
3636 return display->user_data;
3637}
3638
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003639struct wl_display *
3640display_get_display(struct display *display)
3641{
3642 return display->display;
3643}
3644
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003645struct output *
3646display_get_output(struct display *display)
3647{
3648 return container_of(display->output_list.next, struct output, link);
3649}
3650
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003651struct wl_compositor *
3652display_get_compositor(struct display *display)
3653{
3654 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003655}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003656
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003657uint32_t
3658display_get_serial(struct display *display)
3659{
3660 return display->serial;
3661}
3662
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003663EGLDisplay
3664display_get_egl_display(struct display *d)
3665{
3666 return d->dpy;
3667}
3668
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003669struct wl_data_source *
3670display_create_data_source(struct display *display)
3671{
3672 return wl_data_device_manager_create_data_source(display->data_device_manager);
3673}
3674
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003675EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003676display_get_argb_egl_config(struct display *d)
3677{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003678 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003679}
3680
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003681struct wl_shell *
3682display_get_shell(struct display *display)
3683{
3684 return display->shell;
3685}
3686
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003687int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003688display_acquire_window_surface(struct display *display,
3689 struct window *window,
3690 EGLContext ctx)
3691{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003692#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003693 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003694 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003695
3696 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003697 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003698 device = cairo_surface_get_device(window->cairo_surface);
3699 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003700 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003701
Benjamin Franzke0c991632011-09-27 21:57:31 +02003702 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003703 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003704 ctx = display->argb_ctx;
3705 else
3706 assert(0);
3707 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003708
3709 data = cairo_surface_get_user_data(window->cairo_surface,
3710 &surface_data_key);
3711
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003712 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003713 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003714 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3715 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003716
3717 return 0;
3718#else
3719 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003720#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003721}
3722
3723void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003724display_release_window_surface(struct display *display,
3725 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003726{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003727#ifdef HAVE_CAIRO_EGL
3728 cairo_device_t *device;
3729
3730 device = cairo_surface_get_device(window->cairo_surface);
3731 if (!device)
3732 return;
3733
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003734 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003735 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003736 cairo_device_release(device);
3737#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003738}
3739
3740void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003741display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003742{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003743 wl_list_insert(&display->deferred_list, &task->link);
3744}
3745
3746void
3747display_watch_fd(struct display *display,
3748 int fd, uint32_t events, struct task *task)
3749{
3750 struct epoll_event ep;
3751
3752 ep.events = events;
3753 ep.data.ptr = task;
3754 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3755}
3756
3757void
3758display_run(struct display *display)
3759{
3760 struct task *task;
3761 struct epoll_event ep[16];
3762 int i, count;
3763
Pekka Paalanen826d7952011-12-15 10:14:07 +02003764 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003765 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003766 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003767
3768 while (!wl_list_empty(&display->deferred_list)) {
3769 task = container_of(display->deferred_list.next,
3770 struct task, link);
3771 wl_list_remove(&task->link);
3772 task->run(task, 0);
3773 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003774
3775 if (!display->running)
3776 break;
3777
3778 wl_display_flush(display->display);
3779
3780 count = epoll_wait(display->epoll_fd,
3781 ep, ARRAY_LENGTH(ep), -1);
3782 for (i = 0; i < count; i++) {
3783 task = ep[i].data.ptr;
3784 task->run(task, ep[i].events);
3785 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003786 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003787}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003788
3789void
3790display_exit(struct display *display)
3791{
3792 display->running = 0;
3793}