blob: 947240033fa843bc27b1f2236f0115c7ff4194d3 [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øgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400193 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400194 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400196
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500197 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500198 struct widget *grab;
199 uint32_t grab_button;
200
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201 struct wl_data_device *data_device;
202 struct data_offer *drag_offer;
203 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100204
205 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100206 struct xkb_keymap *keymap;
207 struct xkb_state *state;
208 xkb_mod_mask_t control_mask;
209 xkb_mod_mask_t alt_mask;
210 xkb_mod_mask_t shift_mask;
211 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212};
213
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500214struct output {
215 struct display *display;
216 struct wl_output *output;
217 struct rectangle allocation;
218 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200219
220 display_output_handler_t destroy_handler;
221 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500222};
223
Martin Minarik1998b152012-05-10 02:04:35 +0200224enum frame_button_action {
225 FRAME_BUTTON_NULL = 0,
226 FRAME_BUTTON_ICON = 1,
227 FRAME_BUTTON_CLOSE = 2,
228 FRAME_BUTTON_MINIMIZE = 3,
229 FRAME_BUTTON_MAXIMIZE = 4,
230};
231
232enum frame_button_pointer {
233 FRAME_BUTTON_DEFAULT = 0,
234 FRAME_BUTTON_OVER = 1,
235 FRAME_BUTTON_ACTIVE = 2,
236};
237
238enum frame_button_align {
239 FRAME_BUTTON_RIGHT = 0,
240 FRAME_BUTTON_LEFT = 1,
241};
242
243enum frame_button_decoration {
244 FRAME_BUTTON_NONE = 0,
245 FRAME_BUTTON_FANCY = 1,
246};
247
248struct frame_button {
249 struct widget *widget;
250 struct frame *frame;
251 cairo_surface_t *icon;
252 enum frame_button_action type;
253 enum frame_button_pointer state;
254 struct wl_list link; /* buttons_list */
255 enum frame_button_align align;
256 enum frame_button_decoration decoration;
257};
258
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500259struct frame {
260 struct widget *widget;
261 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200262 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500263};
264
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500265struct menu {
266 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500267 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500268 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500269 const char **entries;
270 uint32_t time;
271 int current;
272 int count;
273 menu_func_t func;
274};
275
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276struct tooltip {
277 struct widget *parent;
278 struct window *window;
279 struct widget *widget;
280 char *entry;
281 struct task tooltip_task;
282 int tooltip_fd;
283 float x, y;
284};
285
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700286struct shm_pool {
287 struct wl_shm_pool *pool;
288 size_t size;
289 size_t used;
290 void *data;
291};
292
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400293enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300294 CURSOR_DEFAULT = 100,
295 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400296};
297
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500298enum window_location {
299 WINDOW_INTERIOR = 0,
300 WINDOW_RESIZING_TOP = 1,
301 WINDOW_RESIZING_BOTTOM = 2,
302 WINDOW_RESIZING_LEFT = 4,
303 WINDOW_RESIZING_TOP_LEFT = 5,
304 WINDOW_RESIZING_BOTTOM_LEFT = 6,
305 WINDOW_RESIZING_RIGHT = 8,
306 WINDOW_RESIZING_TOP_RIGHT = 9,
307 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
308 WINDOW_RESIZING_MASK = 15,
309 WINDOW_EXTERIOR = 16,
310 WINDOW_TITLEBAR = 17,
311 WINDOW_CLIENT_AREA = 18,
312};
313
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400314const char *option_xkb_layout = "us";
315const char *option_xkb_variant = "";
316const char *option_xkb_options = "";
317
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400318static const struct weston_option xkb_options[] = {
319 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
320 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
321 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400322};
323
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324static const cairo_user_data_key_t surface_data_key;
325struct surface_data {
326 struct wl_buffer *buffer;
327};
328
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500329#define MULT(_d,c,a,t) \
330 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
331
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500332#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100334struct egl_window_surface_data {
335 struct display *display;
336 struct wl_surface *surface;
337 struct wl_egl_window *window;
338 EGLSurface surf;
339};
340
341static void
342egl_window_surface_data_destroy(void *p)
343{
344 struct egl_window_surface_data *data = p;
345 struct display *d = data->display;
346
347 eglDestroySurface(d->dpy, data->surf);
348 wl_egl_window_destroy(data->window);
349 data->surface = NULL;
350
351 free(p);
352}
353
354static cairo_surface_t *
355display_create_egl_window_surface(struct display *display,
356 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100358 struct rectangle *rectangle)
359{
360 cairo_surface_t *cairo_surface;
361 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200363 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
369 data->display = display;
370 data->surface = surface;
371
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500372 config = display->argb_config;
373 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400375 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500380 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383 data->surf,
384 rectangle->width,
385 rectangle->height);
386
387 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
388 data, egl_window_surface_data_destroy);
389
390 return cairo_surface;
391}
392
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393#endif
394
395struct wl_buffer *
396display_get_buffer_for_surface(struct display *display,
397 cairo_surface_t *surface)
398{
399 struct surface_data *data;
400
401 data = cairo_surface_get_user_data (surface, &surface_data_key);
402
403 return data->buffer;
404}
405
406struct shm_surface_data {
407 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700408 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409};
410
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500411static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700412shm_pool_destroy(struct shm_pool *pool);
413
414static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415shm_surface_data_destroy(void *p)
416{
417 struct shm_surface_data *data = p;
418
419 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700420 if (data->pool)
421 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422}
423
Kristian Høgsberg16626282012-04-03 11:21:27 -0400424static struct wl_shm_pool *
425make_shm_pool(struct display *display, int size, void **data)
426{
427 char filename[] = "/tmp/wayland-shm-XXXXXX";
428 struct wl_shm_pool *pool;
429 int fd;
430
431 fd = mkstemp(filename);
432 if (fd < 0) {
433 fprintf(stderr, "open %s failed: %m\n", filename);
434 return NULL;
435 }
436 if (ftruncate(fd, size) < 0) {
437 fprintf(stderr, "ftruncate failed: %m\n");
438 close(fd);
439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
443 unlink(filename);
444
445 if (*data == MAP_FAILED) {
446 fprintf(stderr, "mmap failed: %m\n");
447 close(fd);
448 return NULL;
449 }
450
451 pool = wl_shm_create_pool(display->shm, fd, size);
452
453 close(fd);
454
455 return pool;
456}
457
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700458static struct shm_pool *
459shm_pool_create(struct display *display, size_t size)
460{
461 struct shm_pool *pool = malloc(sizeof *pool);
462
463 if (!pool)
464 return NULL;
465
466 pool->pool = make_shm_pool(display, size, &pool->data);
467 if (!pool->pool) {
468 free(pool);
469 return NULL;
470 }
471
472 pool->size = size;
473 pool->used = 0;
474
475 return pool;
476}
477
478static void *
479shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
480{
481 if (pool->used + size > pool->size)
482 return NULL;
483
484 *offset = pool->used;
485 pool->used += size;
486
487 return (char *) pool->data + *offset;
488}
489
490/* destroy the pool. this does not unmap the memory though */
491static void
492shm_pool_destroy(struct shm_pool *pool)
493{
494 munmap(pool->data, pool->size);
495 wl_shm_pool_destroy(pool->pool);
496 free(pool);
497}
498
499/* Start allocating from the beginning of the pool again */
500static void
501shm_pool_reset(struct shm_pool *pool)
502{
503 pool->used = 0;
504}
505
506static int
507data_length_for_shm_surface(struct rectangle *rect)
508{
509 int stride;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rect->width);
513 return stride * rect->height;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517display_create_shm_surface_from_pool(struct display *display,
518 struct rectangle *rectangle,
519 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520{
521 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400522 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400523 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400525 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526
527 data = malloc(sizeof *data);
528 if (data == NULL)
529 return NULL;
530
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
532 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700533 length = stride * rectangle->height;
534 data->pool = NULL;
535 map = shm_pool_allocate(pool, length, &offset);
536
537 if (!map) {
538 free(data);
539 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400542 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500554 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700556 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400557 rectangle->width,
558 rectangle->height,
559 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 return surface;
562}
563
564static cairo_surface_t *
565display_create_shm_surface(struct display *display,
566 struct rectangle *rectangle, uint32_t flags,
567 struct window *window)
568{
569 struct shm_surface_data *data;
570 struct shm_pool *pool;
571 cairo_surface_t *surface;
572
573 if (window && window->pool) {
574 shm_pool_reset(window->pool);
575 surface = display_create_shm_surface_from_pool(display,
576 rectangle,
577 flags,
578 window->pool);
579 if (surface)
580 return surface;
581 }
582
583 pool = shm_pool_create(display,
584 data_length_for_shm_surface(rectangle));
585 if (!pool)
586 return NULL;
587
588 surface =
589 display_create_shm_surface_from_pool(display, rectangle,
590 flags, pool);
591
592 if (!surface) {
593 shm_pool_destroy(pool);
594 return NULL;
595 }
596
597 /* make sure we destroy the pool when the surface is destroyed */
598 data = cairo_surface_get_user_data(surface, &surface_data_key);
599 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600
601 return surface;
602}
603
nobled7b87cb02011-02-01 18:51:47 +0000604static int
605check_size(struct rectangle *rect)
606{
607 if (rect->width && rect->height)
608 return 0;
609
610 fprintf(stderr, "tried to create surface of "
611 "width: %d, height: %d\n", rect->width, rect->height);
612 return -1;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615cairo_surface_t *
616display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400618 struct rectangle *rectangle,
619 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620{
nobled7b87cb02011-02-01 18:51:47 +0000621 if (check_size(rectangle) < 0)
622 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500623#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300624 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400625 return display_create_egl_window_surface(display,
626 surface,
627 flags,
628 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400630 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631}
632
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300633static const char *cursors[] = {
634 "bottom_left_corner",
635 "bottom_right_corner",
636 "bottom_side",
637 "grabbing",
638 "left_ptr",
639 "left_side",
640 "right_side",
641 "top_left_corner",
642 "top_right_corner",
643 "top_side",
644 "xterm",
645 "hand1",
646};
647
648static void
649create_cursors(struct display *display)
650{
651 unsigned int i;
652
653 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
654 display->cursors =
655 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
656
657 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
658 display->cursors[i] =
659 wl_cursor_theme_get_cursor(display->cursor_theme,
660 cursors[i]);
661}
662
663static void
664destroy_cursors(struct display *display)
665{
666 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800667 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668}
669
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300670struct wl_cursor_image *
671display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300673 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300674 wl_cursor_theme_get_cursor(display->cursor_theme,
675 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400676
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300677 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400678}
679
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400680static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200681window_get_resize_dx_dy(struct window *window, int *x, int *y)
682{
683 if (window->resize_edges & WINDOW_RESIZING_LEFT)
684 *x = window->server_allocation.width - window->allocation.width;
685 else
686 *x = 0;
687
688 if (window->resize_edges & WINDOW_RESIZING_TOP)
689 *y = window->server_allocation.height -
690 window->allocation.height;
691 else
692 *y = 0;
693
694 window->resize_edges = 0;
695}
696
697static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500698window_attach_surface(struct window *window)
699{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400700 struct display *display = window->display;
701 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000702#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100703 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000704#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500705 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100706
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400707 if (window->type == TYPE_NONE) {
708 window->type = TYPE_TOPLEVEL;
709 if (display->shell)
710 wl_shell_surface_set_toplevel(window->shell_surface);
711 }
712
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000714#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
716 data = cairo_surface_get_user_data(window->cairo_surface,
717 &surface_data_key);
718
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 cairo_gl_surface_swapbuffers(window->cairo_surface);
720 wl_egl_window_get_attached_size(data->window,
721 &window->server_allocation.width,
722 &window->server_allocation.height);
723 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 buffer =
727 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400728 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200730 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400732 wl_surface_damage(window->surface, 0, 0,
733 window->allocation.width,
734 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400736 cairo_surface_destroy(window->cairo_surface);
737 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100738 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000739 default:
740 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100741 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500742
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500743 if (window->input_region) {
744 wl_surface_set_input_region(window->surface,
745 window->input_region);
746 wl_region_destroy(window->input_region);
747 window->input_region = NULL;
748 }
749
750 if (window->opaque_region) {
751 wl_surface_set_opaque_region(window->surface,
752 window->opaque_region);
753 wl_region_destroy(window->opaque_region);
754 window->opaque_region = NULL;
755 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500756}
757
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500758void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400761 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100762 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500763}
764
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400765void
766window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400767{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500768 cairo_surface_reference(surface);
769
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400770 if (window->cairo_surface != NULL)
771 cairo_surface_destroy(window->cairo_surface);
772
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500773 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400774}
775
Benjamin Franzke22d54812011-07-16 19:50:32 +0000776#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100777static void
778window_resize_cairo_window_surface(struct window *window)
779{
780 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200781 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100782
783 data = cairo_surface_get_user_data(window->cairo_surface,
784 &surface_data_key);
785
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200786 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100787 wl_egl_window_resize(data->window,
788 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789 window->allocation.height,
790 x,y);
791
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 cairo_gl_surface_set_size(window->cairo_surface,
793 window->allocation.width,
794 window->allocation.height);
795}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000796#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100797
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400798struct display *
799window_get_display(struct window *window)
800{
801 return window->display;
802}
803
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400804void
805window_create_surface(struct window *window)
806{
807 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400808 uint32_t flags = 0;
809
810 if (!window->transparent)
811 flags = SURFACE_OPAQUE;
812
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400813 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
816 if (window->cairo_surface) {
817 window_resize_cairo_window_surface(window);
818 return;
819 }
820 surface = display_create_surface(window->display,
821 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400822 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824#endif
825 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400826 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400827 &window->allocation,
828 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800830 default:
831 surface = NULL;
832 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400834
835 window_set_surface(window, surface);
836 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837}
838
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200839static void frame_destroy(struct frame *frame);
840
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400841void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500842window_destroy(struct window *window)
843{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200844 struct display *display = window->display;
845 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100846 struct window_output *window_output;
847 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200848
849 if (window->redraw_scheduled)
850 wl_list_remove(&window->redraw_task.link);
851
852 wl_list_for_each(input, &display->input_list, link) {
853 if (input->pointer_focus == window)
854 input->pointer_focus = NULL;
855 if (input->keyboard_focus == window)
856 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500857 if (input->focus_widget &&
858 input->focus_widget->window == window)
859 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200860 }
861
Rob Bradford7507b572012-05-15 17:55:34 +0100862 wl_list_for_each_safe(window_output, window_output_tmp,
863 &window->window_output_list, link) {
864 free (window_output);
865 }
866
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500867 if (window->input_region)
868 wl_region_destroy(window->input_region);
869 if (window->opaque_region)
870 wl_region_destroy(window->opaque_region);
871
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200872 if (window->frame)
873 frame_destroy(window->frame);
874
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200875 if (window->shell_surface)
876 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500877 wl_surface_destroy(window->surface);
878 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200879
880 if (window->cairo_surface != NULL)
881 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200882
883 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500884 free(window);
885}
886
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500887static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500888widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400889{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500892 wl_list_for_each(child, &widget->child_list, link) {
893 target = widget_find_widget(child, x, y);
894 if (target)
895 return target;
896 }
897
898 if (widget->allocation.x <= x &&
899 x < widget->allocation.x + widget->allocation.width &&
900 widget->allocation.y <= y &&
901 y < widget->allocation.y + widget->allocation.height) {
902 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400903 }
904
905 return NULL;
906}
907
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500908static struct widget *
909widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400910{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500911 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 widget = malloc(sizeof *widget);
914 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500915 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500916 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500917 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500918 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500919 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300920 widget->tooltip = NULL;
921 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500922
923 return widget;
924}
925
926struct widget *
927window_add_widget(struct window *window, void *data)
928{
929 window->widget = widget_create(window, data);
930 wl_list_init(&window->widget->link);
931
932 return window->widget;
933}
934
935struct widget *
936widget_add_widget(struct widget *parent, void *data)
937{
938 struct widget *widget;
939
940 widget = widget_create(parent->window, data);
941 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500943 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944}
945
946void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500947widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200949 struct display *display = widget->window->display;
950 struct input *input;
951
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300952 if (widget->tooltip) {
953 free(widget->tooltip);
954 widget->tooltip = NULL;
955 }
956
Pekka Paalanene156fb62012-01-19 13:51:38 +0200957 wl_list_for_each(input, &display->input_list, link) {
958 if (input->focus_widget == widget)
959 input->focus_widget = NULL;
960 }
961
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500962 wl_list_remove(&widget->link);
963 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964}
965
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500969 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970}
971
972void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500973widget_set_size(struct widget *widget, int32_t width, int32_t height)
974{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975 widget->allocation.width = width;
976 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977}
978
979void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980widget_set_allocation(struct widget *widget,
981 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500983 widget->allocation.x = x;
984 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200985 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400986}
987
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500988void
989widget_set_transparent(struct widget *widget, int transparent)
990{
991 widget->opaque = !transparent;
992}
993
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998}
999
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001001widget_set_resize_handler(struct widget *widget,
1002 widget_resize_handler_t handler)
1003{
1004 widget->resize_handler = handler;
1005}
1006
1007void
1008widget_set_redraw_handler(struct widget *widget,
1009 widget_redraw_handler_t handler)
1010{
1011 widget->redraw_handler = handler;
1012}
1013
1014void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001015widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001016{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001017 widget->enter_handler = handler;
1018}
1019
1020void
1021widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1022{
1023 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001024}
1025
1026void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001027widget_set_motion_handler(struct widget *widget,
1028 widget_motion_handler_t handler)
1029{
1030 widget->motion_handler = handler;
1031}
1032
1033void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001034widget_set_button_handler(struct widget *widget,
1035 widget_button_handler_t handler)
1036{
1037 widget->button_handler = handler;
1038}
1039
1040void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001041widget_schedule_redraw(struct widget *widget)
1042{
1043 window_schedule_redraw(widget->window);
1044}
1045
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001046cairo_surface_t *
1047window_get_surface(struct window *window)
1048{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001049 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001050}
1051
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001052struct wl_surface *
1053window_get_wl_surface(struct window *window)
1054{
1055 return window->surface;
1056}
1057
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001058struct wl_shell_surface *
1059window_get_wl_shell_surface(struct window *window)
1060{
1061 return window->shell_surface;
1062}
1063
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001064static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001065tooltip_redraw_handler(struct widget *widget, void *data)
1066{
1067 cairo_t *cr;
1068 const int32_t r = 3;
1069 struct tooltip *tooltip = data;
1070 int32_t width, height;
1071 struct window *window = widget->window;
1072
1073 cr = cairo_create(window->cairo_surface);
1074 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1075 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1076 cairo_paint(cr);
1077
1078 width = window->allocation.width;
1079 height = window->allocation.height;
1080 rounded_rect(cr, 0, 0, width, height, r);
1081
1082 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1083 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1084 cairo_fill(cr);
1085
1086 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1087 cairo_move_to(cr, 10, 16);
1088 cairo_show_text(cr, tooltip->entry);
1089 cairo_destroy(cr);
1090}
1091
1092static cairo_text_extents_t
1093get_text_extents(struct tooltip *tooltip)
1094{
1095 struct window *window;
1096 cairo_t *cr;
1097 cairo_text_extents_t extents;
1098
1099 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1100 * created yet */
1101 window = tooltip->widget->window->parent;
1102 cr = cairo_create(window->cairo_surface);
1103 cairo_text_extents(cr, tooltip->entry, &extents);
1104 cairo_destroy(cr);
1105
1106 return extents;
1107}
1108
1109static int
1110window_create_tooltip(struct tooltip *tooltip)
1111{
1112 struct widget *parent = tooltip->parent;
1113 struct display *display = parent->window->display;
1114 struct window *window;
1115 const int offset_y = 27;
1116 const int margin = 3;
1117 cairo_text_extents_t extents;
1118
1119 if (tooltip->widget)
1120 return 0;
1121
1122 window = window_create_transient(display, parent->window, tooltip->x,
1123 tooltip->y + offset_y,
1124 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1125 if (!window)
1126 return -1;
1127
1128 tooltip->window = window;
1129 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1130
1131 extents = get_text_extents(tooltip);
1132 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1133 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1134
1135 return 0;
1136}
1137
1138void
1139widget_destroy_tooltip(struct widget *parent)
1140{
1141 struct tooltip *tooltip = parent->tooltip;
1142
1143 parent->tooltip_count = 0;
1144 if (!tooltip)
1145 return;
1146
1147 if (tooltip->widget) {
1148 widget_destroy(tooltip->widget);
1149 window_destroy(tooltip->window);
1150 tooltip->widget = NULL;
1151 tooltip->window = NULL;
1152 }
1153
1154 close(tooltip->tooltip_fd);
1155 free(tooltip->entry);
1156 free(tooltip);
1157 parent->tooltip = NULL;
1158}
1159
1160static void
1161tooltip_func(struct task *task, uint32_t events)
1162{
1163 struct tooltip *tooltip =
1164 container_of(task, struct tooltip, tooltip_task);
1165 uint64_t exp;
1166
1167 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1168 window_create_tooltip(tooltip);
1169}
1170
1171#define TOOLTIP_TIMEOUT 500
1172static int
1173tooltip_timer_reset(struct tooltip *tooltip)
1174{
1175 struct itimerspec its;
1176
1177 its.it_interval.tv_sec = 0;
1178 its.it_interval.tv_nsec = 0;
1179 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1180 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1181 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1182 fprintf(stderr, "could not set timerfd\n: %m");
1183 return -1;
1184 }
1185
1186 return 0;
1187}
1188
1189int
1190widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1191{
1192 struct tooltip *tooltip = parent->tooltip;
1193
1194 parent->tooltip_count++;
1195 if (tooltip) {
1196 tooltip->x = x;
1197 tooltip->y = y;
1198 tooltip_timer_reset(tooltip);
1199 return 0;
1200 }
1201
1202 /* the handler might be triggered too fast via input device motion, so
1203 * we need this check here to make sure tooltip is fully initialized */
1204 if (parent->tooltip_count > 1)
1205 return 0;
1206
1207 tooltip = malloc(sizeof *tooltip);
1208 if (!tooltip)
1209 return -1;
1210
1211 parent->tooltip = tooltip;
1212 tooltip->parent = parent;
1213 tooltip->widget = NULL;
1214 tooltip->window = NULL;
1215 tooltip->x = x;
1216 tooltip->y = y;
1217 tooltip->entry = strdup(entry);
1218 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1219 if (tooltip->tooltip_fd < 0) {
1220 fprintf(stderr, "could not create timerfd\n: %m");
1221 return -1;
1222 }
1223
1224 tooltip->tooltip_task.run = tooltip_func;
1225 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1226 EPOLLIN, &tooltip->tooltip_task);
1227 tooltip_timer_reset(tooltip);
1228
1229 return 0;
1230}
1231
1232static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233frame_resize_handler(struct widget *widget,
1234 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236 struct frame *frame = data;
1237 struct widget *child = frame->child;
1238 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001239 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001240 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001241 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001242 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001244 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001246 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001247 decoration_width = (t->width + t->margin) * 2;
1248 decoration_height = t->width +
1249 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001251 allocation.x = t->width + t->margin;
1252 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001253 allocation.width = width - decoration_width;
1254 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001255
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001256 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001257
1258 wl_list_for_each(button, &frame->buttons_list, link)
1259 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001260 } else {
1261 decoration_width = 0;
1262 decoration_height = 0;
1263
1264 allocation.x = 0;
1265 allocation.y = 0;
1266 allocation.width = width;
1267 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001268 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001269
1270 wl_list_for_each(button, &frame->buttons_list, link)
1271 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001272 }
1273
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 widget_set_allocation(child, allocation.x, allocation.y,
1275 allocation.width, allocation.height);
1276
1277 if (child->resize_handler)
1278 child->resize_handler(child,
1279 allocation.width,
1280 allocation.height,
1281 child->user_data);
1282
Scott Moreauf7e498c2012-05-14 11:39:29 -06001283 width = child->allocation.width + decoration_width;
1284 height = child->allocation.height + decoration_height;
1285
1286 widget->window->input_region =
1287 wl_compositor_create_region(display->compositor);
1288 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001289 t->margin, t->margin,
1290 width - 2 * t->margin,
1291 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001292
1293 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001294
1295 if (child->opaque) {
1296 widget->window->opaque_region =
1297 wl_compositor_create_region(display->compositor);
1298 wl_region_add(widget->window->opaque_region,
1299 opaque_margin, opaque_margin,
1300 widget->allocation.width - 2 * opaque_margin,
1301 widget->allocation.height - 2 * opaque_margin);
1302 }
Martin Minarik1998b152012-05-10 02:04:35 +02001303
1304 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001305 x_r = frame->widget->allocation.width - t->width - t->margin;
1306 x_l = t->width + t->margin;
1307 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001308 wl_list_for_each(button, &frame->buttons_list, link) {
1309 const int button_padding = 4;
1310 w = cairo_image_surface_get_width(button->icon);
1311 h = cairo_image_surface_get_height(button->icon);
1312
1313 if (button->decoration == FRAME_BUTTON_FANCY)
1314 w += 10;
1315
1316 if (button->align == FRAME_BUTTON_LEFT) {
1317 widget_set_allocation(button->widget,
1318 x_l, y , w + 1, h + 1);
1319 x_l += w;
1320 x_l += button_padding;
1321 } else {
1322 x_r -= w;
1323 widget_set_allocation(button->widget,
1324 x_r, y , w + 1, h + 1);
1325 x_r -= button_padding;
1326 }
1327 }
1328}
1329
1330static int
1331frame_button_enter_handler(struct widget *widget,
1332 struct input *input, float x, float y, void *data)
1333{
1334 struct frame_button *frame_button = data;
1335
1336 widget_schedule_redraw(frame_button->widget);
1337 frame_button->state = FRAME_BUTTON_OVER;
1338
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001339 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001340}
1341
1342static void
1343frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1344{
1345 struct frame_button *frame_button = data;
1346
1347 widget_schedule_redraw(frame_button->widget);
1348 frame_button->state = FRAME_BUTTON_DEFAULT;
1349}
1350
1351static void
1352frame_button_button_handler(struct widget *widget,
1353 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001354 uint32_t button,
1355 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001356{
1357 struct frame_button *frame_button = data;
1358 struct window *window = widget->window;
1359
1360 if (button != BTN_LEFT)
1361 return;
1362
1363 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001364 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001365 frame_button->state = FRAME_BUTTON_ACTIVE;
1366 widget_schedule_redraw(frame_button->widget);
1367
1368 if (frame_button->type == FRAME_BUTTON_ICON)
1369 window_show_frame_menu(window, input, time);
1370 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001371 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001372 frame_button->state = FRAME_BUTTON_DEFAULT;
1373 widget_schedule_redraw(frame_button->widget);
1374 break;
1375 }
1376
1377 switch (frame_button->type) {
1378 case FRAME_BUTTON_CLOSE:
1379 if (window->close_handler)
1380 window->close_handler(window->parent,
1381 window->user_data);
1382 else
1383 display_exit(window->display);
1384 break;
1385 case FRAME_BUTTON_MINIMIZE:
1386 fprintf(stderr,"Minimize stub\n");
1387 break;
1388 case FRAME_BUTTON_MAXIMIZE:
1389 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1390 break;
1391 default:
1392 /* Unknown operation */
1393 break;
1394 }
1395}
1396
1397static void
1398frame_button_redraw_handler(struct widget *widget, void *data)
1399{
1400 struct frame_button *frame_button = data;
1401 cairo_t *cr;
1402 int width, height, x, y;
1403 struct window *window = widget->window;
1404
1405 x = widget->allocation.x;
1406 y = widget->allocation.y;
1407 width = widget->allocation.width;
1408 height = widget->allocation.height;
1409
1410 if (!width)
1411 return;
1412 if (!height)
1413 return;
1414 if (widget->opaque)
1415 return;
1416
1417 cr = cairo_create(window->cairo_surface);
1418
1419 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1420 cairo_set_line_width(cr, 1);
1421
1422 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1423 cairo_rectangle (cr, x, y, 25, 16);
1424
1425 cairo_stroke_preserve(cr);
1426
1427 switch (frame_button->state) {
1428 case FRAME_BUTTON_DEFAULT:
1429 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1430 break;
1431 case FRAME_BUTTON_OVER:
1432 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1433 break;
1434 case FRAME_BUTTON_ACTIVE:
1435 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1436 break;
1437 }
1438
1439 cairo_fill (cr);
1440
1441 x += 4;
1442 }
1443
1444 cairo_set_source_surface(cr, frame_button->icon, x, y);
1445 cairo_paint(cr);
1446
1447 cairo_destroy(cr);
1448}
1449
1450static struct widget *
1451frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1452 enum frame_button_align align, enum frame_button_decoration style)
1453{
1454 struct frame_button *frame_button;
1455 const char *icon = data;
1456
1457 frame_button = malloc (sizeof *frame_button);
1458 memset(frame_button, 0, sizeof *frame_button);
1459
1460 frame_button->icon = cairo_image_surface_create_from_png(icon);
1461 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1462 frame_button->frame = frame;
1463 frame_button->type = type;
1464 frame_button->align = align;
1465 frame_button->decoration = style;
1466
1467 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1468
1469 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1470 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1471 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1472 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1473 return frame_button->widget;
1474}
1475
1476static void
1477frame_button_destroy(struct frame_button *frame_button)
1478{
1479 widget_destroy(frame_button->widget);
1480 wl_list_remove(&frame_button->link);
1481 cairo_surface_destroy(frame_button->icon);
1482 free(frame_button);
1483
1484 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485}
1486
1487static void
1488frame_redraw_handler(struct widget *widget, void *data)
1489{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001490 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001492 struct theme *t = window->display->theme;
1493 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001495 if (window->type == TYPE_FULLSCREEN)
1496 return;
1497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001498 cr = cairo_create(window->cairo_surface);
1499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001501 flags |= THEME_FRAME_ACTIVE;
1502 theme_render_frame(t, cr, widget->allocation.width,
1503 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001504
1505 cairo_destroy(cr);
1506}
1507
1508static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001509frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001510{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001511 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001512 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001514 location = theme_get_location(t, input->sx, input->sy,
1515 frame->widget->allocation.width,
1516 frame->widget->allocation.height);
1517
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001518 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001520 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001533 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 case THEME_LOCATION_EXTERIOR:
1536 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001537 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001538 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001539 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001540}
1541
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001542static void
1543frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001544{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001545 switch (index) {
1546 case 0: /* close */
1547 if (window->close_handler)
1548 window->close_handler(window->parent,
1549 window->user_data);
1550 else
1551 display_exit(window->display);
1552 break;
1553 case 1: /* fullscreen */
1554 /* we don't have a way to get out of fullscreen for now */
1555 window_set_fullscreen(window, 1);
1556 break;
1557 case 2: /* rotate */
1558 case 3: /* scale */
1559 break;
1560 }
1561}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001562
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001563void
1564window_show_frame_menu(struct window *window,
1565 struct input *input, uint32_t time)
1566{
1567 int32_t x, y;
1568
1569 static const char *entries[] = {
1570 "Close", "Fullscreen", "Rotate", "Scale"
1571 };
1572
1573 input_get_position(input, &x, &y);
1574 window_show_menu(window->display, input, time, window,
1575 x - 10, y - 10, frame_menu_func, entries, 4);
1576}
1577
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001578static int
1579frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001580 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001581{
1582 return frame_get_pointer_image_for_location(data, input);
1583}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001584
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001585static int
1586frame_motion_handler(struct widget *widget,
1587 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001588 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}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594frame_button_handler(struct widget *widget,
1595 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001596 uint32_t button, enum wl_pointer_button_state state,
1597 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001598
1599{
1600 struct frame *frame = data;
1601 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001602 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001604
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001605 location = theme_get_location(display->theme, input->sx, input->sy,
1606 frame->widget->allocation.width,
1607 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608
Daniel Stone4dbadb12012-05-30 16:31:51 +01001609 if (window->display->shell && button == BTN_LEFT &&
1610 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001612 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 if (!window->shell_surface)
1614 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001615 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001618 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001619 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001620 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001621 case THEME_LOCATION_RESIZING_TOP:
1622 case THEME_LOCATION_RESIZING_BOTTOM:
1623 case THEME_LOCATION_RESIZING_LEFT:
1624 case THEME_LOCATION_RESIZING_RIGHT:
1625 case THEME_LOCATION_RESIZING_TOP_LEFT:
1626 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1627 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1628 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001629 if (!window->shell_surface)
1630 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001631 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001632
1633 if (!display->dpy) {
1634 /* If we're using shm, allocate a big
1635 pool to create buffers out of while
1636 we resize. We should probably base
1637 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001638 window->pool =
1639 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001640 }
1641
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001642 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001643 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001644 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001645 break;
1646 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001647 } else if (button == BTN_RIGHT &&
1648 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001649 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001650 }
1651}
1652
1653struct widget *
1654frame_create(struct window *window, void *data)
1655{
1656 struct frame *frame;
1657
1658 frame = malloc(sizeof *frame);
1659 memset(frame, 0, sizeof *frame);
1660
1661 frame->widget = window_add_widget(window, frame);
1662 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001663
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001664 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1665 widget_set_resize_handler(frame->widget, frame_resize_handler);
1666 widget_set_enter_handler(frame->widget, frame_enter_handler);
1667 widget_set_motion_handler(frame->widget, frame_motion_handler);
1668 widget_set_button_handler(frame->widget, frame_button_handler);
1669
Martin Minarik1998b152012-05-10 02:04:35 +02001670 /* Create empty list for frame buttons */
1671 wl_list_init(&frame->buttons_list);
1672
1673 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1674 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1677 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1680 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
1682 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1683 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001685 window->frame = frame;
1686
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001687 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001688}
1689
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001690static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001691frame_destroy(struct frame *frame)
1692{
Martin Minarik1998b152012-05-10 02:04:35 +02001693 struct frame_button *button, *tmp;
1694
1695 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1696 frame_button_destroy(button);
1697
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001698 /* frame->child must be destroyed by the application */
1699 widget_destroy(frame->widget);
1700 free(frame);
1701}
1702
1703static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001704input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001705 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001707 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001708 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001710 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711 return;
1712
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001713 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001714 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001715 widget = old;
1716 if (input->grab)
1717 widget = input->grab;
1718 if (widget->leave_handler)
1719 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001720 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001721 }
1722
1723 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001724 widget = focus;
1725 if (input->grab)
1726 widget = input->grab;
1727 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001729 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001730 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001731
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001732 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001733 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734}
1735
1736static void
Daniel Stone37816df2012-05-16 18:45:18 +01001737pointer_handle_motion(void *data, struct wl_pointer *pointer,
1738 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001739{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001740 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001741 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001742 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001743 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001744 float sx = wl_fixed_to_double(sx_w);
1745 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001746
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001747 input->sx = sx;
1748 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001749
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001750 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001751 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001752 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001753 }
1754
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001755 if (input->grab)
1756 widget = input->grab;
1757 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001758 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001759 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001760 cursor = widget->motion_handler(input->focus_widget,
1761 input, time, sx, sy,
1762 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001763
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001764 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001765}
1766
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001767void
1768input_grab(struct input *input, struct widget *widget, uint32_t button)
1769{
1770 input->grab = widget;
1771 input->grab_button = button;
1772}
1773
1774void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001775input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001776{
1777 struct widget *widget;
1778
1779 input->grab = NULL;
1780 if (input->pointer_focus) {
1781 widget = widget_find_widget(input->pointer_focus->widget,
1782 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001783 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001784 }
1785}
1786
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001787static void
Daniel Stone37816df2012-05-16 18:45:18 +01001788pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001789 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001790{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001791 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001792 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001793 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001794
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001795 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001796 if (input->focus_widget && input->grab == NULL &&
1797 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001798 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001799
Neil Roberts6b28aad2012-01-23 19:11:18 +00001800 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001801 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001802 (*widget->button_handler)(widget,
1803 input, time,
1804 button, state,
1805 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001806
Daniel Stone4dbadb12012-05-30 16:31:51 +01001807 if (input->grab && input->grab_button == button &&
1808 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001809 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001810}
1811
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001812static void
Daniel Stone37816df2012-05-16 18:45:18 +01001813pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001814 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001815{
1816}
1817
1818static void
Daniel Stone37816df2012-05-16 18:45:18 +01001819keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001820 uint32_t serial, uint32_t time, uint32_t key,
1821 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001822{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001823 struct input *input = data;
1824 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001825 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001826 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001827 const xkb_keysym_t *syms;
1828 xkb_keysym_t sym;
1829 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001830
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001831 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001832 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001833 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001834 return;
1835
Daniel Stone97f68542012-05-30 16:32:01 +01001836 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001837
Daniel Stone97f68542012-05-30 16:32:01 +01001838 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001839 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001840 XKB_STATE_LATCHED);
1841 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001842 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001843 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001844 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001845 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001846 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001848
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001849 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001851 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001852 window_set_maximized(window,
1853 window->type != TYPE_MAXIMIZED);
1854 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001855 if (num_syms == 1)
1856 sym = syms[0];
1857 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001858 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001859
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001860 (*window->key_handler)(window, input, time, key,
1861 sym, state, window->user_data);
1862 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001863}
1864
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001865static void
Daniel Stone351eb612012-05-31 15:27:47 -04001866keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1867 uint32_t serial, uint32_t mods_depressed,
1868 uint32_t mods_latched, uint32_t mods_locked,
1869 uint32_t group)
1870{
1871 struct input *input = data;
1872
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001873 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1874 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001875}
1876
1877static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001878input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001879{
1880 struct window *window = input->pointer_focus;
1881
1882 if (!window)
1883 return;
1884
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001885 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886
Pekka Paalanene1207c72011-12-16 12:02:09 +02001887 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001888 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001889}
1890
1891static void
Daniel Stone37816df2012-05-16 18:45:18 +01001892pointer_handle_enter(void *data, struct wl_pointer *pointer,
1893 uint32_t serial, struct wl_surface *surface,
1894 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001895{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001896 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001897 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001898 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001899 float sx = wl_fixed_to_double(sx_w);
1900 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001901
Martin Minarik1998b152012-05-10 02:04:35 +02001902 if (!surface) {
1903 /* enter event for a window we've just destroyed */
1904 return;
1905 }
1906
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001907 input->display->serial = serial;
1908 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001910 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001911
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001912 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001913 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001914 window->pool = NULL;
1915 /* Schedule a redraw to free the pool */
1916 window_schedule_redraw(window);
1917 }
1918
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 input->sx = sx;
1920 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001921
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001922 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001923 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001924}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001925
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001926static void
Daniel Stone37816df2012-05-16 18:45:18 +01001927pointer_handle_leave(void *data, struct wl_pointer *pointer,
1928 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001929{
1930 struct input *input = data;
1931
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001932 input->display->serial = serial;
1933 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001934}
1935
1936static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001937input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001938{
1939 struct window *window = input->keyboard_focus;
1940
1941 if (!window)
1942 return;
1943
1944 window->keyboard_device = NULL;
1945 if (window->keyboard_focus_handler)
1946 (*window->keyboard_focus_handler)(window, NULL,
1947 window->user_data);
1948
1949 input->keyboard_focus = NULL;
1950}
1951
1952static void
Daniel Stone37816df2012-05-16 18:45:18 +01001953keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1954 uint32_t serial, struct wl_surface *surface,
1955 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001956{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001957 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001958 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001959
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001960 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001961 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001962
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001963 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001964 window->keyboard_device = input;
1965 if (window->keyboard_focus_handler)
1966 (*window->keyboard_focus_handler)(window,
1967 window->keyboard_device,
1968 window->user_data);
1969}
1970
1971static void
Daniel Stone37816df2012-05-16 18:45:18 +01001972keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1973 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001974{
1975 struct input *input = data;
1976
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001977 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001978 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001979}
1980
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001981static void
1982keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1983 uint32_t format, int fd, uint32_t size)
1984{
1985 struct input *input = data;
1986 char *map_str;
1987
1988 if (!data) {
1989 close(fd);
1990 return;
1991 }
1992
1993 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1994 close(fd);
1995 return;
1996 }
1997
1998 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1999 if (map_str == MAP_FAILED) {
2000 close(fd);
2001 return;
2002 }
2003
2004 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2005 map_str,
2006 XKB_KEYMAP_FORMAT_TEXT_V1,
2007 0);
2008 munmap(map_str, size);
2009 close(fd);
2010
2011 if (!input->xkb.keymap) {
2012 fprintf(stderr, "failed to compile keymap\n");
2013 return;
2014 }
2015
2016 input->xkb.state = xkb_state_new(input->xkb.keymap);
2017 if (!input->xkb.state) {
2018 fprintf(stderr, "failed to create XKB state\n");
2019 xkb_map_unref(input->xkb.keymap);
2020 input->xkb.keymap = NULL;
2021 return;
2022 }
2023
2024 input->xkb.control_mask =
2025 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2026 input->xkb.alt_mask =
2027 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2028 input->xkb.shift_mask =
2029 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2030}
2031
Daniel Stone37816df2012-05-16 18:45:18 +01002032static const struct wl_pointer_listener pointer_listener = {
2033 pointer_handle_enter,
2034 pointer_handle_leave,
2035 pointer_handle_motion,
2036 pointer_handle_button,
2037 pointer_handle_axis,
2038};
2039
2040static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002041 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002042 keyboard_handle_enter,
2043 keyboard_handle_leave,
2044 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002045 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002046};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002047
2048static void
Daniel Stone37816df2012-05-16 18:45:18 +01002049seat_handle_capabilities(void *data, struct wl_seat *seat,
2050 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002051{
Daniel Stone37816df2012-05-16 18:45:18 +01002052 struct input *input = data;
2053
2054 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2055 input->pointer = wl_seat_get_pointer(seat);
2056 wl_pointer_set_user_data(input->pointer, input);
2057 wl_pointer_add_listener(input->pointer, &pointer_listener,
2058 input);
2059 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2060 wl_pointer_destroy(input->pointer);
2061 input->pointer = NULL;
2062 }
2063
2064 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2065 input->keyboard = wl_seat_get_keyboard(seat);
2066 wl_keyboard_set_user_data(input->keyboard, input);
2067 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2068 input);
2069 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2070 wl_keyboard_destroy(input->keyboard);
2071 input->keyboard = NULL;
2072 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002073}
2074
Daniel Stone37816df2012-05-16 18:45:18 +01002075static const struct wl_seat_listener seat_listener = {
2076 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002077};
2078
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002079void
2080input_get_position(struct input *input, int32_t *x, int32_t *y)
2081{
2082 *x = input->sx;
2083 *y = input->sy;
2084}
2085
Daniel Stone37816df2012-05-16 18:45:18 +01002086struct wl_seat *
2087input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002088{
Daniel Stone37816df2012-05-16 18:45:18 +01002089 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002090}
2091
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002092uint32_t
2093input_get_modifiers(struct input *input)
2094{
2095 return input->modifiers;
2096}
2097
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002098struct widget *
2099input_get_focus_widget(struct input *input)
2100{
2101 return input->focus_widget;
2102}
2103
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002104struct data_offer {
2105 struct wl_data_offer *offer;
2106 struct input *input;
2107 struct wl_array types;
2108 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002109
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002110 struct task io_task;
2111 int fd;
2112 data_func_t func;
2113 int32_t x, y;
2114 void *user_data;
2115};
2116
2117static void
2118data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2119{
2120 struct data_offer *offer = data;
2121 char **p;
2122
2123 p = wl_array_add(&offer->types, sizeof *p);
2124 *p = strdup(type);
2125}
2126
2127static const struct wl_data_offer_listener data_offer_listener = {
2128 data_offer_offer,
2129};
2130
2131static void
2132data_offer_destroy(struct data_offer *offer)
2133{
2134 char **p;
2135
2136 offer->refcount--;
2137 if (offer->refcount == 0) {
2138 wl_data_offer_destroy(offer->offer);
2139 for (p = offer->types.data; *p; p++)
2140 free(*p);
2141 wl_array_release(&offer->types);
2142 free(offer);
2143 }
2144}
2145
2146static void
2147data_device_data_offer(void *data,
2148 struct wl_data_device *data_device, uint32_t id)
2149{
2150 struct data_offer *offer;
2151
2152 offer = malloc(sizeof *offer);
2153
2154 wl_array_init(&offer->types);
2155 offer->refcount = 1;
2156 offer->input = data;
2157
2158 /* FIXME: Generate typesafe wrappers for this */
2159 offer->offer = (struct wl_data_offer *)
2160 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2161 id, &wl_data_offer_interface);
2162
2163 wl_data_offer_add_listener(offer->offer,
2164 &data_offer_listener, offer);
2165}
2166
2167static void
2168data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002169 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002170 wl_fixed_t x_w, wl_fixed_t y_w,
2171 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002172{
2173 struct input *input = data;
2174 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002175 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002176 float x = wl_fixed_to_double(x_w);
2177 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002178 char **p;
2179
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002180 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181 window = wl_surface_get_user_data(surface);
2182 input->pointer_focus = window;
2183
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002184 if (offer) {
2185 input->drag_offer = wl_data_offer_get_user_data(offer);
2186
2187 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2188 *p = NULL;
2189
2190 types_data = input->drag_offer->types.data;
2191 } else {
2192 input->drag_offer = NULL;
2193 types_data = NULL;
2194 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002195
2196 window = input->pointer_focus;
2197 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002198 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002199 window->user_data);
2200}
2201
2202static void
2203data_device_leave(void *data, struct wl_data_device *data_device)
2204{
2205 struct input *input = data;
2206
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002207 if (input->drag_offer) {
2208 data_offer_destroy(input->drag_offer);
2209 input->drag_offer = NULL;
2210 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002211}
2212
2213static void
2214data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002215 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002216{
2217 struct input *input = data;
2218 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002219 float x = wl_fixed_to_double(x_w);
2220 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002221 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002222
2223 input->sx = x;
2224 input->sy = y;
2225
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002226 if (input->drag_offer)
2227 types_data = input->drag_offer->types.data;
2228 else
2229 types_data = NULL;
2230
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002231 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002232 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002233 window->user_data);
2234}
2235
2236static void
2237data_device_drop(void *data, struct wl_data_device *data_device)
2238{
2239 struct input *input = data;
2240 struct window *window = input->pointer_focus;
2241
2242 if (window->drop_handler)
2243 window->drop_handler(window, input,
2244 input->sx, input->sy, window->user_data);
2245}
2246
2247static void
2248data_device_selection(void *data,
2249 struct wl_data_device *wl_data_device,
2250 struct wl_data_offer *offer)
2251{
2252 struct input *input = data;
2253 char **p;
2254
2255 if (input->selection_offer)
2256 data_offer_destroy(input->selection_offer);
2257
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002258 if (offer) {
2259 input->selection_offer = wl_data_offer_get_user_data(offer);
2260 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2261 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002262 } else {
2263 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002264 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002265}
2266
2267static const struct wl_data_device_listener data_device_listener = {
2268 data_device_data_offer,
2269 data_device_enter,
2270 data_device_leave,
2271 data_device_motion,
2272 data_device_drop,
2273 data_device_selection
2274};
2275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002276void
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002277input_set_pointer_image(struct input *input, int pointer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002279 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002280 struct wl_cursor *cursor;
2281 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002283 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002284 return;
2285
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002286 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002287 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002288 return;
2289
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002290 image = cursor->images[0];
2291 buffer = wl_cursor_image_get_buffer(image);
2292 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002293 return;
2294
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002295 input->current_cursor = pointer;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002296 wl_pointer_attach(input->pointer, input->pointer_enter_serial,
2297 buffer, image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002298}
2299
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300struct wl_data_device *
2301input_get_data_device(struct input *input)
2302{
2303 return input->data_device;
2304}
2305
2306void
2307input_set_selection(struct input *input,
2308 struct wl_data_source *source, uint32_t time)
2309{
2310 wl_data_device_set_selection(input->data_device, source, time);
2311}
2312
2313void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002314input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002315{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002316 wl_data_offer_accept(input->drag_offer->offer,
2317 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002318}
2319
2320static void
2321offer_io_func(struct task *task, uint32_t events)
2322{
2323 struct data_offer *offer =
2324 container_of(task, struct data_offer, io_task);
2325 unsigned int len;
2326 char buffer[4096];
2327
2328 len = read(offer->fd, buffer, sizeof buffer);
2329 offer->func(buffer, len,
2330 offer->x, offer->y, offer->user_data);
2331
2332 if (len == 0) {
2333 close(offer->fd);
2334 data_offer_destroy(offer);
2335 }
2336}
2337
2338static void
2339data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2340 data_func_t func, void *user_data)
2341{
2342 int p[2];
2343
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002344 if (pipe2(p, O_CLOEXEC) == -1)
2345 return;
2346
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002347 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2348 close(p[1]);
2349
2350 offer->io_task.run = offer_io_func;
2351 offer->fd = p[0];
2352 offer->func = func;
2353 offer->refcount++;
2354 offer->user_data = user_data;
2355
2356 display_watch_fd(offer->input->display,
2357 offer->fd, EPOLLIN, &offer->io_task);
2358}
2359
2360void
2361input_receive_drag_data(struct input *input, const char *mime_type,
2362 data_func_t func, void *data)
2363{
2364 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2365 input->drag_offer->x = input->sx;
2366 input->drag_offer->y = input->sy;
2367}
2368
2369int
2370input_receive_selection_data(struct input *input, const char *mime_type,
2371 data_func_t func, void *data)
2372{
2373 char **p;
2374
2375 if (input->selection_offer == NULL)
2376 return -1;
2377
2378 for (p = input->selection_offer->types.data; *p; p++)
2379 if (strcmp(mime_type, *p) == 0)
2380 break;
2381
2382 if (*p == NULL)
2383 return -1;
2384
2385 data_offer_receive_data(input->selection_offer,
2386 mime_type, func, data);
2387 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002388}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002389
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002390int
2391input_receive_selection_data_to_fd(struct input *input,
2392 const char *mime_type, int fd)
2393{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002394 if (input->selection_offer)
2395 wl_data_offer_receive(input->selection_offer->offer,
2396 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002397
2398 return 0;
2399}
2400
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002401void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002402window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002403{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002404 if (!window->shell_surface)
2405 return;
2406
Daniel Stone37816df2012-05-16 18:45:18 +01002407 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002408}
2409
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002410static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002411idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002412{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002413 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002414
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002415 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002416 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002417 widget_set_allocation(widget,
2418 window->pending_allocation.x,
2419 window->pending_allocation.y,
2420 window->pending_allocation.width,
2421 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002422
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002423 if (window->input_region) {
2424 wl_region_destroy(window->input_region);
2425 window->input_region = NULL;
2426 }
2427
2428 if (window->opaque_region) {
2429 wl_region_destroy(window->opaque_region);
2430 window->opaque_region = NULL;
2431 }
2432
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002433 if (widget->resize_handler)
2434 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002435 widget->allocation.width,
2436 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002437 widget->user_data);
2438
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002439 if (window->allocation.width != widget->allocation.width ||
2440 window->allocation.height != widget->allocation.height) {
2441 window->allocation = widget->allocation;
2442 window_schedule_redraw(window);
2443 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002444}
2445
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002446void
2447window_schedule_resize(struct window *window, int width, int height)
2448{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002449 window->pending_allocation.x = 0;
2450 window->pending_allocation.y = 0;
2451 window->pending_allocation.width = width;
2452 window->pending_allocation.height = height;
2453
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002454 window->resize_needed = 1;
2455 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002456}
2457
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002458void
2459widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2460{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002462}
2463
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002464static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002465handle_ping(void *data, struct wl_shell_surface *shell_surface,
2466 uint32_t serial)
2467{
2468 wl_shell_surface_pong(shell_surface, serial);
2469}
2470
2471static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002472handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002473 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002474{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002475 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002476
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002477 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002478 return;
2479
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002480 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002481 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002482}
2483
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002484static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002485menu_destroy(struct menu *menu)
2486{
2487 widget_destroy(menu->widget);
2488 window_destroy(menu->window);
2489 free(menu);
2490}
2491
2492static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002493handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2494{
2495 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 struct menu *menu = window->widget->user_data;
2497
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002498 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002499 * device. Or just use wl_callback. And this really needs to
2500 * be a window vfunc that the menu can set. And we need the
2501 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002502
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002503 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002504 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002505 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002506}
2507
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002508static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002509 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002510 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002511 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002512};
2513
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002514void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002515window_get_allocation(struct window *window,
2516 struct rectangle *allocation)
2517{
2518 *allocation = window->allocation;
2519}
2520
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002521static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002522widget_redraw(struct widget *widget)
2523{
2524 struct widget *child;
2525
2526 if (widget->redraw_handler)
2527 widget->redraw_handler(widget, widget->user_data);
2528 wl_list_for_each(child, &widget->child_list, link)
2529 widget_redraw(child);
2530}
2531
2532static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002533frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2534{
2535 struct window *window = data;
2536
2537 wl_callback_destroy(callback);
2538 window->redraw_scheduled = 0;
2539 if (window->redraw_needed)
2540 window_schedule_redraw(window);
2541}
2542
2543static const struct wl_callback_listener listener = {
2544 frame_callback
2545};
2546
2547static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002548idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002549{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002550 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002551 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002552
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002553 if (window->resize_needed)
2554 idle_resize(window);
2555
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002556 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002557 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002558 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002559 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002560 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002561
2562 callback = wl_surface_frame(window->surface);
2563 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002564}
2565
2566void
2567window_schedule_redraw(struct window *window)
2568{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002569 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002570 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002571 window->redraw_task.run = idle_redraw;
2572 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002573 window->redraw_scheduled = 1;
2574 }
2575}
2576
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002577void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002578window_set_custom(struct window *window)
2579{
2580 window->type = TYPE_CUSTOM;
2581}
2582
2583void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002584window_set_fullscreen(struct window *window, int fullscreen)
2585{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002586 if (!window->display->shell)
2587 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002588
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002589 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002590 return;
2591
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002592 if (fullscreen) {
2593 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002594 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002595 wl_shell_surface_set_fullscreen(window->shell_surface,
2596 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2597 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002598 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002599 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002600 wl_shell_surface_set_toplevel(window->shell_surface);
2601 window_schedule_resize(window,
2602 window->saved_allocation.width,
2603 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002604 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002605}
2606
2607void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002608window_set_maximized(struct window *window, int maximized)
2609{
2610 if (!window->display->shell)
2611 return;
2612
2613 if ((window->type == TYPE_MAXIMIZED) == maximized)
2614 return;
2615
2616 if (window->type == TYPE_TOPLEVEL) {
2617 window->saved_allocation = window->allocation;
2618 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2619 window->type = TYPE_MAXIMIZED;
2620 } else {
2621 wl_shell_surface_set_toplevel(window->shell_surface);
2622 window->type = TYPE_TOPLEVEL;
2623 window_schedule_resize(window,
2624 window->saved_allocation.width,
2625 window->saved_allocation.height);
2626 }
2627}
2628
2629void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002630window_set_user_data(struct window *window, void *data)
2631{
2632 window->user_data = data;
2633}
2634
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002635void *
2636window_get_user_data(struct window *window)
2637{
2638 return window->user_data;
2639}
2640
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002641void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002642window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002643 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002644{
2645 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002646}
2647
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002648void
2649window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002650 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002651{
2652 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002653}
2654
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002655void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002656window_set_data_handler(struct window *window, window_data_handler_t handler)
2657{
2658 window->data_handler = handler;
2659}
2660
2661void
2662window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2663{
2664 window->drop_handler = handler;
2665}
2666
2667void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002668window_set_close_handler(struct window *window,
2669 window_close_handler_t handler)
2670{
2671 window->close_handler = handler;
2672}
2673
2674void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002675window_set_title(struct window *window, const char *title)
2676{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002677 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002678 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002679 if (window->shell_surface)
2680 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002681}
2682
2683const char *
2684window_get_title(struct window *window)
2685{
2686 return window->title;
2687}
2688
2689void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002690window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2691{
2692 struct text_cursor_position *text_cursor_position =
2693 window->display->text_cursor_position;
2694
Scott Moreau9295ce02012-06-01 12:46:10 -06002695 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002696 return;
2697
2698 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002699 window->surface,
2700 wl_fixed_from_int(x),
2701 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002702}
2703
2704void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002705window_damage(struct window *window, int32_t x, int32_t y,
2706 int32_t width, int32_t height)
2707{
2708 wl_surface_damage(window->surface, x, y, width, height);
2709}
2710
Casey Dahlin9074db52012-04-19 22:50:09 -04002711static void
2712surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002713 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002714{
Rob Bradford7507b572012-05-15 17:55:34 +01002715 struct window *window = data;
2716 struct output *output;
2717 struct output *output_found = NULL;
2718 struct window_output *window_output;
2719
2720 wl_list_for_each(output, &window->display->output_list, link) {
2721 if (output->output == wl_output) {
2722 output_found = output;
2723 break;
2724 }
2725 }
2726
2727 if (!output_found)
2728 return;
2729
2730 window_output = malloc (sizeof *window_output);
2731 window_output->output = output_found;
2732
2733 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002734}
2735
2736static void
2737surface_leave(void *data,
2738 struct wl_surface *wl_surface, struct wl_output *output)
2739{
Rob Bradford7507b572012-05-15 17:55:34 +01002740 struct window *window = data;
2741 struct window_output *window_output;
2742 struct window_output *window_output_found = NULL;
2743
2744 wl_list_for_each(window_output, &window->window_output_list, link) {
2745 if (window_output->output->output == output) {
2746 window_output_found = window_output;
2747 break;
2748 }
2749 }
2750
2751 if (window_output_found) {
2752 wl_list_remove(&window_output_found->link);
2753 free(window_output_found);
2754 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002755}
2756
2757static const struct wl_surface_listener surface_listener = {
2758 surface_enter,
2759 surface_leave
2760};
2761
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002762static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002763window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002764{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002765 struct window *window;
2766
2767 window = malloc(sizeof *window);
2768 if (window == NULL)
2769 return NULL;
2770
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002771 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002772 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002773 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002774 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002775 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002776 if (display->shell) {
2777 window->shell_surface =
2778 wl_shell_get_shell_surface(display->shell,
2779 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002780 if (window->title)
2781 wl_shell_surface_set_title(window->shell_surface,
2782 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002783 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002784 window->allocation.x = 0;
2785 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002786 window->allocation.width = 0;
2787 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002788 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002789 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002790 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002791 window->input_region = NULL;
2792 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002793
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002794 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002795#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002796 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002797#else
2798 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2799#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002800 else
2801 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002802
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002803 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002804 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002805 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002806
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002807 if (window->shell_surface) {
2808 wl_shell_surface_set_user_data(window->shell_surface, window);
2809 wl_shell_surface_add_listener(window->shell_surface,
2810 &shell_surface_listener, window);
2811 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002812
Rob Bradford7507b572012-05-15 17:55:34 +01002813 wl_list_init (&window->window_output_list);
2814
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002815 return window;
2816}
2817
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002818struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002819window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002820{
2821 struct window *window;
2822
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002823 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002824 if (!window)
2825 return NULL;
2826
2827 return window;
2828}
2829
2830struct window *
2831window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002832 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002833{
2834 struct window *window;
2835
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002836 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002837 if (!window)
2838 return NULL;
2839
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002840 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002841 window->x = x;
2842 window->y = y;
2843
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002844 if (display->shell)
2845 wl_shell_surface_set_transient(window->shell_surface,
2846 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002847 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002848
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002849 return window;
2850}
2851
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002852static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002853menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854{
2855 int next;
2856
2857 next = (sy - 8) / 20;
2858 if (menu->current != next) {
2859 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002860 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002861 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002862}
2863
2864static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002865menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002866 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002867 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002868{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002869 struct menu *menu = data;
2870
2871 if (widget == menu->widget)
2872 menu_set_item(data, y);
2873
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002874 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002875}
2876
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002877static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002878menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002879 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002880{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002881 struct menu *menu = data;
2882
2883 if (widget == menu->widget)
2884 menu_set_item(data, y);
2885
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002886 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002887}
2888
2889static void
2890menu_leave_handler(struct widget *widget, struct input *input, void *data)
2891{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002892 struct menu *menu = data;
2893
2894 if (widget == menu->widget)
2895 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002896}
2897
2898static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002899menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002900 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002901 uint32_t button, enum wl_pointer_button_state state,
2902 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002903
2904{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002905 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002906
Daniel Stone4dbadb12012-05-30 16:31:51 +01002907 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2908 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002909 /* Either relase after press-drag-release or
2910 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002911 menu->func(menu->window->parent,
2912 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002913 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002914 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002915 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002916}
2917
2918static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002919menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002920{
2921 cairo_t *cr;
2922 const int32_t r = 3, margin = 3;
2923 struct menu *menu = data;
2924 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002925 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002926
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002927 cr = cairo_create(window->cairo_surface);
2928 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2929 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2930 cairo_paint(cr);
2931
2932 width = window->allocation.width;
2933 height = window->allocation.height;
2934 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002935
2936 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002937 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2938 cairo_fill(cr);
2939
2940 for (i = 0; i < menu->count; i++) {
2941 if (i == menu->current) {
2942 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2943 cairo_rectangle(cr, margin, i * 20 + margin,
2944 width - 2 * margin, 20);
2945 cairo_fill(cr);
2946 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2947 cairo_move_to(cr, 10, i * 20 + 16);
2948 cairo_show_text(cr, menu->entries[i]);
2949 } else {
2950 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2951 cairo_move_to(cr, 10, i * 20 + 16);
2952 cairo_show_text(cr, menu->entries[i]);
2953 }
2954 }
2955
2956 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002957}
2958
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002959void
2960window_show_menu(struct display *display,
2961 struct input *input, uint32_t time, struct window *parent,
2962 int32_t x, int32_t y,
2963 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002964{
2965 struct window *window;
2966 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002967 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002968
2969 menu = malloc(sizeof *menu);
2970 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002971 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002972
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002973 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002974 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002975 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002976
2977 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002978 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002979 menu->entries = entries;
2980 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002981 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002982 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002983 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002984 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002985 window->type = TYPE_MENU;
2986 window->x = x;
2987 window->y = y;
2988
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002989 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002990 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002991 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002992 window->parent->shell_surface,
2993 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002994
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002995 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002996 widget_set_enter_handler(menu->widget, menu_enter_handler);
2997 widget_set_leave_handler(menu->widget, menu_leave_handler);
2998 widget_set_motion_handler(menu->widget, menu_motion_handler);
2999 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003000
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003001 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003002 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003003}
3004
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003005void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003006window_set_buffer_type(struct window *window, enum window_buffer_type type)
3007{
3008 window->buffer_type = type;
3009}
3010
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003011
3012static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003013display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003014 struct wl_output *wl_output,
3015 int x, int y,
3016 int physical_width,
3017 int physical_height,
3018 int subpixel,
3019 const char *make,
3020 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003021{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003022 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003023
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003024 output->allocation.x = x;
3025 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003026}
3027
3028static void
3029display_handle_mode(void *data,
3030 struct wl_output *wl_output,
3031 uint32_t flags,
3032 int width,
3033 int height,
3034 int refresh)
3035{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003036 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003037 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003038
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003039 if (flags & WL_OUTPUT_MODE_CURRENT) {
3040 output->allocation.width = width;
3041 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003042 if (display->output_configure_handler)
3043 (*display->output_configure_handler)(
3044 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003045 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003046}
3047
3048static const struct wl_output_listener output_listener = {
3049 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003050 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003051};
3052
3053static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003054display_add_output(struct display *d, uint32_t id)
3055{
3056 struct output *output;
3057
3058 output = malloc(sizeof *output);
3059 if (output == NULL)
3060 return;
3061
3062 memset(output, 0, sizeof *output);
3063 output->display = d;
3064 output->output =
3065 wl_display_bind(d->display, id, &wl_output_interface);
3066 wl_list_insert(d->output_list.prev, &output->link);
3067
3068 wl_output_add_listener(output->output, &output_listener, output);
3069}
3070
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003071static void
3072output_destroy(struct output *output)
3073{
3074 if (output->destroy_handler)
3075 (*output->destroy_handler)(output, output->user_data);
3076
3077 wl_output_destroy(output->output);
3078 wl_list_remove(&output->link);
3079 free(output);
3080}
3081
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003082void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003083display_set_output_configure_handler(struct display *display,
3084 display_output_handler_t handler)
3085{
3086 struct output *output;
3087
3088 display->output_configure_handler = handler;
3089 if (!handler)
3090 return;
3091
3092 wl_list_for_each(output, &display->output_list, link)
3093 (*display->output_configure_handler)(output,
3094 display->user_data);
3095}
3096
3097void
3098output_set_user_data(struct output *output, void *data)
3099{
3100 output->user_data = data;
3101}
3102
3103void *
3104output_get_user_data(struct output *output)
3105{
3106 return output->user_data;
3107}
3108
3109void
3110output_set_destroy_handler(struct output *output,
3111 display_output_handler_t handler)
3112{
3113 output->destroy_handler = handler;
3114 /* FIXME: implement this, once we have way to remove outputs */
3115}
3116
3117void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003118output_get_allocation(struct output *output, struct rectangle *allocation)
3119{
3120 *allocation = output->allocation;
3121}
3122
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003123struct wl_output *
3124output_get_wl_output(struct output *output)
3125{
3126 return output->output;
3127}
3128
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003129static void
Daniel Stone97f68542012-05-30 16:32:01 +01003130fini_xkb(struct input *input)
3131{
3132 xkb_state_unref(input->xkb.state);
3133 xkb_map_unref(input->xkb.keymap);
3134}
3135
3136static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003137display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003138{
3139 struct input *input;
3140
3141 input = malloc(sizeof *input);
3142 if (input == NULL)
3143 return;
3144
3145 memset(input, 0, sizeof *input);
3146 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003147 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003148 input->pointer_focus = NULL;
3149 input->keyboard_focus = NULL;
3150 wl_list_insert(d->input_list.prev, &input->link);
3151
Daniel Stone37816df2012-05-16 18:45:18 +01003152 wl_seat_add_listener(input->seat, &seat_listener, input);
3153 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003154
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003155 input->data_device =
3156 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003157 input->seat);
3158 wl_data_device_add_listener(input->data_device, &data_device_listener,
3159 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003160}
3161
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003162static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003163input_destroy(struct input *input)
3164{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003165 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003166 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003167
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003168 if (input->xkb.state)
3169 xkb_state_unref(input->xkb.state);
3170 if (input->xkb.keymap)
3171 xkb_map_unref(input->xkb.keymap);
3172
Pekka Paalanene1207c72011-12-16 12:02:09 +02003173 if (input->drag_offer)
3174 data_offer_destroy(input->drag_offer);
3175
3176 if (input->selection_offer)
3177 data_offer_destroy(input->selection_offer);
3178
3179 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003180 fini_xkb(input);
3181
Pekka Paalanene1207c72011-12-16 12:02:09 +02003182 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003183 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003184 free(input);
3185}
3186
3187static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003188display_handle_global(struct wl_display *display, uint32_t id,
3189 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003190{
3191 struct display *d = data;
3192
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003193 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003194 d->compositor =
3195 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003196 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003197 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003198 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003199 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003200 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003201 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003202 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003203 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003204 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3205 d->data_device_manager =
3206 wl_display_bind(display, id,
3207 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003208 } else if (strcmp(interface, "text_cursor_position") == 0) {
3209 d->text_cursor_position =
3210 wl_display_bind(display, id,
3211 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003212 }
3213}
3214
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003215#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003216static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003217init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003218{
3219 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003220 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003221
Rob Clark6396ed32012-03-11 19:48:41 -05003222#ifdef USE_CAIRO_GLESV2
3223# define GL_BIT EGL_OPENGL_ES2_BIT
3224#else
3225# define GL_BIT EGL_OPENGL_BIT
3226#endif
3227
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003228 static const EGLint argb_cfg_attribs[] = {
3229 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003230 EGL_RED_SIZE, 1,
3231 EGL_GREEN_SIZE, 1,
3232 EGL_BLUE_SIZE, 1,
3233 EGL_ALPHA_SIZE, 1,
3234 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003235 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003236 EGL_NONE
3237 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003238
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003239#ifdef USE_CAIRO_GLESV2
3240 static const EGLint context_attribs[] = {
3241 EGL_CONTEXT_CLIENT_VERSION, 2,
3242 EGL_NONE
3243 };
3244 EGLint api = EGL_OPENGL_ES_API;
3245#else
3246 EGLint *context_attribs = NULL;
3247 EGLint api = EGL_OPENGL_API;
3248#endif
3249
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003250 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003251 if (!eglInitialize(d->dpy, &major, &minor)) {
3252 fprintf(stderr, "failed to initialize display\n");
3253 return -1;
3254 }
3255
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003256 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003257 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3258 return -1;
3259 }
3260
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003261 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3262 &d->argb_config, 1, &n) || n != 1) {
3263 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003264 return -1;
3265 }
3266
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003267 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003268 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003269 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003270 fprintf(stderr, "failed to create context\n");
3271 return -1;
3272 }
3273
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003274 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003275 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003276 return -1;
3277 }
3278
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003279#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003280 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3281 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3282 fprintf(stderr, "failed to get cairo egl argb device\n");
3283 return -1;
3284 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003285#endif
3286
3287 return 0;
3288}
3289
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003290static void
3291fini_egl(struct display *display)
3292{
3293#ifdef HAVE_CAIRO_EGL
3294 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003295#endif
3296
3297 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3298 EGL_NO_CONTEXT);
3299
3300 eglTerminate(display->dpy);
3301 eglReleaseThread();
3302}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003303#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003304
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003305static int
3306event_mask_update(uint32_t mask, void *data)
3307{
3308 struct display *d = data;
3309
3310 d->mask = mask;
3311
3312 return 0;
3313}
3314
3315static void
3316handle_display_data(struct task *task, uint32_t events)
3317{
3318 struct display *display =
3319 container_of(task, struct display, display_task);
3320
3321 wl_display_iterate(display->display, display->mask);
3322}
3323
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003324struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003325display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003326{
3327 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003328
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003329 argc = parse_options(xkb_options,
3330 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003331
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003332 d = malloc(sizeof *d);
3333 if (d == NULL)
3334 return NULL;
3335
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003336 memset(d, 0, sizeof *d);
3337
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003338 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003339 if (d->display == NULL) {
3340 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003341 return NULL;
3342 }
3343
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003344 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003345 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3346 d->display_task.run = handle_display_data;
3347 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3348
3349 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003350 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003351 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003352
Daniel Stone97f68542012-05-30 16:32:01 +01003353 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003354 if (d->xkb_context == NULL) {
3355 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003356 return NULL;
3357 }
3358
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003359 /* Set up listener so we'll catch all events. */
3360 wl_display_add_global_listener(d->display,
3361 display_handle_global, d);
3362
3363 /* Process connection events. */
3364 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003365#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003366 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003367 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003368#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003369
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003370 d->image_target_texture_2d =
3371 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3372 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3373 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3374
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003375 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003376
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003377 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003378
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003379 wl_list_init(&d->window_list);
3380
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003381 return d;
3382}
3383
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003384static void
3385display_destroy_outputs(struct display *display)
3386{
3387 struct output *tmp;
3388 struct output *output;
3389
3390 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3391 output_destroy(output);
3392}
3393
Pekka Paalanene1207c72011-12-16 12:02:09 +02003394static void
3395display_destroy_inputs(struct display *display)
3396{
3397 struct input *tmp;
3398 struct input *input;
3399
3400 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3401 input_destroy(input);
3402}
3403
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003404void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003405display_destroy(struct display *display)
3406{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003407 if (!wl_list_empty(&display->window_list))
3408 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3409
3410 if (!wl_list_empty(&display->deferred_list))
3411 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3412
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003413 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003414 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003415
Daniel Stone97f68542012-05-30 16:32:01 +01003416 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003417
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003418 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003419 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003420
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003421#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003422 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003423#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003424
Pekka Paalanenc2052982011-12-16 11:41:32 +02003425 if (display->shell)
3426 wl_shell_destroy(display->shell);
3427
3428 if (display->shm)
3429 wl_shm_destroy(display->shm);
3430
3431 if (display->data_device_manager)
3432 wl_data_device_manager_destroy(display->data_device_manager);
3433
3434 wl_compositor_destroy(display->compositor);
3435
3436 close(display->epoll_fd);
3437
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003438 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003439 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003440 free(display);
3441}
3442
3443void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003444display_set_user_data(struct display *display, void *data)
3445{
3446 display->user_data = data;
3447}
3448
3449void *
3450display_get_user_data(struct display *display)
3451{
3452 return display->user_data;
3453}
3454
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003455struct wl_display *
3456display_get_display(struct display *display)
3457{
3458 return display->display;
3459}
3460
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003461struct output *
3462display_get_output(struct display *display)
3463{
3464 return container_of(display->output_list.next, struct output, link);
3465}
3466
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003467struct wl_compositor *
3468display_get_compositor(struct display *display)
3469{
3470 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003471}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003472
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003473uint32_t
3474display_get_serial(struct display *display)
3475{
3476 return display->serial;
3477}
3478
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003479EGLDisplay
3480display_get_egl_display(struct display *d)
3481{
3482 return d->dpy;
3483}
3484
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003485struct wl_data_source *
3486display_create_data_source(struct display *display)
3487{
3488 return wl_data_device_manager_create_data_source(display->data_device_manager);
3489}
3490
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003491EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003492display_get_argb_egl_config(struct display *d)
3493{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003494 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003495}
3496
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003497struct wl_shell *
3498display_get_shell(struct display *display)
3499{
3500 return display->shell;
3501}
3502
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003503int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003504display_acquire_window_surface(struct display *display,
3505 struct window *window,
3506 EGLContext ctx)
3507{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003508#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003509 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003510 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003511
3512 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003513 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003514 device = cairo_surface_get_device(window->cairo_surface);
3515 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003516 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003517
Benjamin Franzke0c991632011-09-27 21:57:31 +02003518 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003519 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003520 ctx = display->argb_ctx;
3521 else
3522 assert(0);
3523 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003524
3525 data = cairo_surface_get_user_data(window->cairo_surface,
3526 &surface_data_key);
3527
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003528 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003529 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003530 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3531 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003532
3533 return 0;
3534#else
3535 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003536#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003537}
3538
3539void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003540display_release_window_surface(struct display *display,
3541 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003542{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003543#ifdef HAVE_CAIRO_EGL
3544 cairo_device_t *device;
3545
3546 device = cairo_surface_get_device(window->cairo_surface);
3547 if (!device)
3548 return;
3549
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003550 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003551 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003552 cairo_device_release(device);
3553#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003554}
3555
3556void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003557display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003558{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003559 wl_list_insert(&display->deferred_list, &task->link);
3560}
3561
3562void
3563display_watch_fd(struct display *display,
3564 int fd, uint32_t events, struct task *task)
3565{
3566 struct epoll_event ep;
3567
3568 ep.events = events;
3569 ep.data.ptr = task;
3570 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3571}
3572
3573void
3574display_run(struct display *display)
3575{
3576 struct task *task;
3577 struct epoll_event ep[16];
3578 int i, count;
3579
Pekka Paalanen826d7952011-12-15 10:14:07 +02003580 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003581 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003582 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003583
3584 while (!wl_list_empty(&display->deferred_list)) {
3585 task = container_of(display->deferred_list.next,
3586 struct task, link);
3587 wl_list_remove(&task->link);
3588 task->run(task, 0);
3589 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003590
3591 if (!display->running)
3592 break;
3593
3594 wl_display_flush(display->display);
3595
3596 count = epoll_wait(display->epoll_fd,
3597 ep, ARRAY_LENGTH(ep), -1);
3598 for (i = 0; i < count; i++) {
3599 task = ep[i].data.ptr;
3600 task->run(task, ep[i].events);
3601 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003602 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003603}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003604
3605void
3606display_exit(struct display *display)
3607{
3608 display->running = 0;
3609}