blob: 314e5e98f10f753ee784cd0431a52203a3440578 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030039#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030059#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040063#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060064#include "text-cursor-position-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030065#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050068
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070069struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030070
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050071struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050072 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050073 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040074 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040075 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060077 struct text_cursor_position *text_cursor_position;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040078 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050079 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020080 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020081 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040082 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040083
84 int display_fd;
85 uint32_t mask;
86 struct task display_task;
87
88 int epoll_fd;
89 struct wl_list deferred_list;
90
Pekka Paalanen826d7952011-12-15 10:14:07 +020091 int running;
92
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040094 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050095 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040096
Kristian Høgsberg5adb4802012-05-15 22:25:28 -040097 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030099 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300100 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400101
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200105
106 display_output_handler_t output_configure_handler;
107
108 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100109
110 struct xkb_context *xkb_context;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500111};
112
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400114 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400115 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400116 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500117 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500119 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_CUSTOM
121};
Rob Bradford7507b572012-05-15 17:55:34 +0100122
123struct window_output {
124 struct output *output;
125 struct wl_list link;
126};
127
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500128struct window {
129 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500130 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100131 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500132 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200133 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500136 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500137 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500138 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500139 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400140 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400141 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400142 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400143 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400144 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400146 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400147 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400148 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500149
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400150 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500151
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700152 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400153
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500154 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500155 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500158 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200160 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500161 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500162
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500163 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400164 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500165};
166
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500168 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300169 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500170 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 struct wl_list link;
172 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500177 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500178 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400179 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500180 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300181 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400189 struct window *pointer_focus;
190 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300191 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400192 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400193 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400194 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400195 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400196
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500197 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500198 struct widget *grab;
199 uint32_t grab_button;
200
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201 struct wl_data_device *data_device;
202 struct data_offer *drag_offer;
203 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100204
205 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100206 struct xkb_keymap *keymap;
207 struct xkb_state *state;
208 xkb_mod_mask_t control_mask;
209 xkb_mod_mask_t alt_mask;
210 xkb_mod_mask_t shift_mask;
211 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400212};
213
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500214struct output {
215 struct display *display;
216 struct wl_output *output;
217 struct rectangle allocation;
218 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200219
220 display_output_handler_t destroy_handler;
221 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500222};
223
Martin Minarik1998b152012-05-10 02:04:35 +0200224enum frame_button_action {
225 FRAME_BUTTON_NULL = 0,
226 FRAME_BUTTON_ICON = 1,
227 FRAME_BUTTON_CLOSE = 2,
228 FRAME_BUTTON_MINIMIZE = 3,
229 FRAME_BUTTON_MAXIMIZE = 4,
230};
231
232enum frame_button_pointer {
233 FRAME_BUTTON_DEFAULT = 0,
234 FRAME_BUTTON_OVER = 1,
235 FRAME_BUTTON_ACTIVE = 2,
236};
237
238enum frame_button_align {
239 FRAME_BUTTON_RIGHT = 0,
240 FRAME_BUTTON_LEFT = 1,
241};
242
243enum frame_button_decoration {
244 FRAME_BUTTON_NONE = 0,
245 FRAME_BUTTON_FANCY = 1,
246};
247
248struct frame_button {
249 struct widget *widget;
250 struct frame *frame;
251 cairo_surface_t *icon;
252 enum frame_button_action type;
253 enum frame_button_pointer state;
254 struct wl_list link; /* buttons_list */
255 enum frame_button_align align;
256 enum frame_button_decoration decoration;
257};
258
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500259struct frame {
260 struct widget *widget;
261 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200262 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500263};
264
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500265struct menu {
266 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500267 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500268 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500269 const char **entries;
270 uint32_t time;
271 int current;
272 int count;
273 menu_func_t func;
274};
275
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276struct tooltip {
277 struct widget *parent;
278 struct window *window;
279 struct widget *widget;
280 char *entry;
281 struct task tooltip_task;
282 int tooltip_fd;
283 float x, y;
284};
285
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700286struct shm_pool {
287 struct wl_shm_pool *pool;
288 size_t size;
289 size_t used;
290 void *data;
291};
292
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400293enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300294 CURSOR_DEFAULT = 100,
295 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400296};
297
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500298enum window_location {
299 WINDOW_INTERIOR = 0,
300 WINDOW_RESIZING_TOP = 1,
301 WINDOW_RESIZING_BOTTOM = 2,
302 WINDOW_RESIZING_LEFT = 4,
303 WINDOW_RESIZING_TOP_LEFT = 5,
304 WINDOW_RESIZING_BOTTOM_LEFT = 6,
305 WINDOW_RESIZING_RIGHT = 8,
306 WINDOW_RESIZING_TOP_RIGHT = 9,
307 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
308 WINDOW_RESIZING_MASK = 15,
309 WINDOW_EXTERIOR = 16,
310 WINDOW_TITLEBAR = 17,
311 WINDOW_CLIENT_AREA = 18,
312};
313
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400314const char *option_xkb_layout = "us";
315const char *option_xkb_variant = "";
316const char *option_xkb_options = "";
317
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400318static const struct weston_option xkb_options[] = {
319 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
320 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
321 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400322};
323
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324static const cairo_user_data_key_t surface_data_key;
325struct surface_data {
326 struct wl_buffer *buffer;
327};
328
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500329#define MULT(_d,c,a,t) \
330 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
331
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500332#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100334struct egl_window_surface_data {
335 struct display *display;
336 struct wl_surface *surface;
337 struct wl_egl_window *window;
338 EGLSurface surf;
339};
340
341static void
342egl_window_surface_data_destroy(void *p)
343{
344 struct egl_window_surface_data *data = p;
345 struct display *d = data->display;
346
347 eglDestroySurface(d->dpy, data->surf);
348 wl_egl_window_destroy(data->window);
349 data->surface = NULL;
350
351 free(p);
352}
353
354static cairo_surface_t *
355display_create_egl_window_surface(struct display *display,
356 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100358 struct rectangle *rectangle)
359{
360 cairo_surface_t *cairo_surface;
361 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200363 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
369 data->display = display;
370 data->surface = surface;
371
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500372 config = display->argb_config;
373 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400375 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500380 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100381
Benjamin Franzke0c991632011-09-27 21:57:31 +0200382 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383 data->surf,
384 rectangle->width,
385 rectangle->height);
386
387 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
388 data, egl_window_surface_data_destroy);
389
390 return cairo_surface;
391}
392
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393#endif
394
395struct wl_buffer *
396display_get_buffer_for_surface(struct display *display,
397 cairo_surface_t *surface)
398{
399 struct surface_data *data;
400
401 data = cairo_surface_get_user_data (surface, &surface_data_key);
402
403 return data->buffer;
404}
405
406struct shm_surface_data {
407 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700408 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409};
410
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500411static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700412shm_pool_destroy(struct shm_pool *pool);
413
414static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415shm_surface_data_destroy(void *p)
416{
417 struct shm_surface_data *data = p;
418
419 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700420 if (data->pool)
421 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422}
423
Kristian Høgsberg16626282012-04-03 11:21:27 -0400424static struct wl_shm_pool *
425make_shm_pool(struct display *display, int size, void **data)
426{
427 char filename[] = "/tmp/wayland-shm-XXXXXX";
428 struct wl_shm_pool *pool;
429 int fd;
430
431 fd = mkstemp(filename);
432 if (fd < 0) {
433 fprintf(stderr, "open %s failed: %m\n", filename);
434 return NULL;
435 }
436 if (ftruncate(fd, size) < 0) {
437 fprintf(stderr, "ftruncate failed: %m\n");
438 close(fd);
439 return NULL;
440 }
441
442 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
443 unlink(filename);
444
445 if (*data == MAP_FAILED) {
446 fprintf(stderr, "mmap failed: %m\n");
447 close(fd);
448 return NULL;
449 }
450
451 pool = wl_shm_create_pool(display->shm, fd, size);
452
453 close(fd);
454
455 return pool;
456}
457
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700458static struct shm_pool *
459shm_pool_create(struct display *display, size_t size)
460{
461 struct shm_pool *pool = malloc(sizeof *pool);
462
463 if (!pool)
464 return NULL;
465
466 pool->pool = make_shm_pool(display, size, &pool->data);
467 if (!pool->pool) {
468 free(pool);
469 return NULL;
470 }
471
472 pool->size = size;
473 pool->used = 0;
474
475 return pool;
476}
477
478static void *
479shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
480{
481 if (pool->used + size > pool->size)
482 return NULL;
483
484 *offset = pool->used;
485 pool->used += size;
486
487 return (char *) pool->data + *offset;
488}
489
490/* destroy the pool. this does not unmap the memory though */
491static void
492shm_pool_destroy(struct shm_pool *pool)
493{
494 munmap(pool->data, pool->size);
495 wl_shm_pool_destroy(pool->pool);
496 free(pool);
497}
498
499/* Start allocating from the beginning of the pool again */
500static void
501shm_pool_reset(struct shm_pool *pool)
502{
503 pool->used = 0;
504}
505
506static int
507data_length_for_shm_surface(struct rectangle *rect)
508{
509 int stride;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rect->width);
513 return stride * rect->height;
514}
515
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500516static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700517display_create_shm_surface_from_pool(struct display *display,
518 struct rectangle *rectangle,
519 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520{
521 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400522 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400523 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700524 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400525 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526
527 data = malloc(sizeof *data);
528 if (data == NULL)
529 return NULL;
530
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
532 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700533 length = stride * rectangle->height;
534 data->pool = NULL;
535 map = shm_pool_allocate(pool, length, &offset);
536
537 if (!map) {
538 free(data);
539 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400540 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400542 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500552 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500554 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700556 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400557 rectangle->width,
558 rectangle->height,
559 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700561 return surface;
562}
563
564static cairo_surface_t *
565display_create_shm_surface(struct display *display,
566 struct rectangle *rectangle, uint32_t flags,
567 struct window *window)
568{
569 struct shm_surface_data *data;
570 struct shm_pool *pool;
571 cairo_surface_t *surface;
572
573 if (window && window->pool) {
574 shm_pool_reset(window->pool);
575 surface = display_create_shm_surface_from_pool(display,
576 rectangle,
577 flags,
578 window->pool);
579 if (surface)
580 return surface;
581 }
582
583 pool = shm_pool_create(display,
584 data_length_for_shm_surface(rectangle));
585 if (!pool)
586 return NULL;
587
588 surface =
589 display_create_shm_surface_from_pool(display, rectangle,
590 flags, pool);
591
592 if (!surface) {
593 shm_pool_destroy(pool);
594 return NULL;
595 }
596
597 /* make sure we destroy the pool when the surface is destroyed */
598 data = cairo_surface_get_user_data(surface, &surface_data_key);
599 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600
601 return surface;
602}
603
nobled7b87cb02011-02-01 18:51:47 +0000604static int
605check_size(struct rectangle *rect)
606{
607 if (rect->width && rect->height)
608 return 0;
609
610 fprintf(stderr, "tried to create surface of "
611 "width: %d, height: %d\n", rect->width, rect->height);
612 return -1;
613}
614
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615cairo_surface_t *
616display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400618 struct rectangle *rectangle,
619 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400620{
nobled7b87cb02011-02-01 18:51:47 +0000621 if (check_size(rectangle) < 0)
622 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500623#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300624 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400625 return display_create_egl_window_surface(display,
626 surface,
627 flags,
628 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400630 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631}
632
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300633static const char *cursors[] = {
634 "bottom_left_corner",
635 "bottom_right_corner",
636 "bottom_side",
637 "grabbing",
638 "left_ptr",
639 "left_side",
640 "right_side",
641 "top_left_corner",
642 "top_right_corner",
643 "top_side",
644 "xterm",
645 "hand1",
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -0400646 "watch",
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300647};
648
649static void
650create_cursors(struct display *display)
651{
652 unsigned int i;
653
654 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
655 display->cursors =
656 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
657
658 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
659 display->cursors[i] =
660 wl_cursor_theme_get_cursor(display->cursor_theme,
661 cursors[i]);
662}
663
664static void
665destroy_cursors(struct display *display)
666{
667 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800668 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300669}
670
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300671struct wl_cursor_image *
672display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300674 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300675 wl_cursor_theme_get_cursor(display->cursor_theme,
676 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300678 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400679}
680
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400681static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200682window_get_resize_dx_dy(struct window *window, int *x, int *y)
683{
684 if (window->resize_edges & WINDOW_RESIZING_LEFT)
685 *x = window->server_allocation.width - window->allocation.width;
686 else
687 *x = 0;
688
689 if (window->resize_edges & WINDOW_RESIZING_TOP)
690 *y = window->server_allocation.height -
691 window->allocation.height;
692 else
693 *y = 0;
694
695 window->resize_edges = 0;
696}
697
698static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500699window_attach_surface(struct window *window)
700{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400701 struct display *display = window->display;
702 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000703#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100704 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000705#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500706 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100707
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400708 if (window->type == TYPE_NONE) {
709 window->type = TYPE_TOPLEVEL;
710 if (display->shell)
711 wl_shell_surface_set_toplevel(window->shell_surface);
712 }
713
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100714 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000715#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100716 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
717 data = cairo_surface_get_user_data(window->cairo_surface,
718 &surface_data_key);
719
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100720 cairo_gl_surface_swapbuffers(window->cairo_surface);
721 wl_egl_window_get_attached_size(data->window,
722 &window->server_allocation.width,
723 &window->server_allocation.height);
724 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000725#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 buffer =
728 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400729 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100730
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200731 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400733 wl_surface_damage(window->surface, 0, 0,
734 window->allocation.width,
735 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400737 cairo_surface_destroy(window->cairo_surface);
738 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000740 default:
741 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500743
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500744 if (window->input_region) {
745 wl_surface_set_input_region(window->surface,
746 window->input_region);
747 wl_region_destroy(window->input_region);
748 window->input_region = NULL;
749 }
750
751 if (window->opaque_region) {
752 wl_surface_set_opaque_region(window->surface,
753 window->opaque_region);
754 wl_region_destroy(window->opaque_region);
755 window->opaque_region = NULL;
756 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500757}
758
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500759void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400760window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500761{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400762 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100763 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500764}
765
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400766void
767window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400768{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500769 cairo_surface_reference(surface);
770
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771 if (window->cairo_surface != NULL)
772 cairo_surface_destroy(window->cairo_surface);
773
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500774 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400775}
776
Benjamin Franzke22d54812011-07-16 19:50:32 +0000777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778static void
779window_resize_cairo_window_surface(struct window *window)
780{
781 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200782 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783
784 data = cairo_surface_get_user_data(window->cairo_surface,
785 &surface_data_key);
786
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200787 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 wl_egl_window_resize(data->window,
789 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200790 window->allocation.height,
791 x,y);
792
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793 cairo_gl_surface_set_size(window->cairo_surface,
794 window->allocation.width,
795 window->allocation.height);
796}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400799struct display *
800window_get_display(struct window *window)
801{
802 return window->display;
803}
804
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400805void
806window_create_surface(struct window *window)
807{
808 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400809 uint32_t flags = 0;
810
811 if (!window->transparent)
812 flags = SURFACE_OPAQUE;
813
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500815#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
817 if (window->cairo_surface) {
818 window_resize_cairo_window_surface(window);
819 return;
820 }
821 surface = display_create_surface(window->display,
822 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400823 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825#endif
826 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400827 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400828 &window->allocation,
829 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800831 default:
832 surface = NULL;
833 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400835
836 window_set_surface(window, surface);
837 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400838}
839
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200840static void frame_destroy(struct frame *frame);
841
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400842void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500843window_destroy(struct window *window)
844{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200845 struct display *display = window->display;
846 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100847 struct window_output *window_output;
848 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200849
850 if (window->redraw_scheduled)
851 wl_list_remove(&window->redraw_task.link);
852
853 wl_list_for_each(input, &display->input_list, link) {
854 if (input->pointer_focus == window)
855 input->pointer_focus = NULL;
856 if (input->keyboard_focus == window)
857 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500858 if (input->focus_widget &&
859 input->focus_widget->window == window)
860 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200861 }
862
Rob Bradford7507b572012-05-15 17:55:34 +0100863 wl_list_for_each_safe(window_output, window_output_tmp,
864 &window->window_output_list, link) {
865 free (window_output);
866 }
867
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500868 if (window->input_region)
869 wl_region_destroy(window->input_region);
870 if (window->opaque_region)
871 wl_region_destroy(window->opaque_region);
872
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200873 if (window->frame)
874 frame_destroy(window->frame);
875
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200876 if (window->shell_surface)
877 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500878 wl_surface_destroy(window->surface);
879 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200880
881 if (window->cairo_surface != NULL)
882 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200883
884 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500885 free(window);
886}
887
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500888static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500889widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400890{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500891 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400892
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500893 wl_list_for_each(child, &widget->child_list, link) {
894 target = widget_find_widget(child, x, y);
895 if (target)
896 return target;
897 }
898
899 if (widget->allocation.x <= x &&
900 x < widget->allocation.x + widget->allocation.width &&
901 widget->allocation.y <= y &&
902 y < widget->allocation.y + widget->allocation.height) {
903 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400904 }
905
906 return NULL;
907}
908
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500909static struct widget *
910widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400911{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500912 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400913
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500914 widget = malloc(sizeof *widget);
915 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500916 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500917 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500918 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500919 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500920 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300921 widget->tooltip = NULL;
922 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500923
924 return widget;
925}
926
927struct widget *
928window_add_widget(struct window *window, void *data)
929{
930 window->widget = widget_create(window, data);
931 wl_list_init(&window->widget->link);
932
933 return window->widget;
934}
935
936struct widget *
937widget_add_widget(struct widget *parent, void *data)
938{
939 struct widget *widget;
940
941 widget = widget_create(parent->window, data);
942 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400943
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500944 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400945}
946
947void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500948widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200950 struct display *display = widget->window->display;
951 struct input *input;
952
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300953 if (widget->tooltip) {
954 free(widget->tooltip);
955 widget->tooltip = NULL;
956 }
957
Pekka Paalanene156fb62012-01-19 13:51:38 +0200958 wl_list_for_each(input, &display->input_list, link) {
959 if (input->focus_widget == widget)
960 input->focus_widget = NULL;
961 }
962
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500963 wl_list_remove(&widget->link);
964 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400965}
966
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400967void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500968widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400969{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500970 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400971}
972
973void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500974widget_set_size(struct widget *widget, int32_t width, int32_t height)
975{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500976 widget->allocation.width = width;
977 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500978}
979
980void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500981widget_set_allocation(struct widget *widget,
982 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500984 widget->allocation.x = x;
985 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200986 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987}
988
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500989void
990widget_set_transparent(struct widget *widget, int transparent)
991{
992 widget->opaque = !transparent;
993}
994
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999}
1000
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001001void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001002widget_set_resize_handler(struct widget *widget,
1003 widget_resize_handler_t handler)
1004{
1005 widget->resize_handler = handler;
1006}
1007
1008void
1009widget_set_redraw_handler(struct widget *widget,
1010 widget_redraw_handler_t handler)
1011{
1012 widget->redraw_handler = handler;
1013}
1014
1015void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001016widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001017{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001018 widget->enter_handler = handler;
1019}
1020
1021void
1022widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1023{
1024 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001025}
1026
1027void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001028widget_set_motion_handler(struct widget *widget,
1029 widget_motion_handler_t handler)
1030{
1031 widget->motion_handler = handler;
1032}
1033
1034void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001035widget_set_button_handler(struct widget *widget,
1036 widget_button_handler_t handler)
1037{
1038 widget->button_handler = handler;
1039}
1040
1041void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001042widget_schedule_redraw(struct widget *widget)
1043{
1044 window_schedule_redraw(widget->window);
1045}
1046
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001047cairo_surface_t *
1048window_get_surface(struct window *window)
1049{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001050 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001051}
1052
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001053struct wl_surface *
1054window_get_wl_surface(struct window *window)
1055{
1056 return window->surface;
1057}
1058
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001059struct wl_shell_surface *
1060window_get_wl_shell_surface(struct window *window)
1061{
1062 return window->shell_surface;
1063}
1064
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001065static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001066tooltip_redraw_handler(struct widget *widget, void *data)
1067{
1068 cairo_t *cr;
1069 const int32_t r = 3;
1070 struct tooltip *tooltip = data;
1071 int32_t width, height;
1072 struct window *window = widget->window;
1073
1074 cr = cairo_create(window->cairo_surface);
1075 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1076 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1077 cairo_paint(cr);
1078
1079 width = window->allocation.width;
1080 height = window->allocation.height;
1081 rounded_rect(cr, 0, 0, width, height, r);
1082
1083 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1084 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1085 cairo_fill(cr);
1086
1087 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1088 cairo_move_to(cr, 10, 16);
1089 cairo_show_text(cr, tooltip->entry);
1090 cairo_destroy(cr);
1091}
1092
1093static cairo_text_extents_t
1094get_text_extents(struct tooltip *tooltip)
1095{
1096 struct window *window;
1097 cairo_t *cr;
1098 cairo_text_extents_t extents;
1099
1100 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1101 * created yet */
1102 window = tooltip->widget->window->parent;
1103 cr = cairo_create(window->cairo_surface);
1104 cairo_text_extents(cr, tooltip->entry, &extents);
1105 cairo_destroy(cr);
1106
1107 return extents;
1108}
1109
1110static int
1111window_create_tooltip(struct tooltip *tooltip)
1112{
1113 struct widget *parent = tooltip->parent;
1114 struct display *display = parent->window->display;
1115 struct window *window;
1116 const int offset_y = 27;
1117 const int margin = 3;
1118 cairo_text_extents_t extents;
1119
1120 if (tooltip->widget)
1121 return 0;
1122
1123 window = window_create_transient(display, parent->window, tooltip->x,
1124 tooltip->y + offset_y,
1125 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1126 if (!window)
1127 return -1;
1128
1129 tooltip->window = window;
1130 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1131
1132 extents = get_text_extents(tooltip);
1133 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1134 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1135
1136 return 0;
1137}
1138
1139void
1140widget_destroy_tooltip(struct widget *parent)
1141{
1142 struct tooltip *tooltip = parent->tooltip;
1143
1144 parent->tooltip_count = 0;
1145 if (!tooltip)
1146 return;
1147
1148 if (tooltip->widget) {
1149 widget_destroy(tooltip->widget);
1150 window_destroy(tooltip->window);
1151 tooltip->widget = NULL;
1152 tooltip->window = NULL;
1153 }
1154
1155 close(tooltip->tooltip_fd);
1156 free(tooltip->entry);
1157 free(tooltip);
1158 parent->tooltip = NULL;
1159}
1160
1161static void
1162tooltip_func(struct task *task, uint32_t events)
1163{
1164 struct tooltip *tooltip =
1165 container_of(task, struct tooltip, tooltip_task);
1166 uint64_t exp;
1167
1168 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1169 window_create_tooltip(tooltip);
1170}
1171
1172#define TOOLTIP_TIMEOUT 500
1173static int
1174tooltip_timer_reset(struct tooltip *tooltip)
1175{
1176 struct itimerspec its;
1177
1178 its.it_interval.tv_sec = 0;
1179 its.it_interval.tv_nsec = 0;
1180 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1181 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1182 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1183 fprintf(stderr, "could not set timerfd\n: %m");
1184 return -1;
1185 }
1186
1187 return 0;
1188}
1189
1190int
1191widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1192{
1193 struct tooltip *tooltip = parent->tooltip;
1194
1195 parent->tooltip_count++;
1196 if (tooltip) {
1197 tooltip->x = x;
1198 tooltip->y = y;
1199 tooltip_timer_reset(tooltip);
1200 return 0;
1201 }
1202
1203 /* the handler might be triggered too fast via input device motion, so
1204 * we need this check here to make sure tooltip is fully initialized */
1205 if (parent->tooltip_count > 1)
1206 return 0;
1207
1208 tooltip = malloc(sizeof *tooltip);
1209 if (!tooltip)
1210 return -1;
1211
1212 parent->tooltip = tooltip;
1213 tooltip->parent = parent;
1214 tooltip->widget = NULL;
1215 tooltip->window = NULL;
1216 tooltip->x = x;
1217 tooltip->y = y;
1218 tooltip->entry = strdup(entry);
1219 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1220 if (tooltip->tooltip_fd < 0) {
1221 fprintf(stderr, "could not create timerfd\n: %m");
1222 return -1;
1223 }
1224
1225 tooltip->tooltip_task.run = tooltip_func;
1226 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1227 EPOLLIN, &tooltip->tooltip_task);
1228 tooltip_timer_reset(tooltip);
1229
1230 return 0;
1231}
1232
1233static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001234frame_resize_handler(struct widget *widget,
1235 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 struct frame *frame = data;
1238 struct widget *child = frame->child;
1239 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001240 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001242 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001243 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001245 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001247 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001248 decoration_width = (t->width + t->margin) * 2;
1249 decoration_height = t->width +
1250 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001252 allocation.x = t->width + t->margin;
1253 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001254 allocation.width = width - decoration_width;
1255 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001256
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001257 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001258
1259 wl_list_for_each(button, &frame->buttons_list, link)
1260 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001261 } else {
1262 decoration_width = 0;
1263 decoration_height = 0;
1264
1265 allocation.x = 0;
1266 allocation.y = 0;
1267 allocation.width = width;
1268 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001269 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001270
1271 wl_list_for_each(button, &frame->buttons_list, link)
1272 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001273 }
1274
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275 widget_set_allocation(child, allocation.x, allocation.y,
1276 allocation.width, allocation.height);
1277
1278 if (child->resize_handler)
1279 child->resize_handler(child,
1280 allocation.width,
1281 allocation.height,
1282 child->user_data);
1283
Scott Moreauf7e498c2012-05-14 11:39:29 -06001284 width = child->allocation.width + decoration_width;
1285 height = child->allocation.height + decoration_height;
1286
1287 widget->window->input_region =
1288 wl_compositor_create_region(display->compositor);
1289 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001290 t->margin, t->margin,
1291 width - 2 * t->margin,
1292 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001293
1294 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001295
1296 if (child->opaque) {
1297 widget->window->opaque_region =
1298 wl_compositor_create_region(display->compositor);
1299 wl_region_add(widget->window->opaque_region,
1300 opaque_margin, opaque_margin,
1301 widget->allocation.width - 2 * opaque_margin,
1302 widget->allocation.height - 2 * opaque_margin);
1303 }
Martin Minarik1998b152012-05-10 02:04:35 +02001304
1305 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001306 x_r = frame->widget->allocation.width - t->width - t->margin;
1307 x_l = t->width + t->margin;
1308 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001309 wl_list_for_each(button, &frame->buttons_list, link) {
1310 const int button_padding = 4;
1311 w = cairo_image_surface_get_width(button->icon);
1312 h = cairo_image_surface_get_height(button->icon);
1313
1314 if (button->decoration == FRAME_BUTTON_FANCY)
1315 w += 10;
1316
1317 if (button->align == FRAME_BUTTON_LEFT) {
1318 widget_set_allocation(button->widget,
1319 x_l, y , w + 1, h + 1);
1320 x_l += w;
1321 x_l += button_padding;
1322 } else {
1323 x_r -= w;
1324 widget_set_allocation(button->widget,
1325 x_r, y , w + 1, h + 1);
1326 x_r -= button_padding;
1327 }
1328 }
1329}
1330
1331static int
1332frame_button_enter_handler(struct widget *widget,
1333 struct input *input, float x, float y, void *data)
1334{
1335 struct frame_button *frame_button = data;
1336
1337 widget_schedule_redraw(frame_button->widget);
1338 frame_button->state = FRAME_BUTTON_OVER;
1339
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001340 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001341}
1342
1343static void
1344frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1345{
1346 struct frame_button *frame_button = data;
1347
1348 widget_schedule_redraw(frame_button->widget);
1349 frame_button->state = FRAME_BUTTON_DEFAULT;
1350}
1351
1352static void
1353frame_button_button_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001355 uint32_t button,
1356 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001357{
1358 struct frame_button *frame_button = data;
1359 struct window *window = widget->window;
1360
1361 if (button != BTN_LEFT)
1362 return;
1363
1364 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001365 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001366 frame_button->state = FRAME_BUTTON_ACTIVE;
1367 widget_schedule_redraw(frame_button->widget);
1368
1369 if (frame_button->type == FRAME_BUTTON_ICON)
1370 window_show_frame_menu(window, input, time);
1371 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001372 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001373 frame_button->state = FRAME_BUTTON_DEFAULT;
1374 widget_schedule_redraw(frame_button->widget);
1375 break;
1376 }
1377
1378 switch (frame_button->type) {
1379 case FRAME_BUTTON_CLOSE:
1380 if (window->close_handler)
1381 window->close_handler(window->parent,
1382 window->user_data);
1383 else
1384 display_exit(window->display);
1385 break;
1386 case FRAME_BUTTON_MINIMIZE:
1387 fprintf(stderr,"Minimize stub\n");
1388 break;
1389 case FRAME_BUTTON_MAXIMIZE:
1390 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1391 break;
1392 default:
1393 /* Unknown operation */
1394 break;
1395 }
1396}
1397
1398static void
1399frame_button_redraw_handler(struct widget *widget, void *data)
1400{
1401 struct frame_button *frame_button = data;
1402 cairo_t *cr;
1403 int width, height, x, y;
1404 struct window *window = widget->window;
1405
1406 x = widget->allocation.x;
1407 y = widget->allocation.y;
1408 width = widget->allocation.width;
1409 height = widget->allocation.height;
1410
1411 if (!width)
1412 return;
1413 if (!height)
1414 return;
1415 if (widget->opaque)
1416 return;
1417
1418 cr = cairo_create(window->cairo_surface);
1419
1420 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1421 cairo_set_line_width(cr, 1);
1422
1423 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1424 cairo_rectangle (cr, x, y, 25, 16);
1425
1426 cairo_stroke_preserve(cr);
1427
1428 switch (frame_button->state) {
1429 case FRAME_BUTTON_DEFAULT:
1430 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1431 break;
1432 case FRAME_BUTTON_OVER:
1433 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1434 break;
1435 case FRAME_BUTTON_ACTIVE:
1436 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1437 break;
1438 }
1439
1440 cairo_fill (cr);
1441
1442 x += 4;
1443 }
1444
1445 cairo_set_source_surface(cr, frame_button->icon, x, y);
1446 cairo_paint(cr);
1447
1448 cairo_destroy(cr);
1449}
1450
1451static struct widget *
1452frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1453 enum frame_button_align align, enum frame_button_decoration style)
1454{
1455 struct frame_button *frame_button;
1456 const char *icon = data;
1457
1458 frame_button = malloc (sizeof *frame_button);
1459 memset(frame_button, 0, sizeof *frame_button);
1460
1461 frame_button->icon = cairo_image_surface_create_from_png(icon);
1462 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1463 frame_button->frame = frame;
1464 frame_button->type = type;
1465 frame_button->align = align;
1466 frame_button->decoration = style;
1467
1468 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1469
1470 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1471 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1472 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1473 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1474 return frame_button->widget;
1475}
1476
1477static void
1478frame_button_destroy(struct frame_button *frame_button)
1479{
1480 widget_destroy(frame_button->widget);
1481 wl_list_remove(&frame_button->link);
1482 cairo_surface_destroy(frame_button->icon);
1483 free(frame_button);
1484
1485 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001486}
1487
1488static void
1489frame_redraw_handler(struct widget *widget, void *data)
1490{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001492 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001493 struct theme *t = window->display->theme;
1494 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001496 if (window->type == TYPE_FULLSCREEN)
1497 return;
1498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001499 cr = cairo_create(window->cairo_surface);
1500
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001501 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001502 flags |= THEME_FRAME_ACTIVE;
1503 theme_render_frame(t, cr, widget->allocation.width,
1504 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001505
1506 cairo_destroy(cr);
1507}
1508
1509static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001510frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001511{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001512 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001513 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001514
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001515 location = theme_get_location(t, input->sx, input->sy,
1516 frame->widget->allocation.width,
1517 frame->widget->allocation.height);
1518
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001519 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001520 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001521 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001522 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001523 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001524 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001525 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001526 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001527 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001528 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001529 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001530 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001531 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001532 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001533 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001534 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001535 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001536 case THEME_LOCATION_EXTERIOR:
1537 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001539 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001540 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541}
1542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001543static void
1544frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001546 switch (index) {
1547 case 0: /* close */
1548 if (window->close_handler)
1549 window->close_handler(window->parent,
1550 window->user_data);
1551 else
1552 display_exit(window->display);
1553 break;
1554 case 1: /* fullscreen */
1555 /* we don't have a way to get out of fullscreen for now */
1556 window_set_fullscreen(window, 1);
1557 break;
1558 case 2: /* rotate */
1559 case 3: /* scale */
1560 break;
1561 }
1562}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001563
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001564void
1565window_show_frame_menu(struct window *window,
1566 struct input *input, uint32_t time)
1567{
1568 int32_t x, y;
1569
1570 static const char *entries[] = {
1571 "Close", "Fullscreen", "Rotate", "Scale"
1572 };
1573
1574 input_get_position(input, &x, &y);
1575 window_show_menu(window->display, input, time, window,
1576 x - 10, y - 10, frame_menu_func, entries, 4);
1577}
1578
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001579static int
1580frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001581 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001582{
1583 return frame_get_pointer_image_for_location(data, input);
1584}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001585
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001586static int
1587frame_motion_handler(struct widget *widget,
1588 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001589 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001590{
1591 return frame_get_pointer_image_for_location(data, input);
1592}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001593
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001594static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595frame_button_handler(struct widget *widget,
1596 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001597 uint32_t button, enum wl_pointer_button_state state,
1598 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001599
1600{
1601 struct frame *frame = data;
1602 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001603 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001605
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001606 location = theme_get_location(display->theme, input->sx, input->sy,
1607 frame->widget->allocation.width,
1608 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609
Daniel Stone4dbadb12012-05-30 16:31:51 +01001610 if (window->display->shell && button == BTN_LEFT &&
1611 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001613 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001614 if (!window->shell_surface)
1615 break;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001616 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001617 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001618 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001619 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001620 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001621 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001622 case THEME_LOCATION_RESIZING_TOP:
1623 case THEME_LOCATION_RESIZING_BOTTOM:
1624 case THEME_LOCATION_RESIZING_LEFT:
1625 case THEME_LOCATION_RESIZING_RIGHT:
1626 case THEME_LOCATION_RESIZING_TOP_LEFT:
1627 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1628 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1629 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001630 if (!window->shell_surface)
1631 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001632 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001633
1634 if (!display->dpy) {
1635 /* If we're using shm, allocate a big
1636 pool to create buffers out of while
1637 we resize. We should probably base
1638 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001639 window->pool =
1640 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001641 }
1642
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001643 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001644 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001645 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001646 break;
1647 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001648 } else if (button == BTN_RIGHT &&
1649 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001650 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001651 }
1652}
1653
1654struct widget *
1655frame_create(struct window *window, void *data)
1656{
1657 struct frame *frame;
1658
1659 frame = malloc(sizeof *frame);
1660 memset(frame, 0, sizeof *frame);
1661
1662 frame->widget = window_add_widget(window, frame);
1663 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001664
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001665 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1666 widget_set_resize_handler(frame->widget, frame_resize_handler);
1667 widget_set_enter_handler(frame->widget, frame_enter_handler);
1668 widget_set_motion_handler(frame->widget, frame_motion_handler);
1669 widget_set_button_handler(frame->widget, frame_button_handler);
1670
Martin Minarik1998b152012-05-10 02:04:35 +02001671 /* Create empty list for frame buttons */
1672 wl_list_init(&frame->buttons_list);
1673
1674 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1675 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1676
1677 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1678 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1679
1680 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1681 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1682
1683 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1684 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1685
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001686 window->frame = frame;
1687
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001688 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001689}
1690
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001691static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001692frame_destroy(struct frame *frame)
1693{
Martin Minarik1998b152012-05-10 02:04:35 +02001694 struct frame_button *button, *tmp;
1695
1696 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1697 frame_button_destroy(button);
1698
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001699 /* frame->child must be destroyed by the application */
1700 widget_destroy(frame->widget);
1701 free(frame);
1702}
1703
1704static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001705input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001706 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001708 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001709 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001710
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001711 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001712 return;
1713
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001714 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001715 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001716 widget = old;
1717 if (input->grab)
1718 widget = input->grab;
1719 if (widget->leave_handler)
1720 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001721 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001722 }
1723
1724 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 widget = focus;
1726 if (input->grab)
1727 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04001728 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001729 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001730 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001731 widget->user_data);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001732
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001733 input_set_pointer_image(input, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001734 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735}
1736
1737static void
Daniel Stone37816df2012-05-16 18:45:18 +01001738pointer_handle_motion(void *data, struct wl_pointer *pointer,
1739 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001740{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001741 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001742 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001743 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001744 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001745 float sx = wl_fixed_to_double(sx_w);
1746 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001747
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001748 input->sx = sx;
1749 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001750
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001751 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001752 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001753 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754 }
1755
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001756 if (input->grab)
1757 widget = input->grab;
1758 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001759 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001760 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001761 cursor = widget->motion_handler(input->focus_widget,
1762 input, time, sx, sy,
1763 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001764
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04001765 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001766}
1767
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001768void
1769input_grab(struct input *input, struct widget *widget, uint32_t button)
1770{
1771 input->grab = widget;
1772 input->grab_button = button;
1773}
1774
1775void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001776input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001777{
1778 struct widget *widget;
1779
1780 input->grab = NULL;
1781 if (input->pointer_focus) {
1782 widget = widget_find_widget(input->pointer_focus->widget,
1783 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001784 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001785 }
1786}
1787
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001788static void
Daniel Stone37816df2012-05-16 18:45:18 +01001789pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001790 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001791{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001792 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001793 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001794 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001795
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001796 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001797 if (input->focus_widget && input->grab == NULL &&
1798 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001799 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001800
Neil Roberts6b28aad2012-01-23 19:11:18 +00001801 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001802 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001803 (*widget->button_handler)(widget,
1804 input, time,
1805 button, state,
1806 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001807
Daniel Stone4dbadb12012-05-30 16:31:51 +01001808 if (input->grab && input->grab_button == button &&
1809 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001810 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001811}
1812
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001813static void
Daniel Stone37816df2012-05-16 18:45:18 +01001814pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001815 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001816{
1817}
1818
1819static void
Daniel Stone37816df2012-05-16 18:45:18 +01001820keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001821 uint32_t serial, uint32_t time, uint32_t key,
1822 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001823{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001824 struct input *input = data;
1825 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001826 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001827 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001828 const xkb_keysym_t *syms;
1829 xkb_keysym_t sym;
1830 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001832 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001833 code = key + 8;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001834 if (!window || window->keyboard_device != input || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001835 return;
1836
Daniel Stone97f68542012-05-30 16:32:01 +01001837 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001838
Daniel Stone97f68542012-05-30 16:32:01 +01001839 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001840 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001841 XKB_STATE_LATCHED);
1842 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001843 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001844 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001845 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001846 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001847 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001848 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001849
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001850 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001851 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001852 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001853 window_set_maximized(window,
1854 window->type != TYPE_MAXIMIZED);
1855 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856 if (num_syms == 1)
1857 sym = syms[0];
1858 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001859 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001860
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001861 (*window->key_handler)(window, input, time, key,
1862 sym, state, window->user_data);
1863 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001864}
1865
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001866static void
Daniel Stone351eb612012-05-31 15:27:47 -04001867keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1868 uint32_t serial, uint32_t mods_depressed,
1869 uint32_t mods_latched, uint32_t mods_locked,
1870 uint32_t group)
1871{
1872 struct input *input = data;
1873
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001874 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1875 mods_locked, 0, 0, group);
Daniel Stone351eb612012-05-31 15:27:47 -04001876}
1877
1878static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001879input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001880{
1881 struct window *window = input->pointer_focus;
1882
1883 if (!window)
1884 return;
1885
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001886 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001887
Pekka Paalanene1207c72011-12-16 12:02:09 +02001888 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001889 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001890}
1891
1892static void
Daniel Stone37816df2012-05-16 18:45:18 +01001893pointer_handle_enter(void *data, struct wl_pointer *pointer,
1894 uint32_t serial, struct wl_surface *surface,
1895 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001896{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001897 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001898 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001899 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001900 float sx = wl_fixed_to_double(sx_w);
1901 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001902
Martin Minarik1998b152012-05-10 02:04:35 +02001903 if (!surface) {
1904 /* enter event for a window we've just destroyed */
1905 return;
1906 }
1907
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001908 input->display->serial = serial;
1909 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001910 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001911 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001912
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001913 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001914 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001915 window->pool = NULL;
1916 /* Schedule a redraw to free the pool */
1917 window_schedule_redraw(window);
1918 }
1919
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001920 input->sx = sx;
1921 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001922
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001923 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001924 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001925}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001926
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001927static void
Daniel Stone37816df2012-05-16 18:45:18 +01001928pointer_handle_leave(void *data, struct wl_pointer *pointer,
1929 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001930{
1931 struct input *input = data;
1932
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001933 input->display->serial = serial;
1934 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001935}
1936
1937static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001938input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001939{
1940 struct window *window = input->keyboard_focus;
1941
1942 if (!window)
1943 return;
1944
1945 window->keyboard_device = NULL;
1946 if (window->keyboard_focus_handler)
1947 (*window->keyboard_focus_handler)(window, NULL,
1948 window->user_data);
1949
1950 input->keyboard_focus = NULL;
1951}
1952
1953static void
Daniel Stone37816df2012-05-16 18:45:18 +01001954keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1955 uint32_t serial, struct wl_surface *surface,
1956 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001957{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001958 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001959 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001960
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001962 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001963
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001964 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001965 window->keyboard_device = input;
1966 if (window->keyboard_focus_handler)
1967 (*window->keyboard_focus_handler)(window,
1968 window->keyboard_device,
1969 window->user_data);
1970}
1971
1972static void
Daniel Stone37816df2012-05-16 18:45:18 +01001973keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1974 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001975{
1976 struct input *input = data;
1977
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001978 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001979 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001980}
1981
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001982static void
1983keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1984 uint32_t format, int fd, uint32_t size)
1985{
1986 struct input *input = data;
1987 char *map_str;
1988
1989 if (!data) {
1990 close(fd);
1991 return;
1992 }
1993
1994 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1995 close(fd);
1996 return;
1997 }
1998
1999 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2000 if (map_str == MAP_FAILED) {
2001 close(fd);
2002 return;
2003 }
2004
2005 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2006 map_str,
2007 XKB_KEYMAP_FORMAT_TEXT_V1,
2008 0);
2009 munmap(map_str, size);
2010 close(fd);
2011
2012 if (!input->xkb.keymap) {
2013 fprintf(stderr, "failed to compile keymap\n");
2014 return;
2015 }
2016
2017 input->xkb.state = xkb_state_new(input->xkb.keymap);
2018 if (!input->xkb.state) {
2019 fprintf(stderr, "failed to create XKB state\n");
2020 xkb_map_unref(input->xkb.keymap);
2021 input->xkb.keymap = NULL;
2022 return;
2023 }
2024
2025 input->xkb.control_mask =
2026 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2027 input->xkb.alt_mask =
2028 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2029 input->xkb.shift_mask =
2030 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2031}
2032
Daniel Stone37816df2012-05-16 18:45:18 +01002033static const struct wl_pointer_listener pointer_listener = {
2034 pointer_handle_enter,
2035 pointer_handle_leave,
2036 pointer_handle_motion,
2037 pointer_handle_button,
2038 pointer_handle_axis,
2039};
2040
2041static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002042 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002043 keyboard_handle_enter,
2044 keyboard_handle_leave,
2045 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002046 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002047};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002048
2049static void
Daniel Stone37816df2012-05-16 18:45:18 +01002050seat_handle_capabilities(void *data, struct wl_seat *seat,
2051 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002052{
Daniel Stone37816df2012-05-16 18:45:18 +01002053 struct input *input = data;
2054
2055 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2056 input->pointer = wl_seat_get_pointer(seat);
2057 wl_pointer_set_user_data(input->pointer, input);
2058 wl_pointer_add_listener(input->pointer, &pointer_listener,
2059 input);
2060 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2061 wl_pointer_destroy(input->pointer);
2062 input->pointer = NULL;
2063 }
2064
2065 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2066 input->keyboard = wl_seat_get_keyboard(seat);
2067 wl_keyboard_set_user_data(input->keyboard, input);
2068 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2069 input);
2070 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2071 wl_keyboard_destroy(input->keyboard);
2072 input->keyboard = NULL;
2073 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002074}
2075
Daniel Stone37816df2012-05-16 18:45:18 +01002076static const struct wl_seat_listener seat_listener = {
2077 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002078};
2079
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002080void
2081input_get_position(struct input *input, int32_t *x, int32_t *y)
2082{
2083 *x = input->sx;
2084 *y = input->sy;
2085}
2086
Daniel Stone37816df2012-05-16 18:45:18 +01002087struct wl_seat *
2088input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002089{
Daniel Stone37816df2012-05-16 18:45:18 +01002090 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002091}
2092
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002093uint32_t
2094input_get_modifiers(struct input *input)
2095{
2096 return input->modifiers;
2097}
2098
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002099struct widget *
2100input_get_focus_widget(struct input *input)
2101{
2102 return input->focus_widget;
2103}
2104
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002105struct data_offer {
2106 struct wl_data_offer *offer;
2107 struct input *input;
2108 struct wl_array types;
2109 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002110
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002111 struct task io_task;
2112 int fd;
2113 data_func_t func;
2114 int32_t x, y;
2115 void *user_data;
2116};
2117
2118static void
2119data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2120{
2121 struct data_offer *offer = data;
2122 char **p;
2123
2124 p = wl_array_add(&offer->types, sizeof *p);
2125 *p = strdup(type);
2126}
2127
2128static const struct wl_data_offer_listener data_offer_listener = {
2129 data_offer_offer,
2130};
2131
2132static void
2133data_offer_destroy(struct data_offer *offer)
2134{
2135 char **p;
2136
2137 offer->refcount--;
2138 if (offer->refcount == 0) {
2139 wl_data_offer_destroy(offer->offer);
2140 for (p = offer->types.data; *p; p++)
2141 free(*p);
2142 wl_array_release(&offer->types);
2143 free(offer);
2144 }
2145}
2146
2147static void
2148data_device_data_offer(void *data,
2149 struct wl_data_device *data_device, uint32_t id)
2150{
2151 struct data_offer *offer;
2152
2153 offer = malloc(sizeof *offer);
2154
2155 wl_array_init(&offer->types);
2156 offer->refcount = 1;
2157 offer->input = data;
2158
2159 /* FIXME: Generate typesafe wrappers for this */
2160 offer->offer = (struct wl_data_offer *)
2161 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2162 id, &wl_data_offer_interface);
2163
2164 wl_data_offer_add_listener(offer->offer,
2165 &data_offer_listener, offer);
2166}
2167
2168static void
2169data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002170 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002171 wl_fixed_t x_w, wl_fixed_t y_w,
2172 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002173{
2174 struct input *input = data;
2175 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002176 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002177 float x = wl_fixed_to_double(x_w);
2178 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002179 char **p;
2180
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002181 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002182 window = wl_surface_get_user_data(surface);
2183 input->pointer_focus = window;
2184
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002185 if (offer) {
2186 input->drag_offer = wl_data_offer_get_user_data(offer);
2187
2188 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2189 *p = NULL;
2190
2191 types_data = input->drag_offer->types.data;
2192 } else {
2193 input->drag_offer = NULL;
2194 types_data = NULL;
2195 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196
2197 window = input->pointer_focus;
2198 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002199 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002200 window->user_data);
2201}
2202
2203static void
2204data_device_leave(void *data, struct wl_data_device *data_device)
2205{
2206 struct input *input = data;
2207
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002208 if (input->drag_offer) {
2209 data_offer_destroy(input->drag_offer);
2210 input->drag_offer = NULL;
2211 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002212}
2213
2214static void
2215data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002216 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002217{
2218 struct input *input = data;
2219 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002220 float x = wl_fixed_to_double(x_w);
2221 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002222 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002223
2224 input->sx = x;
2225 input->sy = y;
2226
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002227 if (input->drag_offer)
2228 types_data = input->drag_offer->types.data;
2229 else
2230 types_data = NULL;
2231
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002232 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002233 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002234 window->user_data);
2235}
2236
2237static void
2238data_device_drop(void *data, struct wl_data_device *data_device)
2239{
2240 struct input *input = data;
2241 struct window *window = input->pointer_focus;
2242
2243 if (window->drop_handler)
2244 window->drop_handler(window, input,
2245 input->sx, input->sy, window->user_data);
2246}
2247
2248static void
2249data_device_selection(void *data,
2250 struct wl_data_device *wl_data_device,
2251 struct wl_data_offer *offer)
2252{
2253 struct input *input = data;
2254 char **p;
2255
2256 if (input->selection_offer)
2257 data_offer_destroy(input->selection_offer);
2258
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002259 if (offer) {
2260 input->selection_offer = wl_data_offer_get_user_data(offer);
2261 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2262 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002263 } else {
2264 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002265 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002266}
2267
2268static const struct wl_data_device_listener data_device_listener = {
2269 data_device_data_offer,
2270 data_device_enter,
2271 data_device_leave,
2272 data_device_motion,
2273 data_device_drop,
2274 data_device_selection
2275};
2276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002277void
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002278input_set_pointer_image(struct input *input, int pointer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002279{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002280 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002281 struct wl_cursor *cursor;
2282 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002284 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002285 return;
2286
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002287 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002288 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002289 return;
2290
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002291 image = cursor->images[0];
2292 buffer = wl_cursor_image_get_buffer(image);
2293 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002294 return;
2295
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002296 input->current_cursor = pointer;
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002297 wl_pointer_attach(input->pointer, input->pointer_enter_serial,
2298 buffer, image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002299}
2300
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002301struct wl_data_device *
2302input_get_data_device(struct input *input)
2303{
2304 return input->data_device;
2305}
2306
2307void
2308input_set_selection(struct input *input,
2309 struct wl_data_source *source, uint32_t time)
2310{
2311 wl_data_device_set_selection(input->data_device, source, time);
2312}
2313
2314void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002315input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002316{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002317 wl_data_offer_accept(input->drag_offer->offer,
2318 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002319}
2320
2321static void
2322offer_io_func(struct task *task, uint32_t events)
2323{
2324 struct data_offer *offer =
2325 container_of(task, struct data_offer, io_task);
2326 unsigned int len;
2327 char buffer[4096];
2328
2329 len = read(offer->fd, buffer, sizeof buffer);
2330 offer->func(buffer, len,
2331 offer->x, offer->y, offer->user_data);
2332
2333 if (len == 0) {
2334 close(offer->fd);
2335 data_offer_destroy(offer);
2336 }
2337}
2338
2339static void
2340data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2341 data_func_t func, void *user_data)
2342{
2343 int p[2];
2344
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002345 if (pipe2(p, O_CLOEXEC) == -1)
2346 return;
2347
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002348 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2349 close(p[1]);
2350
2351 offer->io_task.run = offer_io_func;
2352 offer->fd = p[0];
2353 offer->func = func;
2354 offer->refcount++;
2355 offer->user_data = user_data;
2356
2357 display_watch_fd(offer->input->display,
2358 offer->fd, EPOLLIN, &offer->io_task);
2359}
2360
2361void
2362input_receive_drag_data(struct input *input, const char *mime_type,
2363 data_func_t func, void *data)
2364{
2365 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2366 input->drag_offer->x = input->sx;
2367 input->drag_offer->y = input->sy;
2368}
2369
2370int
2371input_receive_selection_data(struct input *input, const char *mime_type,
2372 data_func_t func, void *data)
2373{
2374 char **p;
2375
2376 if (input->selection_offer == NULL)
2377 return -1;
2378
2379 for (p = input->selection_offer->types.data; *p; p++)
2380 if (strcmp(mime_type, *p) == 0)
2381 break;
2382
2383 if (*p == NULL)
2384 return -1;
2385
2386 data_offer_receive_data(input->selection_offer,
2387 mime_type, func, data);
2388 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002389}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002390
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002391int
2392input_receive_selection_data_to_fd(struct input *input,
2393 const char *mime_type, int fd)
2394{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04002395 if (input->selection_offer)
2396 wl_data_offer_receive(input->selection_offer->offer,
2397 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002398
2399 return 0;
2400}
2401
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002402void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002403window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002404{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002405 if (!window->shell_surface)
2406 return;
2407
Daniel Stone37816df2012-05-16 18:45:18 +01002408 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002409}
2410
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002411static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002412idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002413{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002414 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002415
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002416 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002417 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002418 widget_set_allocation(widget,
2419 window->pending_allocation.x,
2420 window->pending_allocation.y,
2421 window->pending_allocation.width,
2422 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002423
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002424 if (window->input_region) {
2425 wl_region_destroy(window->input_region);
2426 window->input_region = NULL;
2427 }
2428
2429 if (window->opaque_region) {
2430 wl_region_destroy(window->opaque_region);
2431 window->opaque_region = NULL;
2432 }
2433
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002434 if (widget->resize_handler)
2435 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002436 widget->allocation.width,
2437 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002438 widget->user_data);
2439
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002440 if (window->allocation.width != widget->allocation.width ||
2441 window->allocation.height != widget->allocation.height) {
2442 window->allocation = widget->allocation;
2443 window_schedule_redraw(window);
2444 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002445}
2446
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002447void
2448window_schedule_resize(struct window *window, int width, int height)
2449{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002450 window->pending_allocation.x = 0;
2451 window->pending_allocation.y = 0;
2452 window->pending_allocation.width = width;
2453 window->pending_allocation.height = height;
2454
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002455 window->resize_needed = 1;
2456 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002457}
2458
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002459void
2460widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2461{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002462 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002463}
2464
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002465static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002466handle_ping(void *data, struct wl_shell_surface *shell_surface,
2467 uint32_t serial)
2468{
2469 wl_shell_surface_pong(shell_surface, serial);
2470}
2471
2472static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002473handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002474 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002475{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002476 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002477
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002478 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002479 return;
2480
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002481 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002482 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002483}
2484
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002485static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002486menu_destroy(struct menu *menu)
2487{
2488 widget_destroy(menu->widget);
2489 window_destroy(menu->window);
2490 free(menu);
2491}
2492
2493static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002494handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2495{
2496 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002497 struct menu *menu = window->widget->user_data;
2498
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002499 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002500 * device. Or just use wl_callback. And this really needs to
2501 * be a window vfunc that the menu can set. And we need the
2502 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002503
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002504 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002505 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002506 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002507}
2508
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002509static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002510 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002511 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002512 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002513};
2514
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002515void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002516window_get_allocation(struct window *window,
2517 struct rectangle *allocation)
2518{
2519 *allocation = window->allocation;
2520}
2521
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002522static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002523widget_redraw(struct widget *widget)
2524{
2525 struct widget *child;
2526
2527 if (widget->redraw_handler)
2528 widget->redraw_handler(widget, widget->user_data);
2529 wl_list_for_each(child, &widget->child_list, link)
2530 widget_redraw(child);
2531}
2532
2533static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002534frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2535{
2536 struct window *window = data;
2537
2538 wl_callback_destroy(callback);
2539 window->redraw_scheduled = 0;
2540 if (window->redraw_needed)
2541 window_schedule_redraw(window);
2542}
2543
2544static const struct wl_callback_listener listener = {
2545 frame_callback
2546};
2547
2548static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002549idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002550{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002551 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002552 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002553
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002554 if (window->resize_needed)
2555 idle_resize(window);
2556
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002557 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002558 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002559 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002560 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002561 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002562
2563 callback = wl_surface_frame(window->surface);
2564 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002565}
2566
2567void
2568window_schedule_redraw(struct window *window)
2569{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002570 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002571 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002572 window->redraw_task.run = idle_redraw;
2573 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002574 window->redraw_scheduled = 1;
2575 }
2576}
2577
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002578void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002579window_set_custom(struct window *window)
2580{
2581 window->type = TYPE_CUSTOM;
2582}
2583
2584void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002585window_set_fullscreen(struct window *window, int fullscreen)
2586{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002587 if (!window->display->shell)
2588 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002589
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002590 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002591 return;
2592
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002593 if (fullscreen) {
2594 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002595 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002596 wl_shell_surface_set_fullscreen(window->shell_surface,
2597 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2598 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002599 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002600 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002601 wl_shell_surface_set_toplevel(window->shell_surface);
2602 window_schedule_resize(window,
2603 window->saved_allocation.width,
2604 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002605 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002606}
2607
2608void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002609window_set_maximized(struct window *window, int maximized)
2610{
2611 if (!window->display->shell)
2612 return;
2613
2614 if ((window->type == TYPE_MAXIMIZED) == maximized)
2615 return;
2616
2617 if (window->type == TYPE_TOPLEVEL) {
2618 window->saved_allocation = window->allocation;
2619 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2620 window->type = TYPE_MAXIMIZED;
2621 } else {
2622 wl_shell_surface_set_toplevel(window->shell_surface);
2623 window->type = TYPE_TOPLEVEL;
2624 window_schedule_resize(window,
2625 window->saved_allocation.width,
2626 window->saved_allocation.height);
2627 }
2628}
2629
2630void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002631window_set_user_data(struct window *window, void *data)
2632{
2633 window->user_data = data;
2634}
2635
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002636void *
2637window_get_user_data(struct window *window)
2638{
2639 return window->user_data;
2640}
2641
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002642void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002643window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002644 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002645{
2646 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002647}
2648
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002649void
2650window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002651 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002652{
2653 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002654}
2655
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002656void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002657window_set_data_handler(struct window *window, window_data_handler_t handler)
2658{
2659 window->data_handler = handler;
2660}
2661
2662void
2663window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2664{
2665 window->drop_handler = handler;
2666}
2667
2668void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002669window_set_close_handler(struct window *window,
2670 window_close_handler_t handler)
2671{
2672 window->close_handler = handler;
2673}
2674
2675void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002676window_set_title(struct window *window, const char *title)
2677{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002678 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002679 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002680 if (window->shell_surface)
2681 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002682}
2683
2684const char *
2685window_get_title(struct window *window)
2686{
2687 return window->title;
2688}
2689
2690void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002691window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2692{
2693 struct text_cursor_position *text_cursor_position =
2694 window->display->text_cursor_position;
2695
Scott Moreau9295ce02012-06-01 12:46:10 -06002696 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002697 return;
2698
2699 text_cursor_position_notify(text_cursor_position,
Scott Moreauae712202012-06-01 12:46:09 -06002700 window->surface,
2701 wl_fixed_from_int(x),
2702 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002703}
2704
2705void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002706window_damage(struct window *window, int32_t x, int32_t y,
2707 int32_t width, int32_t height)
2708{
2709 wl_surface_damage(window->surface, x, y, width, height);
2710}
2711
Casey Dahlin9074db52012-04-19 22:50:09 -04002712static void
2713surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002714 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002715{
Rob Bradford7507b572012-05-15 17:55:34 +01002716 struct window *window = data;
2717 struct output *output;
2718 struct output *output_found = NULL;
2719 struct window_output *window_output;
2720
2721 wl_list_for_each(output, &window->display->output_list, link) {
2722 if (output->output == wl_output) {
2723 output_found = output;
2724 break;
2725 }
2726 }
2727
2728 if (!output_found)
2729 return;
2730
2731 window_output = malloc (sizeof *window_output);
2732 window_output->output = output_found;
2733
2734 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002735}
2736
2737static void
2738surface_leave(void *data,
2739 struct wl_surface *wl_surface, struct wl_output *output)
2740{
Rob Bradford7507b572012-05-15 17:55:34 +01002741 struct window *window = data;
2742 struct window_output *window_output;
2743 struct window_output *window_output_found = NULL;
2744
2745 wl_list_for_each(window_output, &window->window_output_list, link) {
2746 if (window_output->output->output == output) {
2747 window_output_found = window_output;
2748 break;
2749 }
2750 }
2751
2752 if (window_output_found) {
2753 wl_list_remove(&window_output_found->link);
2754 free(window_output_found);
2755 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002756}
2757
2758static const struct wl_surface_listener surface_listener = {
2759 surface_enter,
2760 surface_leave
2761};
2762
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002763static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002764window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002765{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002766 struct window *window;
2767
2768 window = malloc(sizeof *window);
2769 if (window == NULL)
2770 return NULL;
2771
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002772 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002773 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002774 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002775 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002776 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002777 if (display->shell) {
2778 window->shell_surface =
2779 wl_shell_get_shell_surface(display->shell,
2780 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002781 if (window->title)
2782 wl_shell_surface_set_title(window->shell_surface,
2783 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002784 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002785 window->allocation.x = 0;
2786 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002787 window->allocation.width = 0;
2788 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002789 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002790 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002791 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002792 window->input_region = NULL;
2793 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002794
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002795 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002796#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002797 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002798#else
2799 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2800#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002801 else
2802 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002803
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002804 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002805 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002806 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002807
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002808 if (window->shell_surface) {
2809 wl_shell_surface_set_user_data(window->shell_surface, window);
2810 wl_shell_surface_add_listener(window->shell_surface,
2811 &shell_surface_listener, window);
2812 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002813
Rob Bradford7507b572012-05-15 17:55:34 +01002814 wl_list_init (&window->window_output_list);
2815
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002816 return window;
2817}
2818
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002819struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002820window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002821{
2822 struct window *window;
2823
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002824 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002825 if (!window)
2826 return NULL;
2827
2828 return window;
2829}
2830
2831struct window *
2832window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002833 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002834{
2835 struct window *window;
2836
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002837 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002838 if (!window)
2839 return NULL;
2840
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002841 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002842 window->x = x;
2843 window->y = y;
2844
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002845 if (display->shell)
2846 wl_shell_surface_set_transient(window->shell_surface,
2847 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002848 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002849
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002850 return window;
2851}
2852
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002853static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002854menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002855{
2856 int next;
2857
2858 next = (sy - 8) / 20;
2859 if (menu->current != next) {
2860 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002861 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002862 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002863}
2864
2865static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002866menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002867 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002868 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002869{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002870 struct menu *menu = data;
2871
2872 if (widget == menu->widget)
2873 menu_set_item(data, y);
2874
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002875 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002876}
2877
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002878static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002879menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002880 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002881{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002882 struct menu *menu = data;
2883
2884 if (widget == menu->widget)
2885 menu_set_item(data, y);
2886
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002887 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002888}
2889
2890static void
2891menu_leave_handler(struct widget *widget, struct input *input, void *data)
2892{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002893 struct menu *menu = data;
2894
2895 if (widget == menu->widget)
2896 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002897}
2898
2899static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002900menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002901 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002902 uint32_t button, enum wl_pointer_button_state state,
2903 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002904
2905{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002906 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002907
Daniel Stone4dbadb12012-05-30 16:31:51 +01002908 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2909 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002910 /* Either relase after press-drag-release or
2911 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002912 menu->func(menu->window->parent,
2913 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002914 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002915 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002916 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002917}
2918
2919static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002920menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002921{
2922 cairo_t *cr;
2923 const int32_t r = 3, margin = 3;
2924 struct menu *menu = data;
2925 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002926 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002927
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002928 cr = cairo_create(window->cairo_surface);
2929 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2930 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2931 cairo_paint(cr);
2932
2933 width = window->allocation.width;
2934 height = window->allocation.height;
2935 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002936
2937 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002938 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2939 cairo_fill(cr);
2940
2941 for (i = 0; i < menu->count; i++) {
2942 if (i == menu->current) {
2943 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2944 cairo_rectangle(cr, margin, i * 20 + margin,
2945 width - 2 * margin, 20);
2946 cairo_fill(cr);
2947 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2948 cairo_move_to(cr, 10, i * 20 + 16);
2949 cairo_show_text(cr, menu->entries[i]);
2950 } else {
2951 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2952 cairo_move_to(cr, 10, i * 20 + 16);
2953 cairo_show_text(cr, menu->entries[i]);
2954 }
2955 }
2956
2957 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002958}
2959
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002960void
2961window_show_menu(struct display *display,
2962 struct input *input, uint32_t time, struct window *parent,
2963 int32_t x, int32_t y,
2964 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002965{
2966 struct window *window;
2967 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002968 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002969
2970 menu = malloc(sizeof *menu);
2971 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002972 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002973
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002974 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002975 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002976 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002977
2978 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002979 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002980 menu->entries = entries;
2981 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002982 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002983 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002984 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002985 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002986 window->type = TYPE_MENU;
2987 window->x = x;
2988 window->y = y;
2989
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002990 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002991 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002992 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993 window->parent->shell_surface,
2994 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002995
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002996 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002997 widget_set_enter_handler(menu->widget, menu_enter_handler);
2998 widget_set_leave_handler(menu->widget, menu_leave_handler);
2999 widget_set_motion_handler(menu->widget, menu_motion_handler);
3000 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05003001
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003002 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003003 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05003004}
3005
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05003006void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04003007window_set_buffer_type(struct window *window, enum window_buffer_type type)
3008{
3009 window->buffer_type = type;
3010}
3011
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04003012
3013static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003014display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003015 struct wl_output *wl_output,
3016 int x, int y,
3017 int physical_width,
3018 int physical_height,
3019 int subpixel,
3020 const char *make,
3021 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003022{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003023 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003024
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003025 output->allocation.x = x;
3026 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003027}
3028
3029static void
3030display_handle_mode(void *data,
3031 struct wl_output *wl_output,
3032 uint32_t flags,
3033 int width,
3034 int height,
3035 int refresh)
3036{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003037 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003038 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003039
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003040 if (flags & WL_OUTPUT_MODE_CURRENT) {
3041 output->allocation.width = width;
3042 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003043 if (display->output_configure_handler)
3044 (*display->output_configure_handler)(
3045 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003046 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003047}
3048
3049static const struct wl_output_listener output_listener = {
3050 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003051 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003052};
3053
3054static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003055display_add_output(struct display *d, uint32_t id)
3056{
3057 struct output *output;
3058
3059 output = malloc(sizeof *output);
3060 if (output == NULL)
3061 return;
3062
3063 memset(output, 0, sizeof *output);
3064 output->display = d;
3065 output->output =
3066 wl_display_bind(d->display, id, &wl_output_interface);
3067 wl_list_insert(d->output_list.prev, &output->link);
3068
3069 wl_output_add_listener(output->output, &output_listener, output);
3070}
3071
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003072static void
3073output_destroy(struct output *output)
3074{
3075 if (output->destroy_handler)
3076 (*output->destroy_handler)(output, output->user_data);
3077
3078 wl_output_destroy(output->output);
3079 wl_list_remove(&output->link);
3080 free(output);
3081}
3082
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003083void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003084display_set_output_configure_handler(struct display *display,
3085 display_output_handler_t handler)
3086{
3087 struct output *output;
3088
3089 display->output_configure_handler = handler;
3090 if (!handler)
3091 return;
3092
3093 wl_list_for_each(output, &display->output_list, link)
3094 (*display->output_configure_handler)(output,
3095 display->user_data);
3096}
3097
3098void
3099output_set_user_data(struct output *output, void *data)
3100{
3101 output->user_data = data;
3102}
3103
3104void *
3105output_get_user_data(struct output *output)
3106{
3107 return output->user_data;
3108}
3109
3110void
3111output_set_destroy_handler(struct output *output,
3112 display_output_handler_t handler)
3113{
3114 output->destroy_handler = handler;
3115 /* FIXME: implement this, once we have way to remove outputs */
3116}
3117
3118void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003119output_get_allocation(struct output *output, struct rectangle *allocation)
3120{
3121 *allocation = output->allocation;
3122}
3123
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003124struct wl_output *
3125output_get_wl_output(struct output *output)
3126{
3127 return output->output;
3128}
3129
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003130static void
Daniel Stone97f68542012-05-30 16:32:01 +01003131fini_xkb(struct input *input)
3132{
3133 xkb_state_unref(input->xkb.state);
3134 xkb_map_unref(input->xkb.keymap);
3135}
3136
3137static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003138display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003139{
3140 struct input *input;
3141
3142 input = malloc(sizeof *input);
3143 if (input == NULL)
3144 return;
3145
3146 memset(input, 0, sizeof *input);
3147 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003148 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003149 input->pointer_focus = NULL;
3150 input->keyboard_focus = NULL;
3151 wl_list_insert(d->input_list.prev, &input->link);
3152
Daniel Stone37816df2012-05-16 18:45:18 +01003153 wl_seat_add_listener(input->seat, &seat_listener, input);
3154 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003155
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003156 input->data_device =
3157 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003158 input->seat);
3159 wl_data_device_add_listener(input->data_device, &data_device_listener,
3160 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003161}
3162
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003163static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003164input_destroy(struct input *input)
3165{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003166 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003167 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003168
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003169 if (input->xkb.state)
3170 xkb_state_unref(input->xkb.state);
3171 if (input->xkb.keymap)
3172 xkb_map_unref(input->xkb.keymap);
3173
Pekka Paalanene1207c72011-12-16 12:02:09 +02003174 if (input->drag_offer)
3175 data_offer_destroy(input->drag_offer);
3176
3177 if (input->selection_offer)
3178 data_offer_destroy(input->selection_offer);
3179
3180 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003181 fini_xkb(input);
3182
Pekka Paalanene1207c72011-12-16 12:02:09 +02003183 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003184 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003185 free(input);
3186}
3187
3188static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003189display_handle_global(struct wl_display *display, uint32_t id,
3190 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003191{
3192 struct display *d = data;
3193
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003194 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003195 d->compositor =
3196 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003197 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003198 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003199 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003200 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003201 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003202 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003203 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003204 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003205 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3206 d->data_device_manager =
3207 wl_display_bind(display, id,
3208 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003209 } else if (strcmp(interface, "text_cursor_position") == 0) {
3210 d->text_cursor_position =
3211 wl_display_bind(display, id,
3212 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003213 }
3214}
3215
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003216#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003217static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003218init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003219{
3220 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003221 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003222
Rob Clark6396ed32012-03-11 19:48:41 -05003223#ifdef USE_CAIRO_GLESV2
3224# define GL_BIT EGL_OPENGL_ES2_BIT
3225#else
3226# define GL_BIT EGL_OPENGL_BIT
3227#endif
3228
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003229 static const EGLint argb_cfg_attribs[] = {
3230 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003231 EGL_RED_SIZE, 1,
3232 EGL_GREEN_SIZE, 1,
3233 EGL_BLUE_SIZE, 1,
3234 EGL_ALPHA_SIZE, 1,
3235 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003236 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003237 EGL_NONE
3238 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003239
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003240#ifdef USE_CAIRO_GLESV2
3241 static const EGLint context_attribs[] = {
3242 EGL_CONTEXT_CLIENT_VERSION, 2,
3243 EGL_NONE
3244 };
3245 EGLint api = EGL_OPENGL_ES_API;
3246#else
3247 EGLint *context_attribs = NULL;
3248 EGLint api = EGL_OPENGL_API;
3249#endif
3250
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003251 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003252 if (!eglInitialize(d->dpy, &major, &minor)) {
3253 fprintf(stderr, "failed to initialize display\n");
3254 return -1;
3255 }
3256
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003257 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003258 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3259 return -1;
3260 }
3261
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003262 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3263 &d->argb_config, 1, &n) || n != 1) {
3264 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003265 return -1;
3266 }
3267
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003268 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003269 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003270 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003271 fprintf(stderr, "failed to create context\n");
3272 return -1;
3273 }
3274
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003275 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003276 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003277 return -1;
3278 }
3279
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003280#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003281 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3282 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3283 fprintf(stderr, "failed to get cairo egl argb device\n");
3284 return -1;
3285 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003286#endif
3287
3288 return 0;
3289}
3290
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003291static void
3292fini_egl(struct display *display)
3293{
3294#ifdef HAVE_CAIRO_EGL
3295 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003296#endif
3297
3298 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3299 EGL_NO_CONTEXT);
3300
3301 eglTerminate(display->dpy);
3302 eglReleaseThread();
3303}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003304#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003305
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003306static int
3307event_mask_update(uint32_t mask, void *data)
3308{
3309 struct display *d = data;
3310
3311 d->mask = mask;
3312
3313 return 0;
3314}
3315
3316static void
3317handle_display_data(struct task *task, uint32_t events)
3318{
3319 struct display *display =
3320 container_of(task, struct display, display_task);
3321
3322 wl_display_iterate(display->display, display->mask);
3323}
3324
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003325struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003326display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003327{
3328 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003329
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003330 argc = parse_options(xkb_options,
3331 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003332
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003333 d = malloc(sizeof *d);
3334 if (d == NULL)
3335 return NULL;
3336
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003337 memset(d, 0, sizeof *d);
3338
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003339 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003340 if (d->display == NULL) {
3341 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003342 return NULL;
3343 }
3344
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003345 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003346 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3347 d->display_task.run = handle_display_data;
3348 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3349
3350 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003351 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003352 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003353
Daniel Stone97f68542012-05-30 16:32:01 +01003354 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003355 if (d->xkb_context == NULL) {
3356 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01003357 return NULL;
3358 }
3359
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003360 /* Set up listener so we'll catch all events. */
3361 wl_display_add_global_listener(d->display,
3362 display_handle_global, d);
3363
3364 /* Process connection events. */
3365 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003366#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003367 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003368 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003369#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003370
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003371 d->image_target_texture_2d =
3372 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3373 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3374 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3375
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003376 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003377
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003378 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003379
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003380 wl_list_init(&d->window_list);
3381
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003382 return d;
3383}
3384
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003385static void
3386display_destroy_outputs(struct display *display)
3387{
3388 struct output *tmp;
3389 struct output *output;
3390
3391 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3392 output_destroy(output);
3393}
3394
Pekka Paalanene1207c72011-12-16 12:02:09 +02003395static void
3396display_destroy_inputs(struct display *display)
3397{
3398 struct input *tmp;
3399 struct input *input;
3400
3401 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3402 input_destroy(input);
3403}
3404
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003405void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003406display_destroy(struct display *display)
3407{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003408 if (!wl_list_empty(&display->window_list))
3409 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3410
3411 if (!wl_list_empty(&display->deferred_list))
3412 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3413
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003414 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003415 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003416
Daniel Stone97f68542012-05-30 16:32:01 +01003417 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003418
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003419 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003420 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003421
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003422#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003423 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003424#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003425
Pekka Paalanenc2052982011-12-16 11:41:32 +02003426 if (display->shell)
3427 wl_shell_destroy(display->shell);
3428
3429 if (display->shm)
3430 wl_shm_destroy(display->shm);
3431
3432 if (display->data_device_manager)
3433 wl_data_device_manager_destroy(display->data_device_manager);
3434
3435 wl_compositor_destroy(display->compositor);
3436
3437 close(display->epoll_fd);
3438
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003439 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003440 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003441 free(display);
3442}
3443
3444void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003445display_set_user_data(struct display *display, void *data)
3446{
3447 display->user_data = data;
3448}
3449
3450void *
3451display_get_user_data(struct display *display)
3452{
3453 return display->user_data;
3454}
3455
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003456struct wl_display *
3457display_get_display(struct display *display)
3458{
3459 return display->display;
3460}
3461
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003462struct output *
3463display_get_output(struct display *display)
3464{
3465 return container_of(display->output_list.next, struct output, link);
3466}
3467
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003468struct wl_compositor *
3469display_get_compositor(struct display *display)
3470{
3471 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003472}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003473
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003474uint32_t
3475display_get_serial(struct display *display)
3476{
3477 return display->serial;
3478}
3479
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003480EGLDisplay
3481display_get_egl_display(struct display *d)
3482{
3483 return d->dpy;
3484}
3485
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003486struct wl_data_source *
3487display_create_data_source(struct display *display)
3488{
3489 return wl_data_device_manager_create_data_source(display->data_device_manager);
3490}
3491
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003492EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003493display_get_argb_egl_config(struct display *d)
3494{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003495 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003496}
3497
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003498struct wl_shell *
3499display_get_shell(struct display *display)
3500{
3501 return display->shell;
3502}
3503
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003504int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003505display_acquire_window_surface(struct display *display,
3506 struct window *window,
3507 EGLContext ctx)
3508{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003509#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003510 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003511 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003512
3513 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003514 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003515 device = cairo_surface_get_device(window->cairo_surface);
3516 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003517 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003518
Benjamin Franzke0c991632011-09-27 21:57:31 +02003519 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003520 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003521 ctx = display->argb_ctx;
3522 else
3523 assert(0);
3524 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003525
3526 data = cairo_surface_get_user_data(window->cairo_surface,
3527 &surface_data_key);
3528
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003529 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003530 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003531 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3532 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003533
3534 return 0;
3535#else
3536 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003537#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003538}
3539
3540void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003541display_release_window_surface(struct display *display,
3542 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003543{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003544#ifdef HAVE_CAIRO_EGL
3545 cairo_device_t *device;
3546
3547 device = cairo_surface_get_device(window->cairo_surface);
3548 if (!device)
3549 return;
3550
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003551 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003552 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003553 cairo_device_release(device);
3554#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003555}
3556
3557void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003558display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003559{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003560 wl_list_insert(&display->deferred_list, &task->link);
3561}
3562
3563void
3564display_watch_fd(struct display *display,
3565 int fd, uint32_t events, struct task *task)
3566{
3567 struct epoll_event ep;
3568
3569 ep.events = events;
3570 ep.data.ptr = task;
3571 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3572}
3573
3574void
3575display_run(struct display *display)
3576{
3577 struct task *task;
3578 struct epoll_event ep[16];
3579 int i, count;
3580
Pekka Paalanen826d7952011-12-15 10:14:07 +02003581 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003582 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003583 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003584
3585 while (!wl_list_empty(&display->deferred_list)) {
3586 task = container_of(display->deferred_list.next,
3587 struct task, link);
3588 wl_list_remove(&task->link);
3589 task->run(task, 0);
3590 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003591
3592 if (!display->running)
3593 break;
3594
3595 wl_display_flush(display->display);
3596
3597 count = epoll_wait(display->epoll_fd,
3598 ep, ARRAY_LENGTH(ep), -1);
3599 for (i = 0; i < count; i++) {
3600 task = ep[i].data.ptr;
3601 task->run(task, ep[i].events);
3602 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003603 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003604}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003605
3606void
3607display_exit(struct display *display)
3608{
3609 display->running = 0;
3610}