blob: 53022aadf39527ab2b4da607b9ee7a217bf67eae [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{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400427 struct wl_shm_pool *pool;
428 int fd;
429
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300430 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400431 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300432 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
433 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400434 return NULL;
435 }
436
437 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400438 if (*data == MAP_FAILED) {
439 fprintf(stderr, "mmap failed: %m\n");
440 close(fd);
441 return NULL;
442 }
443
444 pool = wl_shm_create_pool(display->shm, fd, size);
445
446 close(fd);
447
448 return pool;
449}
450
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700451static struct shm_pool *
452shm_pool_create(struct display *display, size_t size)
453{
454 struct shm_pool *pool = malloc(sizeof *pool);
455
456 if (!pool)
457 return NULL;
458
459 pool->pool = make_shm_pool(display, size, &pool->data);
460 if (!pool->pool) {
461 free(pool);
462 return NULL;
463 }
464
465 pool->size = size;
466 pool->used = 0;
467
468 return pool;
469}
470
471static void *
472shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
473{
474 if (pool->used + size > pool->size)
475 return NULL;
476
477 *offset = pool->used;
478 pool->used += size;
479
480 return (char *) pool->data + *offset;
481}
482
483/* destroy the pool. this does not unmap the memory though */
484static void
485shm_pool_destroy(struct shm_pool *pool)
486{
487 munmap(pool->data, pool->size);
488 wl_shm_pool_destroy(pool->pool);
489 free(pool);
490}
491
492/* Start allocating from the beginning of the pool again */
493static void
494shm_pool_reset(struct shm_pool *pool)
495{
496 pool->used = 0;
497}
498
499static int
500data_length_for_shm_surface(struct rectangle *rect)
501{
502 int stride;
503
504 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
505 rect->width);
506 return stride * rect->height;
507}
508
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500509static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700510display_create_shm_surface_from_pool(struct display *display,
511 struct rectangle *rectangle,
512 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513{
514 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400515 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400516 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400518 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519
520 data = malloc(sizeof *data);
521 if (data == NULL)
522 return NULL;
523
524 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
525 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700526 length = stride * rectangle->height;
527 data->pool = NULL;
528 map = shm_pool_allocate(pool, length, &offset);
529
530 if (!map) {
531 free(data);
532 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400533 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400535 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536 CAIRO_FORMAT_ARGB32,
537 rectangle->width,
538 rectangle->height,
539 stride);
540
541 cairo_surface_set_user_data (surface, &surface_data_key,
542 data, shm_surface_data_destroy);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500545 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500547 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700549 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400550 rectangle->width,
551 rectangle->height,
552 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700554 return surface;
555}
556
557static cairo_surface_t *
558display_create_shm_surface(struct display *display,
559 struct rectangle *rectangle, uint32_t flags,
560 struct window *window)
561{
562 struct shm_surface_data *data;
563 struct shm_pool *pool;
564 cairo_surface_t *surface;
565
566 if (window && window->pool) {
567 shm_pool_reset(window->pool);
568 surface = display_create_shm_surface_from_pool(display,
569 rectangle,
570 flags,
571 window->pool);
572 if (surface)
573 return surface;
574 }
575
576 pool = shm_pool_create(display,
577 data_length_for_shm_surface(rectangle));
578 if (!pool)
579 return NULL;
580
581 surface =
582 display_create_shm_surface_from_pool(display, rectangle,
583 flags, pool);
584
585 if (!surface) {
586 shm_pool_destroy(pool);
587 return NULL;
588 }
589
590 /* make sure we destroy the pool when the surface is destroyed */
591 data = cairo_surface_get_user_data(surface, &surface_data_key);
592 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593
594 return surface;
595}
596
nobled7b87cb02011-02-01 18:51:47 +0000597static int
598check_size(struct rectangle *rect)
599{
600 if (rect->width && rect->height)
601 return 0;
602
603 fprintf(stderr, "tried to create surface of "
604 "width: %d, height: %d\n", rect->width, rect->height);
605 return -1;
606}
607
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608cairo_surface_t *
609display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100610 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400611 struct rectangle *rectangle,
612 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400613{
nobled7b87cb02011-02-01 18:51:47 +0000614 if (check_size(rectangle) < 0)
615 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500616#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300617 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400618 return display_create_egl_window_surface(display,
619 surface,
620 flags,
621 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400623 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624}
625
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300626static const char *cursors[] = {
627 "bottom_left_corner",
628 "bottom_right_corner",
629 "bottom_side",
630 "grabbing",
631 "left_ptr",
632 "left_side",
633 "right_side",
634 "top_left_corner",
635 "top_right_corner",
636 "top_side",
637 "xterm",
638 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400639 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300640};
641
642static void
643create_cursors(struct display *display)
644{
645 unsigned int i;
646
647 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
648 display->cursors =
649 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
650
651 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
652 display->cursors[i] =
653 wl_cursor_theme_get_cursor(display->cursor_theme,
654 cursors[i]);
655}
656
657static void
658destroy_cursors(struct display *display)
659{
660 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800661 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300662}
663
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300664struct wl_cursor_image *
665display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400666{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300667 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300668 wl_cursor_theme_get_cursor(display->cursor_theme,
669 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300671 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672}
673
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400674static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200675window_get_resize_dx_dy(struct window *window, int *x, int *y)
676{
677 if (window->resize_edges & WINDOW_RESIZING_LEFT)
678 *x = window->server_allocation.width - window->allocation.width;
679 else
680 *x = 0;
681
682 if (window->resize_edges & WINDOW_RESIZING_TOP)
683 *y = window->server_allocation.height -
684 window->allocation.height;
685 else
686 *y = 0;
687
688 window->resize_edges = 0;
689}
690
691static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500692window_attach_surface(struct window *window)
693{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400694 struct display *display = window->display;
695 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000696#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100697 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000698#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500699 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100700
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400701 if (window->type == TYPE_NONE) {
702 window->type = TYPE_TOPLEVEL;
703 if (display->shell)
704 wl_shell_surface_set_toplevel(window->shell_surface);
705 }
706
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100707 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000708#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100709 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
710 data = cairo_surface_get_user_data(window->cairo_surface,
711 &surface_data_key);
712
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100713 cairo_gl_surface_swapbuffers(window->cairo_surface);
714 wl_egl_window_get_attached_size(data->window,
715 &window->server_allocation.width,
716 &window->server_allocation.height);
717 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000718#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100719 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 buffer =
721 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400722 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100723
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200724 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400726 wl_surface_damage(window->surface, 0, 0,
727 window->allocation.width,
728 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 cairo_surface_destroy(window->cairo_surface);
731 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000733 default:
734 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500736
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500737 if (window->input_region) {
738 wl_surface_set_input_region(window->surface,
739 window->input_region);
740 wl_region_destroy(window->input_region);
741 window->input_region = NULL;
742 }
743
744 if (window->opaque_region) {
745 wl_surface_set_opaque_region(window->surface,
746 window->opaque_region);
747 wl_region_destroy(window->opaque_region);
748 window->opaque_region = NULL;
749 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500750}
751
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500752void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400753window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500754{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400755 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100756 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500757}
758
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400759void
760window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400761{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500762 cairo_surface_reference(surface);
763
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400764 if (window->cairo_surface != NULL)
765 cairo_surface_destroy(window->cairo_surface);
766
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500767 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400768}
769
Benjamin Franzke22d54812011-07-16 19:50:32 +0000770#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100771static void
772window_resize_cairo_window_surface(struct window *window)
773{
774 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200775 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100776
777 data = cairo_surface_get_user_data(window->cairo_surface,
778 &surface_data_key);
779
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200780 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781 wl_egl_window_resize(data->window,
782 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 window->allocation.height,
784 x,y);
785
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786 cairo_gl_surface_set_size(window->cairo_surface,
787 window->allocation.width,
788 window->allocation.height);
789}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000790#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100791
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400792struct display *
793window_get_display(struct window *window)
794{
795 return window->display;
796}
797
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400798void
799window_create_surface(struct window *window)
800{
801 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400802 uint32_t flags = 0;
803
804 if (!window->transparent)
805 flags = SURFACE_OPAQUE;
806
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400807 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500808#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
810 if (window->cairo_surface) {
811 window_resize_cairo_window_surface(window);
812 return;
813 }
814 surface = display_create_surface(window->display,
815 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400816 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818#endif
819 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400820 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400821 &window->allocation,
822 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800824 default:
825 surface = NULL;
826 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828
829 window_set_surface(window, surface);
830 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831}
832
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200833static void frame_destroy(struct frame *frame);
834
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400835void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500836window_destroy(struct window *window)
837{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200838 struct display *display = window->display;
839 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100840 struct window_output *window_output;
841 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200842
843 if (window->redraw_scheduled)
844 wl_list_remove(&window->redraw_task.link);
845
846 wl_list_for_each(input, &display->input_list, link) {
847 if (input->pointer_focus == window)
848 input->pointer_focus = NULL;
849 if (input->keyboard_focus == window)
850 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500851 if (input->focus_widget &&
852 input->focus_widget->window == window)
853 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200854 }
855
Rob Bradford7507b572012-05-15 17:55:34 +0100856 wl_list_for_each_safe(window_output, window_output_tmp,
857 &window->window_output_list, link) {
858 free (window_output);
859 }
860
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500861 if (window->input_region)
862 wl_region_destroy(window->input_region);
863 if (window->opaque_region)
864 wl_region_destroy(window->opaque_region);
865
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200866 if (window->frame)
867 frame_destroy(window->frame);
868
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200869 if (window->shell_surface)
870 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500871 wl_surface_destroy(window->surface);
872 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200873
874 if (window->cairo_surface != NULL)
875 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200876
877 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500878 free(window);
879}
880
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500881static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500882widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400883{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500884 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400885
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500886 wl_list_for_each(child, &widget->child_list, link) {
887 target = widget_find_widget(child, x, y);
888 if (target)
889 return target;
890 }
891
892 if (widget->allocation.x <= x &&
893 x < widget->allocation.x + widget->allocation.width &&
894 widget->allocation.y <= y &&
895 y < widget->allocation.y + widget->allocation.height) {
896 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400897 }
898
899 return NULL;
900}
901
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500902static struct widget *
903widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400904{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500905 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400906
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500907 widget = malloc(sizeof *widget);
908 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500909 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500910 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500911 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500912 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500913 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300914 widget->tooltip = NULL;
915 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500916
917 return widget;
918}
919
920struct widget *
921window_add_widget(struct window *window, void *data)
922{
923 window->widget = widget_create(window, data);
924 wl_list_init(&window->widget->link);
925
926 return window->widget;
927}
928
929struct widget *
930widget_add_widget(struct widget *parent, void *data)
931{
932 struct widget *widget;
933
934 widget = widget_create(parent->window, data);
935 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400936
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500937 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400938}
939
940void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500941widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200943 struct display *display = widget->window->display;
944 struct input *input;
945
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300946 if (widget->tooltip) {
947 free(widget->tooltip);
948 widget->tooltip = NULL;
949 }
950
Pekka Paalanene156fb62012-01-19 13:51:38 +0200951 wl_list_for_each(input, &display->input_list, link) {
952 if (input->focus_widget == widget)
953 input->focus_widget = NULL;
954 }
955
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500956 wl_list_remove(&widget->link);
957 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400958}
959
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400960void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500961widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400962{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500963 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400964}
965
966void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500967widget_set_size(struct widget *widget, int32_t width, int32_t height)
968{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500969 widget->allocation.width = width;
970 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500971}
972
973void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500974widget_set_allocation(struct widget *widget,
975 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400976{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977 widget->allocation.x = x;
978 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200979 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400980}
981
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500982void
983widget_set_transparent(struct widget *widget, int transparent)
984{
985 widget->opaque = !transparent;
986}
987
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500989widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500991 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992}
993
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500994void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500995widget_set_resize_handler(struct widget *widget,
996 widget_resize_handler_t handler)
997{
998 widget->resize_handler = handler;
999}
1000
1001void
1002widget_set_redraw_handler(struct widget *widget,
1003 widget_redraw_handler_t handler)
1004{
1005 widget->redraw_handler = handler;
1006}
1007
1008void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001009widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001010{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001011 widget->enter_handler = handler;
1012}
1013
1014void
1015widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1016{
1017 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001018}
1019
1020void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001021widget_set_motion_handler(struct widget *widget,
1022 widget_motion_handler_t handler)
1023{
1024 widget->motion_handler = handler;
1025}
1026
1027void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001028widget_set_button_handler(struct widget *widget,
1029 widget_button_handler_t handler)
1030{
1031 widget->button_handler = handler;
1032}
1033
1034void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001035widget_schedule_redraw(struct widget *widget)
1036{
1037 window_schedule_redraw(widget->window);
1038}
1039
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001040cairo_surface_t *
1041window_get_surface(struct window *window)
1042{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001043 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001044}
1045
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001046struct wl_surface *
1047window_get_wl_surface(struct window *window)
1048{
1049 return window->surface;
1050}
1051
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001052struct wl_shell_surface *
1053window_get_wl_shell_surface(struct window *window)
1054{
1055 return window->shell_surface;
1056}
1057
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001058static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001059tooltip_redraw_handler(struct widget *widget, void *data)
1060{
1061 cairo_t *cr;
1062 const int32_t r = 3;
1063 struct tooltip *tooltip = data;
1064 int32_t width, height;
1065 struct window *window = widget->window;
1066
1067 cr = cairo_create(window->cairo_surface);
1068 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1069 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1070 cairo_paint(cr);
1071
1072 width = window->allocation.width;
1073 height = window->allocation.height;
1074 rounded_rect(cr, 0, 0, width, height, r);
1075
1076 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1077 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1078 cairo_fill(cr);
1079
1080 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1081 cairo_move_to(cr, 10, 16);
1082 cairo_show_text(cr, tooltip->entry);
1083 cairo_destroy(cr);
1084}
1085
1086static cairo_text_extents_t
1087get_text_extents(struct tooltip *tooltip)
1088{
1089 struct window *window;
1090 cairo_t *cr;
1091 cairo_text_extents_t extents;
1092
1093 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1094 * created yet */
1095 window = tooltip->widget->window->parent;
1096 cr = cairo_create(window->cairo_surface);
1097 cairo_text_extents(cr, tooltip->entry, &extents);
1098 cairo_destroy(cr);
1099
1100 return extents;
1101}
1102
1103static int
1104window_create_tooltip(struct tooltip *tooltip)
1105{
1106 struct widget *parent = tooltip->parent;
1107 struct display *display = parent->window->display;
1108 struct window *window;
1109 const int offset_y = 27;
1110 const int margin = 3;
1111 cairo_text_extents_t extents;
1112
1113 if (tooltip->widget)
1114 return 0;
1115
1116 window = window_create_transient(display, parent->window, tooltip->x,
1117 tooltip->y + offset_y,
1118 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1119 if (!window)
1120 return -1;
1121
1122 tooltip->window = window;
1123 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1124
1125 extents = get_text_extents(tooltip);
1126 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1127 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1128
1129 return 0;
1130}
1131
1132void
1133widget_destroy_tooltip(struct widget *parent)
1134{
1135 struct tooltip *tooltip = parent->tooltip;
1136
1137 parent->tooltip_count = 0;
1138 if (!tooltip)
1139 return;
1140
1141 if (tooltip->widget) {
1142 widget_destroy(tooltip->widget);
1143 window_destroy(tooltip->window);
1144 tooltip->widget = NULL;
1145 tooltip->window = NULL;
1146 }
1147
1148 close(tooltip->tooltip_fd);
1149 free(tooltip->entry);
1150 free(tooltip);
1151 parent->tooltip = NULL;
1152}
1153
1154static void
1155tooltip_func(struct task *task, uint32_t events)
1156{
1157 struct tooltip *tooltip =
1158 container_of(task, struct tooltip, tooltip_task);
1159 uint64_t exp;
1160
1161 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1162 window_create_tooltip(tooltip);
1163}
1164
1165#define TOOLTIP_TIMEOUT 500
1166static int
1167tooltip_timer_reset(struct tooltip *tooltip)
1168{
1169 struct itimerspec its;
1170
1171 its.it_interval.tv_sec = 0;
1172 its.it_interval.tv_nsec = 0;
1173 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1174 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1175 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1176 fprintf(stderr, "could not set timerfd\n: %m");
1177 return -1;
1178 }
1179
1180 return 0;
1181}
1182
1183int
1184widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1185{
1186 struct tooltip *tooltip = parent->tooltip;
1187
1188 parent->tooltip_count++;
1189 if (tooltip) {
1190 tooltip->x = x;
1191 tooltip->y = y;
1192 tooltip_timer_reset(tooltip);
1193 return 0;
1194 }
1195
1196 /* the handler might be triggered too fast via input device motion, so
1197 * we need this check here to make sure tooltip is fully initialized */
1198 if (parent->tooltip_count > 1)
1199 return 0;
1200
1201 tooltip = malloc(sizeof *tooltip);
1202 if (!tooltip)
1203 return -1;
1204
1205 parent->tooltip = tooltip;
1206 tooltip->parent = parent;
1207 tooltip->widget = NULL;
1208 tooltip->window = NULL;
1209 tooltip->x = x;
1210 tooltip->y = y;
1211 tooltip->entry = strdup(entry);
1212 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1213 if (tooltip->tooltip_fd < 0) {
1214 fprintf(stderr, "could not create timerfd\n: %m");
1215 return -1;
1216 }
1217
1218 tooltip->tooltip_task.run = tooltip_func;
1219 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1220 EPOLLIN, &tooltip->tooltip_task);
1221 tooltip_timer_reset(tooltip);
1222
1223 return 0;
1224}
1225
1226static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001227frame_resize_handler(struct widget *widget,
1228 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001229{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230 struct frame *frame = data;
1231 struct widget *child = frame->child;
1232 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001233 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001234 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001235 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001236 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001238 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001240 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001241 decoration_width = (t->width + t->margin) * 2;
1242 decoration_height = t->width +
1243 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001245 allocation.x = t->width + t->margin;
1246 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001247 allocation.width = width - decoration_width;
1248 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001249
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001250 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001251
1252 wl_list_for_each(button, &frame->buttons_list, link)
1253 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001254 } else {
1255 decoration_width = 0;
1256 decoration_height = 0;
1257
1258 allocation.x = 0;
1259 allocation.y = 0;
1260 allocation.width = width;
1261 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001262 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001263
1264 wl_list_for_each(button, &frame->buttons_list, link)
1265 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001266 }
1267
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001268 widget_set_allocation(child, allocation.x, allocation.y,
1269 allocation.width, allocation.height);
1270
1271 if (child->resize_handler)
1272 child->resize_handler(child,
1273 allocation.width,
1274 allocation.height,
1275 child->user_data);
1276
Scott Moreauf7e498c2012-05-14 11:39:29 -06001277 width = child->allocation.width + decoration_width;
1278 height = child->allocation.height + decoration_height;
1279
1280 widget->window->input_region =
1281 wl_compositor_create_region(display->compositor);
1282 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001283 t->margin, t->margin,
1284 width - 2 * t->margin,
1285 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001286
1287 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001288
1289 if (child->opaque) {
1290 widget->window->opaque_region =
1291 wl_compositor_create_region(display->compositor);
1292 wl_region_add(widget->window->opaque_region,
1293 opaque_margin, opaque_margin,
1294 widget->allocation.width - 2 * opaque_margin,
1295 widget->allocation.height - 2 * opaque_margin);
1296 }
Martin Minarik1998b152012-05-10 02:04:35 +02001297
1298 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001299 x_r = frame->widget->allocation.width - t->width - t->margin;
1300 x_l = t->width + t->margin;
1301 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001302 wl_list_for_each(button, &frame->buttons_list, link) {
1303 const int button_padding = 4;
1304 w = cairo_image_surface_get_width(button->icon);
1305 h = cairo_image_surface_get_height(button->icon);
1306
1307 if (button->decoration == FRAME_BUTTON_FANCY)
1308 w += 10;
1309
1310 if (button->align == FRAME_BUTTON_LEFT) {
1311 widget_set_allocation(button->widget,
1312 x_l, y , w + 1, h + 1);
1313 x_l += w;
1314 x_l += button_padding;
1315 } else {
1316 x_r -= w;
1317 widget_set_allocation(button->widget,
1318 x_r, y , w + 1, h + 1);
1319 x_r -= button_padding;
1320 }
1321 }
1322}
1323
1324static int
1325frame_button_enter_handler(struct widget *widget,
1326 struct input *input, float x, float y, void *data)
1327{
1328 struct frame_button *frame_button = data;
1329
1330 widget_schedule_redraw(frame_button->widget);
1331 frame_button->state = FRAME_BUTTON_OVER;
1332
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001333 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001334}
1335
1336static void
1337frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1338{
1339 struct frame_button *frame_button = data;
1340
1341 widget_schedule_redraw(frame_button->widget);
1342 frame_button->state = FRAME_BUTTON_DEFAULT;
1343}
1344
1345static void
1346frame_button_button_handler(struct widget *widget,
1347 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001348 uint32_t button,
1349 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001350{
1351 struct frame_button *frame_button = data;
1352 struct window *window = widget->window;
1353
1354 if (button != BTN_LEFT)
1355 return;
1356
1357 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001358 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001359 frame_button->state = FRAME_BUTTON_ACTIVE;
1360 widget_schedule_redraw(frame_button->widget);
1361
1362 if (frame_button->type == FRAME_BUTTON_ICON)
1363 window_show_frame_menu(window, input, time);
1364 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001365 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001366 frame_button->state = FRAME_BUTTON_DEFAULT;
1367 widget_schedule_redraw(frame_button->widget);
1368 break;
1369 }
1370
1371 switch (frame_button->type) {
1372 case FRAME_BUTTON_CLOSE:
1373 if (window->close_handler)
1374 window->close_handler(window->parent,
1375 window->user_data);
1376 else
1377 display_exit(window->display);
1378 break;
1379 case FRAME_BUTTON_MINIMIZE:
1380 fprintf(stderr,"Minimize stub\n");
1381 break;
1382 case FRAME_BUTTON_MAXIMIZE:
1383 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1384 break;
1385 default:
1386 /* Unknown operation */
1387 break;
1388 }
1389}
1390
1391static void
1392frame_button_redraw_handler(struct widget *widget, void *data)
1393{
1394 struct frame_button *frame_button = data;
1395 cairo_t *cr;
1396 int width, height, x, y;
1397 struct window *window = widget->window;
1398
1399 x = widget->allocation.x;
1400 y = widget->allocation.y;
1401 width = widget->allocation.width;
1402 height = widget->allocation.height;
1403
1404 if (!width)
1405 return;
1406 if (!height)
1407 return;
1408 if (widget->opaque)
1409 return;
1410
1411 cr = cairo_create(window->cairo_surface);
1412
1413 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1414 cairo_set_line_width(cr, 1);
1415
1416 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1417 cairo_rectangle (cr, x, y, 25, 16);
1418
1419 cairo_stroke_preserve(cr);
1420
1421 switch (frame_button->state) {
1422 case FRAME_BUTTON_DEFAULT:
1423 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1424 break;
1425 case FRAME_BUTTON_OVER:
1426 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1427 break;
1428 case FRAME_BUTTON_ACTIVE:
1429 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1430 break;
1431 }
1432
1433 cairo_fill (cr);
1434
1435 x += 4;
1436 }
1437
1438 cairo_set_source_surface(cr, frame_button->icon, x, y);
1439 cairo_paint(cr);
1440
1441 cairo_destroy(cr);
1442}
1443
1444static struct widget *
1445frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1446 enum frame_button_align align, enum frame_button_decoration style)
1447{
1448 struct frame_button *frame_button;
1449 const char *icon = data;
1450
1451 frame_button = malloc (sizeof *frame_button);
1452 memset(frame_button, 0, sizeof *frame_button);
1453
1454 frame_button->icon = cairo_image_surface_create_from_png(icon);
1455 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1456 frame_button->frame = frame;
1457 frame_button->type = type;
1458 frame_button->align = align;
1459 frame_button->decoration = style;
1460
1461 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1462
1463 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1464 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1465 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1466 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1467 return frame_button->widget;
1468}
1469
1470static void
1471frame_button_destroy(struct frame_button *frame_button)
1472{
1473 widget_destroy(frame_button->widget);
1474 wl_list_remove(&frame_button->link);
1475 cairo_surface_destroy(frame_button->icon);
1476 free(frame_button);
1477
1478 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001479}
1480
1481static void
1482frame_redraw_handler(struct widget *widget, void *data)
1483{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001484 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001485 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001486 struct theme *t = window->display->theme;
1487 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001488
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001489 if (window->type == TYPE_FULLSCREEN)
1490 return;
1491
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001492 cr = cairo_create(window->cairo_surface);
1493
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001494 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001495 flags |= THEME_FRAME_ACTIVE;
1496 theme_render_frame(t, cr, widget->allocation.width,
1497 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001498
1499 cairo_destroy(cr);
1500}
1501
1502static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001504{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001505 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001506 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001508 location = theme_get_location(t, input->sx, input->sy,
1509 frame->widget->allocation.width,
1510 frame->widget->allocation.height);
1511
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001512 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001513 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001514 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001515 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001516 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001517 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001518 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001519 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001520 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_EXTERIOR:
1530 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001531 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001534}
1535
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001536static void
1537frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001538{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001539 switch (index) {
1540 case 0: /* close */
1541 if (window->close_handler)
1542 window->close_handler(window->parent,
1543 window->user_data);
1544 else
1545 display_exit(window->display);
1546 break;
1547 case 1: /* fullscreen */
1548 /* we don't have a way to get out of fullscreen for now */
1549 window_set_fullscreen(window, 1);
1550 break;
1551 case 2: /* rotate */
1552 case 3: /* scale */
1553 break;
1554 }
1555}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001556
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001557void
1558window_show_frame_menu(struct window *window,
1559 struct input *input, uint32_t time)
1560{
1561 int32_t x, y;
1562
1563 static const char *entries[] = {
1564 "Close", "Fullscreen", "Rotate", "Scale"
1565 };
1566
1567 input_get_position(input, &x, &y);
1568 window_show_menu(window->display, input, time, window,
1569 x - 10, y - 10, frame_menu_func, entries, 4);
1570}
1571
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001572static int
1573frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001574 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001575{
1576 return frame_get_pointer_image_for_location(data, input);
1577}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001578
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001579static int
1580frame_motion_handler(struct widget *widget,
1581 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001582 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001583{
1584 return frame_get_pointer_image_for_location(data, input);
1585}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001586
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588frame_button_handler(struct widget *widget,
1589 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001590 uint32_t button, enum wl_pointer_button_state state,
1591 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001592
1593{
1594 struct frame *frame = data;
1595 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001596 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001597 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001598
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001599 location = theme_get_location(display->theme, input->sx, input->sy,
1600 frame->widget->allocation.width,
1601 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001602
Daniel Stone4dbadb12012-05-30 16:31:51 +01001603 if (window->display->shell && button == BTN_LEFT &&
1604 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001606 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001607 if (!window->shell_surface)
1608 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001609 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001611 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001612 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001613 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001615 case THEME_LOCATION_RESIZING_TOP:
1616 case THEME_LOCATION_RESIZING_BOTTOM:
1617 case THEME_LOCATION_RESIZING_LEFT:
1618 case THEME_LOCATION_RESIZING_RIGHT:
1619 case THEME_LOCATION_RESIZING_TOP_LEFT:
1620 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1621 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1622 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001623 if (!window->shell_surface)
1624 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001625 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001626
1627 if (!display->dpy) {
1628 /* If we're using shm, allocate a big
1629 pool to create buffers out of while
1630 we resize. We should probably base
1631 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001632 window->pool =
1633 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001634 }
1635
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001636 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001637 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001638 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001639 break;
1640 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001641 } else if (button == BTN_RIGHT &&
1642 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001643 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 }
1645}
1646
1647struct widget *
1648frame_create(struct window *window, void *data)
1649{
1650 struct frame *frame;
1651
1652 frame = malloc(sizeof *frame);
1653 memset(frame, 0, sizeof *frame);
1654
1655 frame->widget = window_add_widget(window, frame);
1656 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001657
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001658 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1659 widget_set_resize_handler(frame->widget, frame_resize_handler);
1660 widget_set_enter_handler(frame->widget, frame_enter_handler);
1661 widget_set_motion_handler(frame->widget, frame_motion_handler);
1662 widget_set_button_handler(frame->widget, frame_button_handler);
1663
Martin Minarik1998b152012-05-10 02:04:35 +02001664 /* Create empty list for frame buttons */
1665 wl_list_init(&frame->buttons_list);
1666
1667 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1668 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1669
1670 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1671 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1672
1673 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1674 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1677 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001679 window->frame = frame;
1680
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001681 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001682}
1683
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001684static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001685frame_destroy(struct frame *frame)
1686{
Martin Minarik1998b152012-05-10 02:04:35 +02001687 struct frame_button *button, *tmp;
1688
1689 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1690 frame_button_destroy(button);
1691
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001692 /* frame->child must be destroyed by the application */
1693 widget_destroy(frame->widget);
1694 free(frame);
1695}
1696
1697static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001698input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001699 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001701 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001702 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001704 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705 return;
1706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001708 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001709 widget = old;
1710 if (input->grab)
1711 widget = input->grab;
1712 if (widget->leave_handler)
1713 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001714 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001715 }
1716
1717 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001718 widget = focus;
1719 if (input->grab)
1720 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001721 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001722 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001723 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001724 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001725
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001726 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001727 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728}
1729
1730static void
Daniel Stone37816df2012-05-16 18:45:18 +01001731pointer_handle_motion(void *data, struct wl_pointer *pointer,
1732 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001733{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001734 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001735 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001736 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001737 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001738 float sx = wl_fixed_to_double(sx_w);
1739 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001740
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001741 input->sx = sx;
1742 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001743
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001744 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001745 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001746 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747 }
1748
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001749 if (input->grab)
1750 widget = input->grab;
1751 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001752 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001753 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001754 cursor = widget->motion_handler(input->focus_widget,
1755 input, time, sx, sy,
1756 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001757
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001758 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001759}
1760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761void
1762input_grab(struct input *input, struct widget *widget, uint32_t button)
1763{
1764 input->grab = widget;
1765 input->grab_button = button;
1766}
1767
1768void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001769input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770{
1771 struct widget *widget;
1772
1773 input->grab = NULL;
1774 if (input->pointer_focus) {
1775 widget = widget_find_widget(input->pointer_focus->widget,
1776 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001777 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778 }
1779}
1780
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001781static void
Daniel Stone37816df2012-05-16 18:45:18 +01001782pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001783 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001784{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001785 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001786 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001787 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001788
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001789 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001790 if (input->focus_widget && input->grab == NULL &&
1791 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001792 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001793
Neil Roberts6b28aad2012-01-23 19:11:18 +00001794 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001795 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001796 (*widget->button_handler)(widget,
1797 input, time,
1798 button, state,
1799 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001800
Daniel Stone4dbadb12012-05-30 16:31:51 +01001801 if (input->grab && input->grab_button == button &&
1802 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001803 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001804}
1805
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001806static void
Daniel Stone37816df2012-05-16 18:45:18 +01001807pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001808 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001809{
1810}
1811
1812static void
Daniel Stone37816df2012-05-16 18:45:18 +01001813keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001814 uint32_t serial, uint32_t time, uint32_t key,
1815 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001816{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001817 struct input *input = data;
1818 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001819 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001820 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001821 const xkb_keysym_t *syms;
1822 xkb_keysym_t sym;
1823 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001824
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001825 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001826 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001827 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001828 return;
1829
Daniel Stone97f68542012-05-30 16:32:01 +01001830 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831
Daniel Stone97f68542012-05-30 16:32:01 +01001832 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001833 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001834 XKB_STATE_LATCHED);
1835 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001836 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001837 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001838 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001839 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001840 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001841 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001842
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001843 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001844 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001845 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001846 window_set_maximized(window,
1847 window->type != TYPE_MAXIMIZED);
1848 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001849 if (num_syms == 1)
1850 sym = syms[0];
1851 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001852 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001853
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001854 (*window->key_handler)(window, input, time, key,
1855 sym, state, window->user_data);
1856 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001857}
1858
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001859static void
Daniel Stone351eb612012-05-31 15:27:47 -04001860keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1861 uint32_t serial, uint32_t mods_depressed,
1862 uint32_t mods_latched, uint32_t mods_locked,
1863 uint32_t group)
1864{
1865 struct input *input = data;
1866
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001867 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1868 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001869}
1870
1871static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001872input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001873{
1874 struct window *window = input->pointer_focus;
1875
1876 if (!window)
1877 return;
1878
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001879 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001880
Pekka Paalanene1207c72011-12-16 12:02:09 +02001881 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001882 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001883}
1884
1885static void
Daniel Stone37816df2012-05-16 18:45:18 +01001886pointer_handle_enter(void *data, struct wl_pointer *pointer,
1887 uint32_t serial, struct wl_surface *surface,
1888 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001889{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001890 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001891 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001892 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001893 float sx = wl_fixed_to_double(sx_w);
1894 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001895
Martin Minarik1998b152012-05-10 02:04:35 +02001896 if (!surface) {
1897 /* enter event for a window we've just destroyed */
1898 return;
1899 }
1900
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001901 input->display->serial = serial;
1902 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001903 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001904 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001905
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001906 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001907 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001908 window->pool = NULL;
1909 /* Schedule a redraw to free the pool */
1910 window_schedule_redraw(window);
1911 }
1912
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001913 input->sx = sx;
1914 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001915
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001916 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001917 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001918}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001919
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001920static void
Daniel Stone37816df2012-05-16 18:45:18 +01001921pointer_handle_leave(void *data, struct wl_pointer *pointer,
1922 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001923{
1924 struct input *input = data;
1925
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001926 input->display->serial = serial;
1927 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001928}
1929
1930static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001931input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001932{
1933 struct window *window = input->keyboard_focus;
1934
1935 if (!window)
1936 return;
1937
1938 window->keyboard_device = NULL;
1939 if (window->keyboard_focus_handler)
1940 (*window->keyboard_focus_handler)(window, NULL,
1941 window->user_data);
1942
1943 input->keyboard_focus = NULL;
1944}
1945
1946static void
Daniel Stone37816df2012-05-16 18:45:18 +01001947keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1948 uint32_t serial, struct wl_surface *surface,
1949 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001950{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001951 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001952 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001953
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001954 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001955 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001956
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001957 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001958 window->keyboard_device = input;
1959 if (window->keyboard_focus_handler)
1960 (*window->keyboard_focus_handler)(window,
1961 window->keyboard_device,
1962 window->user_data);
1963}
1964
1965static void
Daniel Stone37816df2012-05-16 18:45:18 +01001966keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1967 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001968{
1969 struct input *input = data;
1970
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001971 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001972 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001973}
1974
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001975static void
1976keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1977 uint32_t format, int fd, uint32_t size)
1978{
1979 struct input *input = data;
1980 char *map_str;
1981
1982 if (!data) {
1983 close(fd);
1984 return;
1985 }
1986
1987 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1988 close(fd);
1989 return;
1990 }
1991
1992 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1993 if (map_str == MAP_FAILED) {
1994 close(fd);
1995 return;
1996 }
1997
1998 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
1999 map_str,
2000 XKB_KEYMAP_FORMAT_TEXT_V1,
2001 0);
2002 munmap(map_str, size);
2003 close(fd);
2004
2005 if (!input->xkb.keymap) {
2006 fprintf(stderr, "failed to compile keymap\n");
2007 return;
2008 }
2009
2010 input->xkb.state = xkb_state_new(input->xkb.keymap);
2011 if (!input->xkb.state) {
2012 fprintf(stderr, "failed to create XKB state\n");
2013 xkb_map_unref(input->xkb.keymap);
2014 input->xkb.keymap = NULL;
2015 return;
2016 }
2017
2018 input->xkb.control_mask =
2019 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2020 input->xkb.alt_mask =
2021 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2022 input->xkb.shift_mask =
2023 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2024}
2025
Daniel Stone37816df2012-05-16 18:45:18 +01002026static const struct wl_pointer_listener pointer_listener = {
2027 pointer_handle_enter,
2028 pointer_handle_leave,
2029 pointer_handle_motion,
2030 pointer_handle_button,
2031 pointer_handle_axis,
2032};
2033
2034static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002035 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002036 keyboard_handle_enter,
2037 keyboard_handle_leave,
2038 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002039 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002040};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002041
2042static void
Daniel Stone37816df2012-05-16 18:45:18 +01002043seat_handle_capabilities(void *data, struct wl_seat *seat,
2044 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002045{
Daniel Stone37816df2012-05-16 18:45:18 +01002046 struct input *input = data;
2047
2048 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2049 input->pointer = wl_seat_get_pointer(seat);
2050 wl_pointer_set_user_data(input->pointer, input);
2051 wl_pointer_add_listener(input->pointer, &pointer_listener,
2052 input);
2053 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2054 wl_pointer_destroy(input->pointer);
2055 input->pointer = NULL;
2056 }
2057
2058 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2059 input->keyboard = wl_seat_get_keyboard(seat);
2060 wl_keyboard_set_user_data(input->keyboard, input);
2061 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2062 input);
2063 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2064 wl_keyboard_destroy(input->keyboard);
2065 input->keyboard = NULL;
2066 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002067}
2068
Daniel Stone37816df2012-05-16 18:45:18 +01002069static const struct wl_seat_listener seat_listener = {
2070 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002071};
2072
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002073void
2074input_get_position(struct input *input, int32_t *x, int32_t *y)
2075{
2076 *x = input->sx;
2077 *y = input->sy;
2078}
2079
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002080struct display *
2081input_get_display(struct input *input)
2082{
2083 return input->display;
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øgsberg7cee1972012-06-04 23:37:42 -04002277input_set_pointer_image_index(struct input *input, int pointer, int index)
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 Oliveirad8f527c2012-05-25 09:30:02 +03002283 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002284 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002285 return;
2286
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002287 if (index >= (int) cursor->image_count) {
2288 fprintf(stderr, "cursor index out of range\n");
2289 return;
2290 }
2291
2292 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002293 buffer = wl_cursor_image_get_buffer(image);
2294 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002295 return;
2296
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002297 input->current_cursor = pointer;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002298 wl_pointer_attach(input->pointer, input->pointer_enter_serial,
2299 buffer, image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300}
2301
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04002302void
2303input_set_pointer_image(struct input *input, int pointer)
2304{
2305 if (pointer == input->current_cursor)
2306 return;
2307
2308 input_set_pointer_image_index(input, pointer, 0);
2309}
2310
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002311struct wl_data_device *
2312input_get_data_device(struct input *input)
2313{
2314 return input->data_device;
2315}
2316
2317void
2318input_set_selection(struct input *input,
2319 struct wl_data_source *source, uint32_t time)
2320{
2321 wl_data_device_set_selection(input->data_device, source, time);
2322}
2323
2324void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002325input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002326{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002327 wl_data_offer_accept(input->drag_offer->offer,
2328 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002329}
2330
2331static void
2332offer_io_func(struct task *task, uint32_t events)
2333{
2334 struct data_offer *offer =
2335 container_of(task, struct data_offer, io_task);
2336 unsigned int len;
2337 char buffer[4096];
2338
2339 len = read(offer->fd, buffer, sizeof buffer);
2340 offer->func(buffer, len,
2341 offer->x, offer->y, offer->user_data);
2342
2343 if (len == 0) {
2344 close(offer->fd);
2345 data_offer_destroy(offer);
2346 }
2347}
2348
2349static void
2350data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2351 data_func_t func, void *user_data)
2352{
2353 int p[2];
2354
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002355 if (pipe2(p, O_CLOEXEC) == -1)
2356 return;
2357
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002358 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2359 close(p[1]);
2360
2361 offer->io_task.run = offer_io_func;
2362 offer->fd = p[0];
2363 offer->func = func;
2364 offer->refcount++;
2365 offer->user_data = user_data;
2366
2367 display_watch_fd(offer->input->display,
2368 offer->fd, EPOLLIN, &offer->io_task);
2369}
2370
2371void
2372input_receive_drag_data(struct input *input, const char *mime_type,
2373 data_func_t func, void *data)
2374{
2375 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2376 input->drag_offer->x = input->sx;
2377 input->drag_offer->y = input->sy;
2378}
2379
2380int
2381input_receive_selection_data(struct input *input, const char *mime_type,
2382 data_func_t func, void *data)
2383{
2384 char **p;
2385
2386 if (input->selection_offer == NULL)
2387 return -1;
2388
2389 for (p = input->selection_offer->types.data; *p; p++)
2390 if (strcmp(mime_type, *p) == 0)
2391 break;
2392
2393 if (*p == NULL)
2394 return -1;
2395
2396 data_offer_receive_data(input->selection_offer,
2397 mime_type, func, data);
2398 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002399}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002400
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002401int
2402input_receive_selection_data_to_fd(struct input *input,
2403 const char *mime_type, int fd)
2404{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002405 if (input->selection_offer)
2406 wl_data_offer_receive(input->selection_offer->offer,
2407 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002408
2409 return 0;
2410}
2411
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002412void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002413window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002414{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002415 if (!window->shell_surface)
2416 return;
2417
Daniel Stone37816df2012-05-16 18:45:18 +01002418 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002419}
2420
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002421static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002422idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002423{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002424 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002425
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002426 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002427 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002428 widget_set_allocation(widget,
2429 window->pending_allocation.x,
2430 window->pending_allocation.y,
2431 window->pending_allocation.width,
2432 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002433
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002434 if (window->input_region) {
2435 wl_region_destroy(window->input_region);
2436 window->input_region = NULL;
2437 }
2438
2439 if (window->opaque_region) {
2440 wl_region_destroy(window->opaque_region);
2441 window->opaque_region = NULL;
2442 }
2443
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002444 if (widget->resize_handler)
2445 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002446 widget->allocation.width,
2447 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002448 widget->user_data);
2449
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002450 if (window->allocation.width != widget->allocation.width ||
2451 window->allocation.height != widget->allocation.height) {
2452 window->allocation = widget->allocation;
2453 window_schedule_redraw(window);
2454 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002455}
2456
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002457void
2458window_schedule_resize(struct window *window, int width, int height)
2459{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002460 window->pending_allocation.x = 0;
2461 window->pending_allocation.y = 0;
2462 window->pending_allocation.width = width;
2463 window->pending_allocation.height = height;
2464
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002465 window->resize_needed = 1;
2466 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002467}
2468
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002469void
2470widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2471{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002473}
2474
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002475static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002476handle_ping(void *data, struct wl_shell_surface *shell_surface,
2477 uint32_t serial)
2478{
2479 wl_shell_surface_pong(shell_surface, serial);
2480}
2481
2482static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002483handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002484 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002485{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002486 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002487
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002488 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002489 return;
2490
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002491 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002492 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002493}
2494
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002495static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002496menu_destroy(struct menu *menu)
2497{
2498 widget_destroy(menu->widget);
2499 window_destroy(menu->window);
2500 free(menu);
2501}
2502
2503static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002504handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2505{
2506 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002507 struct menu *menu = window->widget->user_data;
2508
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002509 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002510 * device. Or just use wl_callback. And this really needs to
2511 * be a window vfunc that the menu can set. And we need the
2512 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002513
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002514 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002515 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002516 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002517}
2518
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002519static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002520 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002521 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002522 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002523};
2524
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002525void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002526window_get_allocation(struct window *window,
2527 struct rectangle *allocation)
2528{
2529 *allocation = window->allocation;
2530}
2531
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002532static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002533widget_redraw(struct widget *widget)
2534{
2535 struct widget *child;
2536
2537 if (widget->redraw_handler)
2538 widget->redraw_handler(widget, widget->user_data);
2539 wl_list_for_each(child, &widget->child_list, link)
2540 widget_redraw(child);
2541}
2542
2543static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002544frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2545{
2546 struct window *window = data;
2547
2548 wl_callback_destroy(callback);
2549 window->redraw_scheduled = 0;
2550 if (window->redraw_needed)
2551 window_schedule_redraw(window);
2552}
2553
2554static const struct wl_callback_listener listener = {
2555 frame_callback
2556};
2557
2558static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002559idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002560{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002561 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002562 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002563
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002564 if (window->resize_needed)
2565 idle_resize(window);
2566
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002567 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002568 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002569 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002570 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002571 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002572
2573 callback = wl_surface_frame(window->surface);
2574 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002575}
2576
2577void
2578window_schedule_redraw(struct window *window)
2579{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002580 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002581 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002582 window->redraw_task.run = idle_redraw;
2583 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002584 window->redraw_scheduled = 1;
2585 }
2586}
2587
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002588void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002589window_set_custom(struct window *window)
2590{
2591 window->type = TYPE_CUSTOM;
2592}
2593
2594void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002595window_set_fullscreen(struct window *window, int fullscreen)
2596{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002597 if (!window->display->shell)
2598 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002599
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002600 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002601 return;
2602
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002603 if (fullscreen) {
2604 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002605 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002606 wl_shell_surface_set_fullscreen(window->shell_surface,
2607 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2608 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002609 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002610 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002611 wl_shell_surface_set_toplevel(window->shell_surface);
2612 window_schedule_resize(window,
2613 window->saved_allocation.width,
2614 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002615 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002616}
2617
2618void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002619window_set_maximized(struct window *window, int maximized)
2620{
2621 if (!window->display->shell)
2622 return;
2623
2624 if ((window->type == TYPE_MAXIMIZED) == maximized)
2625 return;
2626
2627 if (window->type == TYPE_TOPLEVEL) {
2628 window->saved_allocation = window->allocation;
2629 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2630 window->type = TYPE_MAXIMIZED;
2631 } else {
2632 wl_shell_surface_set_toplevel(window->shell_surface);
2633 window->type = TYPE_TOPLEVEL;
2634 window_schedule_resize(window,
2635 window->saved_allocation.width,
2636 window->saved_allocation.height);
2637 }
2638}
2639
2640void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002641window_set_user_data(struct window *window, void *data)
2642{
2643 window->user_data = data;
2644}
2645
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002646void *
2647window_get_user_data(struct window *window)
2648{
2649 return window->user_data;
2650}
2651
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002652void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002653window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002654 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002655{
2656 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002657}
2658
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002659void
2660window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002661 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002662{
2663 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002664}
2665
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002666void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002667window_set_data_handler(struct window *window, window_data_handler_t handler)
2668{
2669 window->data_handler = handler;
2670}
2671
2672void
2673window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2674{
2675 window->drop_handler = handler;
2676}
2677
2678void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002679window_set_close_handler(struct window *window,
2680 window_close_handler_t handler)
2681{
2682 window->close_handler = handler;
2683}
2684
2685void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002686window_set_title(struct window *window, const char *title)
2687{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002688 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002689 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002690 if (window->shell_surface)
2691 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002692}
2693
2694const char *
2695window_get_title(struct window *window)
2696{
2697 return window->title;
2698}
2699
2700void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002701window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2702{
2703 struct text_cursor_position *text_cursor_position =
2704 window->display->text_cursor_position;
2705
Scott Moreau9295ce02012-06-01 12:46:10 -06002706 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002707 return;
2708
2709 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002710 window->surface,
2711 wl_fixed_from_int(x),
2712 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002713}
2714
2715void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002716window_damage(struct window *window, int32_t x, int32_t y,
2717 int32_t width, int32_t height)
2718{
2719 wl_surface_damage(window->surface, x, y, width, height);
2720}
2721
Casey Dahlin9074db52012-04-19 22:50:09 -04002722static void
2723surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002724 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002725{
Rob Bradford7507b572012-05-15 17:55:34 +01002726 struct window *window = data;
2727 struct output *output;
2728 struct output *output_found = NULL;
2729 struct window_output *window_output;
2730
2731 wl_list_for_each(output, &window->display->output_list, link) {
2732 if (output->output == wl_output) {
2733 output_found = output;
2734 break;
2735 }
2736 }
2737
2738 if (!output_found)
2739 return;
2740
2741 window_output = malloc (sizeof *window_output);
2742 window_output->output = output_found;
2743
2744 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002745}
2746
2747static void
2748surface_leave(void *data,
2749 struct wl_surface *wl_surface, struct wl_output *output)
2750{
Rob Bradford7507b572012-05-15 17:55:34 +01002751 struct window *window = data;
2752 struct window_output *window_output;
2753 struct window_output *window_output_found = NULL;
2754
2755 wl_list_for_each(window_output, &window->window_output_list, link) {
2756 if (window_output->output->output == output) {
2757 window_output_found = window_output;
2758 break;
2759 }
2760 }
2761
2762 if (window_output_found) {
2763 wl_list_remove(&window_output_found->link);
2764 free(window_output_found);
2765 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002766}
2767
2768static const struct wl_surface_listener surface_listener = {
2769 surface_enter,
2770 surface_leave
2771};
2772
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002773static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002774window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002775{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002776 struct window *window;
2777
2778 window = malloc(sizeof *window);
2779 if (window == NULL)
2780 return NULL;
2781
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002782 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002783 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002784 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002785 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002786 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002787 if (display->shell) {
2788 window->shell_surface =
2789 wl_shell_get_shell_surface(display->shell,
2790 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002791 if (window->title)
2792 wl_shell_surface_set_title(window->shell_surface,
2793 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002794 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002795 window->allocation.x = 0;
2796 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002797 window->allocation.width = 0;
2798 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002799 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002800 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002801 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002802 window->input_region = NULL;
2803 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002804
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002805 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002806#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002807 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002808#else
2809 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2810#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002811 else
2812 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002813
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002814 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002815 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002816 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002817
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002818 if (window->shell_surface) {
2819 wl_shell_surface_set_user_data(window->shell_surface, window);
2820 wl_shell_surface_add_listener(window->shell_surface,
2821 &shell_surface_listener, window);
2822 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002823
Rob Bradford7507b572012-05-15 17:55:34 +01002824 wl_list_init (&window->window_output_list);
2825
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002826 return window;
2827}
2828
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002829struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002830window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002831{
2832 struct window *window;
2833
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002834 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002835 if (!window)
2836 return NULL;
2837
2838 return window;
2839}
2840
2841struct window *
2842window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002843 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002844{
2845 struct window *window;
2846
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002847 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002848 if (!window)
2849 return NULL;
2850
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002851 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002852 window->x = x;
2853 window->y = y;
2854
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002855 if (display->shell)
2856 wl_shell_surface_set_transient(window->shell_surface,
2857 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002858 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002859
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002860 return window;
2861}
2862
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002863static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002864menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002865{
2866 int next;
2867
2868 next = (sy - 8) / 20;
2869 if (menu->current != next) {
2870 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002871 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002872 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002873}
2874
2875static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002876menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002877 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002878 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002879{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002880 struct menu *menu = data;
2881
2882 if (widget == menu->widget)
2883 menu_set_item(data, y);
2884
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002885 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002886}
2887
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002888static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002889menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002890 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002891{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002892 struct menu *menu = data;
2893
2894 if (widget == menu->widget)
2895 menu_set_item(data, y);
2896
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002897 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002898}
2899
2900static void
2901menu_leave_handler(struct widget *widget, struct input *input, void *data)
2902{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002903 struct menu *menu = data;
2904
2905 if (widget == menu->widget)
2906 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002907}
2908
2909static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002910menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002911 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002912 uint32_t button, enum wl_pointer_button_state state,
2913 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002914
2915{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002916 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002917
Daniel Stone4dbadb12012-05-30 16:31:51 +01002918 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2919 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002920 /* Either relase after press-drag-release or
2921 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002922 menu->func(menu->window->parent,
2923 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002924 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002925 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002926 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002927}
2928
2929static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002930menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002931{
2932 cairo_t *cr;
2933 const int32_t r = 3, margin = 3;
2934 struct menu *menu = data;
2935 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002936 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002937
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002938 cr = cairo_create(window->cairo_surface);
2939 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2940 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2941 cairo_paint(cr);
2942
2943 width = window->allocation.width;
2944 height = window->allocation.height;
2945 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002946
2947 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002948 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2949 cairo_fill(cr);
2950
2951 for (i = 0; i < menu->count; i++) {
2952 if (i == menu->current) {
2953 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2954 cairo_rectangle(cr, margin, i * 20 + margin,
2955 width - 2 * margin, 20);
2956 cairo_fill(cr);
2957 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2958 cairo_move_to(cr, 10, i * 20 + 16);
2959 cairo_show_text(cr, menu->entries[i]);
2960 } else {
2961 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2962 cairo_move_to(cr, 10, i * 20 + 16);
2963 cairo_show_text(cr, menu->entries[i]);
2964 }
2965 }
2966
2967 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002968}
2969
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002970void
2971window_show_menu(struct display *display,
2972 struct input *input, uint32_t time, struct window *parent,
2973 int32_t x, int32_t y,
2974 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002975{
2976 struct window *window;
2977 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002978 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002979
2980 menu = malloc(sizeof *menu);
2981 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002982 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002983
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002984 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002985 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002986 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002987
2988 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002989 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002990 menu->entries = entries;
2991 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002992 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002994 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002995 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002996 window->type = TYPE_MENU;
2997 window->x = x;
2998 window->y = y;
2999
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04003000 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01003001 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04003002 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003 window->parent->shell_surface,
3004 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003005
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003006 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05003007 widget_set_enter_handler(menu->widget, menu_enter_handler);
3008 widget_set_leave_handler(menu->widget, menu_leave_handler);
3009 widget_set_motion_handler(menu->widget, menu_motion_handler);
3010 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003011
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003012 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003013 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003014}
3015
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003016void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003017window_set_buffer_type(struct window *window, enum window_buffer_type type)
3018{
3019 window->buffer_type = type;
3020}
3021
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003022
3023static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003024display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003025 struct wl_output *wl_output,
3026 int x, int y,
3027 int physical_width,
3028 int physical_height,
3029 int subpixel,
3030 const char *make,
3031 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003032{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003033 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003034
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003035 output->allocation.x = x;
3036 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003037}
3038
3039static void
3040display_handle_mode(void *data,
3041 struct wl_output *wl_output,
3042 uint32_t flags,
3043 int width,
3044 int height,
3045 int refresh)
3046{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003047 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003048 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003049
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003050 if (flags & WL_OUTPUT_MODE_CURRENT) {
3051 output->allocation.width = width;
3052 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003053 if (display->output_configure_handler)
3054 (*display->output_configure_handler)(
3055 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003056 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003057}
3058
3059static const struct wl_output_listener output_listener = {
3060 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003061 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003062};
3063
3064static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003065display_add_output(struct display *d, uint32_t id)
3066{
3067 struct output *output;
3068
3069 output = malloc(sizeof *output);
3070 if (output == NULL)
3071 return;
3072
3073 memset(output, 0, sizeof *output);
3074 output->display = d;
3075 output->output =
3076 wl_display_bind(d->display, id, &wl_output_interface);
3077 wl_list_insert(d->output_list.prev, &output->link);
3078
3079 wl_output_add_listener(output->output, &output_listener, output);
3080}
3081
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003082static void
3083output_destroy(struct output *output)
3084{
3085 if (output->destroy_handler)
3086 (*output->destroy_handler)(output, output->user_data);
3087
3088 wl_output_destroy(output->output);
3089 wl_list_remove(&output->link);
3090 free(output);
3091}
3092
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003093void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003094display_set_output_configure_handler(struct display *display,
3095 display_output_handler_t handler)
3096{
3097 struct output *output;
3098
3099 display->output_configure_handler = handler;
3100 if (!handler)
3101 return;
3102
3103 wl_list_for_each(output, &display->output_list, link)
3104 (*display->output_configure_handler)(output,
3105 display->user_data);
3106}
3107
3108void
3109output_set_user_data(struct output *output, void *data)
3110{
3111 output->user_data = data;
3112}
3113
3114void *
3115output_get_user_data(struct output *output)
3116{
3117 return output->user_data;
3118}
3119
3120void
3121output_set_destroy_handler(struct output *output,
3122 display_output_handler_t handler)
3123{
3124 output->destroy_handler = handler;
3125 /* FIXME: implement this, once we have way to remove outputs */
3126}
3127
3128void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003129output_get_allocation(struct output *output, struct rectangle *allocation)
3130{
3131 *allocation = output->allocation;
3132}
3133
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003134struct wl_output *
3135output_get_wl_output(struct output *output)
3136{
3137 return output->output;
3138}
3139
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003140static void
Daniel Stone97f68542012-05-30 16:32:01 +01003141fini_xkb(struct input *input)
3142{
3143 xkb_state_unref(input->xkb.state);
3144 xkb_map_unref(input->xkb.keymap);
3145}
3146
3147static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003148display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003149{
3150 struct input *input;
3151
3152 input = malloc(sizeof *input);
3153 if (input == NULL)
3154 return;
3155
3156 memset(input, 0, sizeof *input);
3157 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003158 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003159 input->pointer_focus = NULL;
3160 input->keyboard_focus = NULL;
3161 wl_list_insert(d->input_list.prev, &input->link);
3162
Daniel Stone37816df2012-05-16 18:45:18 +01003163 wl_seat_add_listener(input->seat, &seat_listener, input);
3164 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003165
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003166 input->data_device =
3167 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003168 input->seat);
3169 wl_data_device_add_listener(input->data_device, &data_device_listener,
3170 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003171}
3172
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003173static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003174input_destroy(struct input *input)
3175{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003176 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003177 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003178
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003179 if (input->xkb.state)
3180 xkb_state_unref(input->xkb.state);
3181 if (input->xkb.keymap)
3182 xkb_map_unref(input->xkb.keymap);
3183
Pekka Paalanene1207c72011-12-16 12:02:09 +02003184 if (input->drag_offer)
3185 data_offer_destroy(input->drag_offer);
3186
3187 if (input->selection_offer)
3188 data_offer_destroy(input->selection_offer);
3189
3190 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003191 fini_xkb(input);
3192
Pekka Paalanene1207c72011-12-16 12:02:09 +02003193 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003194 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003195 free(input);
3196}
3197
3198static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003199display_handle_global(struct wl_display *display, uint32_t id,
3200 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003201{
3202 struct display *d = data;
3203
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003204 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003205 d->compositor =
3206 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003207 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003208 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003209 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003210 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003211 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003212 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003213 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003214 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003215 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3216 d->data_device_manager =
3217 wl_display_bind(display, id,
3218 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003219 } else if (strcmp(interface, "text_cursor_position") == 0) {
3220 d->text_cursor_position =
3221 wl_display_bind(display, id,
3222 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003223 }
3224}
3225
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003226#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003227static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003228init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003229{
3230 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003231 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003232
Rob Clark6396ed32012-03-11 19:48:41 -05003233#ifdef USE_CAIRO_GLESV2
3234# define GL_BIT EGL_OPENGL_ES2_BIT
3235#else
3236# define GL_BIT EGL_OPENGL_BIT
3237#endif
3238
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003239 static const EGLint argb_cfg_attribs[] = {
3240 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003241 EGL_RED_SIZE, 1,
3242 EGL_GREEN_SIZE, 1,
3243 EGL_BLUE_SIZE, 1,
3244 EGL_ALPHA_SIZE, 1,
3245 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003246 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003247 EGL_NONE
3248 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003249
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003250#ifdef USE_CAIRO_GLESV2
3251 static const EGLint context_attribs[] = {
3252 EGL_CONTEXT_CLIENT_VERSION, 2,
3253 EGL_NONE
3254 };
3255 EGLint api = EGL_OPENGL_ES_API;
3256#else
3257 EGLint *context_attribs = NULL;
3258 EGLint api = EGL_OPENGL_API;
3259#endif
3260
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003261 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003262 if (!eglInitialize(d->dpy, &major, &minor)) {
3263 fprintf(stderr, "failed to initialize display\n");
3264 return -1;
3265 }
3266
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003267 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003268 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3269 return -1;
3270 }
3271
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003272 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3273 &d->argb_config, 1, &n) || n != 1) {
3274 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003275 return -1;
3276 }
3277
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003278 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003279 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003280 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003281 fprintf(stderr, "failed to create context\n");
3282 return -1;
3283 }
3284
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003285 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003286 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003287 return -1;
3288 }
3289
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003290#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003291 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3292 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3293 fprintf(stderr, "failed to get cairo egl argb device\n");
3294 return -1;
3295 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003296#endif
3297
3298 return 0;
3299}
3300
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003301static void
3302fini_egl(struct display *display)
3303{
3304#ifdef HAVE_CAIRO_EGL
3305 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003306#endif
3307
3308 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3309 EGL_NO_CONTEXT);
3310
3311 eglTerminate(display->dpy);
3312 eglReleaseThread();
3313}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003314#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003315
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003316static int
3317event_mask_update(uint32_t mask, void *data)
3318{
3319 struct display *d = data;
3320
3321 d->mask = mask;
3322
3323 return 0;
3324}
3325
3326static void
3327handle_display_data(struct task *task, uint32_t events)
3328{
3329 struct display *display =
3330 container_of(task, struct display, display_task);
3331
3332 wl_display_iterate(display->display, display->mask);
3333}
3334
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003335struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003336display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003337{
3338 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003339
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003340 argc = parse_options(xkb_options,
3341 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003342
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003343 d = malloc(sizeof *d);
3344 if (d == NULL)
3345 return NULL;
3346
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003347 memset(d, 0, sizeof *d);
3348
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003349 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003350 if (d->display == NULL) {
3351 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003352 return NULL;
3353 }
3354
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003355 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003356 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3357 d->display_task.run = handle_display_data;
3358 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3359
3360 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003361 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003362 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003363
Daniel Stone97f68542012-05-30 16:32:01 +01003364 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003365 if (d->xkb_context == NULL) {
3366 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003367 return NULL;
3368 }
3369
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003370 /* Set up listener so we'll catch all events. */
3371 wl_display_add_global_listener(d->display,
3372 display_handle_global, d);
3373
3374 /* Process connection events. */
3375 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003376#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003377 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003378 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003379#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003380
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003381 d->image_target_texture_2d =
3382 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3383 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3384 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3385
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003386 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003387
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003388 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003389
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003390 wl_list_init(&d->window_list);
3391
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003392 return d;
3393}
3394
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003395static void
3396display_destroy_outputs(struct display *display)
3397{
3398 struct output *tmp;
3399 struct output *output;
3400
3401 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3402 output_destroy(output);
3403}
3404
Pekka Paalanene1207c72011-12-16 12:02:09 +02003405static void
3406display_destroy_inputs(struct display *display)
3407{
3408 struct input *tmp;
3409 struct input *input;
3410
3411 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3412 input_destroy(input);
3413}
3414
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003415void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003416display_destroy(struct display *display)
3417{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003418 if (!wl_list_empty(&display->window_list))
3419 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3420
3421 if (!wl_list_empty(&display->deferred_list))
3422 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3423
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003424 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003425 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003426
Daniel Stone97f68542012-05-30 16:32:01 +01003427 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003428
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003429 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003430 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003431
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003432#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003433 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003434#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003435
Pekka Paalanenc2052982011-12-16 11:41:32 +02003436 if (display->shell)
3437 wl_shell_destroy(display->shell);
3438
3439 if (display->shm)
3440 wl_shm_destroy(display->shm);
3441
3442 if (display->data_device_manager)
3443 wl_data_device_manager_destroy(display->data_device_manager);
3444
3445 wl_compositor_destroy(display->compositor);
3446
3447 close(display->epoll_fd);
3448
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003449 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003450 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003451 free(display);
3452}
3453
3454void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003455display_set_user_data(struct display *display, void *data)
3456{
3457 display->user_data = data;
3458}
3459
3460void *
3461display_get_user_data(struct display *display)
3462{
3463 return display->user_data;
3464}
3465
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003466struct wl_display *
3467display_get_display(struct display *display)
3468{
3469 return display->display;
3470}
3471
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003472struct output *
3473display_get_output(struct display *display)
3474{
3475 return container_of(display->output_list.next, struct output, link);
3476}
3477
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003478struct wl_compositor *
3479display_get_compositor(struct display *display)
3480{
3481 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003482}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003483
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003484uint32_t
3485display_get_serial(struct display *display)
3486{
3487 return display->serial;
3488}
3489
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003490EGLDisplay
3491display_get_egl_display(struct display *d)
3492{
3493 return d->dpy;
3494}
3495
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003496struct wl_data_source *
3497display_create_data_source(struct display *display)
3498{
3499 return wl_data_device_manager_create_data_source(display->data_device_manager);
3500}
3501
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003502EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003503display_get_argb_egl_config(struct display *d)
3504{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003505 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003506}
3507
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003508struct wl_shell *
3509display_get_shell(struct display *display)
3510{
3511 return display->shell;
3512}
3513
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003514int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003515display_acquire_window_surface(struct display *display,
3516 struct window *window,
3517 EGLContext ctx)
3518{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003519#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003520 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003521 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003522
3523 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003524 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003525 device = cairo_surface_get_device(window->cairo_surface);
3526 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003527 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003528
Benjamin Franzke0c991632011-09-27 21:57:31 +02003529 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003530 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003531 ctx = display->argb_ctx;
3532 else
3533 assert(0);
3534 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003535
3536 data = cairo_surface_get_user_data(window->cairo_surface,
3537 &surface_data_key);
3538
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003539 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003540 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003541 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3542 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003543
3544 return 0;
3545#else
3546 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003547#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003548}
3549
3550void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003551display_release_window_surface(struct display *display,
3552 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003553{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003554#ifdef HAVE_CAIRO_EGL
3555 cairo_device_t *device;
3556
3557 device = cairo_surface_get_device(window->cairo_surface);
3558 if (!device)
3559 return;
3560
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003561 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003562 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003563 cairo_device_release(device);
3564#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003565}
3566
3567void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003568display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003569{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003570 wl_list_insert(&display->deferred_list, &task->link);
3571}
3572
3573void
3574display_watch_fd(struct display *display,
3575 int fd, uint32_t events, struct task *task)
3576{
3577 struct epoll_event ep;
3578
3579 ep.events = events;
3580 ep.data.ptr = task;
3581 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3582}
3583
3584void
3585display_run(struct display *display)
3586{
3587 struct task *task;
3588 struct epoll_event ep[16];
3589 int i, count;
3590
Pekka Paalanen826d7952011-12-15 10:14:07 +02003591 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003592 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003593 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003594
3595 while (!wl_list_empty(&display->deferred_list)) {
3596 task = container_of(display->deferred_list.next,
3597 struct task, link);
3598 wl_list_remove(&task->link);
3599 task->run(task, 0);
3600 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003601
3602 if (!display->running)
3603 break;
3604
3605 wl_display_flush(display->display);
3606
3607 count = epoll_wait(display->epoll_fd,
3608 ep, ARRAY_LENGTH(ep), -1);
3609 for (i = 0; i < count; i++) {
3610 task = ep[i].data.ptr;
3611 task->run(task, ep[i].events);
3612 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003613 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003614}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003615
3616void
3617display_exit(struct display *display)
3618{
3619 display->running = 0;
3620}