blob: 2bea5c6c2486c0ddcd2a00c78e136153159b9c1c [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;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600147 int send_cursor_position;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400148 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400149 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500150
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400151 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700153 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400154
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500155 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500156 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500159 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400160
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200161 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500162 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500164 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400165 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500166};
167
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500168struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500169 struct window *window;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300170 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500171 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400172 struct wl_list link;
173 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500174 widget_resize_handler_t resize_handler;
175 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500176 widget_enter_handler_t enter_handler;
177 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500178 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500179 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400180 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500181 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300182 int tooltip_count;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183};
184
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185struct input {
186 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100187 struct wl_seat *seat;
188 struct wl_pointer *pointer;
189 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100205
206 struct {
207 struct xkb_rule_names names;
208 struct xkb_keymap *keymap;
209 struct xkb_state *state;
210 xkb_mod_mask_t control_mask;
211 xkb_mod_mask_t alt_mask;
212 xkb_mod_mask_t shift_mask;
213 } xkb;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400214};
215
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500216struct output {
217 struct display *display;
218 struct wl_output *output;
219 struct rectangle allocation;
220 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200221
222 display_output_handler_t destroy_handler;
223 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500224};
225
Martin Minarik1998b152012-05-10 02:04:35 +0200226enum frame_button_action {
227 FRAME_BUTTON_NULL = 0,
228 FRAME_BUTTON_ICON = 1,
229 FRAME_BUTTON_CLOSE = 2,
230 FRAME_BUTTON_MINIMIZE = 3,
231 FRAME_BUTTON_MAXIMIZE = 4,
232};
233
234enum frame_button_pointer {
235 FRAME_BUTTON_DEFAULT = 0,
236 FRAME_BUTTON_OVER = 1,
237 FRAME_BUTTON_ACTIVE = 2,
238};
239
240enum frame_button_align {
241 FRAME_BUTTON_RIGHT = 0,
242 FRAME_BUTTON_LEFT = 1,
243};
244
245enum frame_button_decoration {
246 FRAME_BUTTON_NONE = 0,
247 FRAME_BUTTON_FANCY = 1,
248};
249
250struct frame_button {
251 struct widget *widget;
252 struct frame *frame;
253 cairo_surface_t *icon;
254 enum frame_button_action type;
255 enum frame_button_pointer state;
256 struct wl_list link; /* buttons_list */
257 enum frame_button_align align;
258 enum frame_button_decoration decoration;
259};
260
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500261struct frame {
262 struct widget *widget;
263 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200264 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500265};
266
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500267struct menu {
268 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500269 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500270 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500271 const char **entries;
272 uint32_t time;
273 int current;
274 int count;
275 menu_func_t func;
276};
277
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300278struct tooltip {
279 struct widget *parent;
280 struct window *window;
281 struct widget *widget;
282 char *entry;
283 struct task tooltip_task;
284 int tooltip_fd;
285 float x, y;
286};
287
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700288struct shm_pool {
289 struct wl_shm_pool *pool;
290 size_t size;
291 size_t used;
292 void *data;
293};
294
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400295enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300296 CURSOR_DEFAULT = 100,
297 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400298};
299
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500300enum window_location {
301 WINDOW_INTERIOR = 0,
302 WINDOW_RESIZING_TOP = 1,
303 WINDOW_RESIZING_BOTTOM = 2,
304 WINDOW_RESIZING_LEFT = 4,
305 WINDOW_RESIZING_TOP_LEFT = 5,
306 WINDOW_RESIZING_BOTTOM_LEFT = 6,
307 WINDOW_RESIZING_RIGHT = 8,
308 WINDOW_RESIZING_TOP_RIGHT = 9,
309 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
310 WINDOW_RESIZING_MASK = 15,
311 WINDOW_EXTERIOR = 16,
312 WINDOW_TITLEBAR = 17,
313 WINDOW_CLIENT_AREA = 18,
314};
315
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400316const char *option_xkb_layout = "us";
317const char *option_xkb_variant = "";
318const char *option_xkb_options = "";
319
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400320static const struct weston_option xkb_options[] = {
321 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
322 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
323 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400324};
325
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326static const cairo_user_data_key_t surface_data_key;
327struct surface_data {
328 struct wl_buffer *buffer;
329};
330
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500331#define MULT(_d,c,a,t) \
332 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500334#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100336struct egl_window_surface_data {
337 struct display *display;
338 struct wl_surface *surface;
339 struct wl_egl_window *window;
340 EGLSurface surf;
341};
342
343static void
344egl_window_surface_data_destroy(void *p)
345{
346 struct egl_window_surface_data *data = p;
347 struct display *d = data->display;
348
349 eglDestroySurface(d->dpy, data->surf);
350 wl_egl_window_destroy(data->window);
351 data->surface = NULL;
352
353 free(p);
354}
355
356static cairo_surface_t *
357display_create_egl_window_surface(struct display *display,
358 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400359 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100360 struct rectangle *rectangle)
361{
362 cairo_surface_t *cairo_surface;
363 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200365 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100366
367 data = malloc(sizeof *data);
368 if (data == NULL)
369 return NULL;
370
371 data->display = display;
372 data->surface = surface;
373
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500374 config = display->argb_config;
375 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400377 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100380
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400381 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500382 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100383
Benjamin Franzke0c991632011-09-27 21:57:31 +0200384 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100385 data->surf,
386 rectangle->width,
387 rectangle->height);
388
389 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
390 data, egl_window_surface_data_destroy);
391
392 return cairo_surface;
393}
394
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395#endif
396
397struct wl_buffer *
398display_get_buffer_for_surface(struct display *display,
399 cairo_surface_t *surface)
400{
401 struct surface_data *data;
402
403 data = cairo_surface_get_user_data (surface, &surface_data_key);
404
405 return data->buffer;
406}
407
408struct shm_surface_data {
409 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700410 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411};
412
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500413static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414shm_pool_destroy(struct shm_pool *pool);
415
416static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417shm_surface_data_destroy(void *p)
418{
419 struct shm_surface_data *data = p;
420
421 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700422 if (data->pool)
423 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424}
425
Kristian Høgsberg16626282012-04-03 11:21:27 -0400426static struct wl_shm_pool *
427make_shm_pool(struct display *display, int size, void **data)
428{
429 char filename[] = "/tmp/wayland-shm-XXXXXX";
430 struct wl_shm_pool *pool;
431 int fd;
432
433 fd = mkstemp(filename);
434 if (fd < 0) {
435 fprintf(stderr, "open %s failed: %m\n", filename);
436 return NULL;
437 }
438 if (ftruncate(fd, size) < 0) {
439 fprintf(stderr, "ftruncate failed: %m\n");
440 close(fd);
441 return NULL;
442 }
443
444 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
445 unlink(filename);
446
447 if (*data == MAP_FAILED) {
448 fprintf(stderr, "mmap failed: %m\n");
449 close(fd);
450 return NULL;
451 }
452
453 pool = wl_shm_create_pool(display->shm, fd, size);
454
455 close(fd);
456
457 return pool;
458}
459
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700460static struct shm_pool *
461shm_pool_create(struct display *display, size_t size)
462{
463 struct shm_pool *pool = malloc(sizeof *pool);
464
465 if (!pool)
466 return NULL;
467
468 pool->pool = make_shm_pool(display, size, &pool->data);
469 if (!pool->pool) {
470 free(pool);
471 return NULL;
472 }
473
474 pool->size = size;
475 pool->used = 0;
476
477 return pool;
478}
479
480static void *
481shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
482{
483 if (pool->used + size > pool->size)
484 return NULL;
485
486 *offset = pool->used;
487 pool->used += size;
488
489 return (char *) pool->data + *offset;
490}
491
492/* destroy the pool. this does not unmap the memory though */
493static void
494shm_pool_destroy(struct shm_pool *pool)
495{
496 munmap(pool->data, pool->size);
497 wl_shm_pool_destroy(pool->pool);
498 free(pool);
499}
500
501/* Start allocating from the beginning of the pool again */
502static void
503shm_pool_reset(struct shm_pool *pool)
504{
505 pool->used = 0;
506}
507
508static int
509data_length_for_shm_surface(struct rectangle *rect)
510{
511 int stride;
512
513 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
514 rect->width);
515 return stride * rect->height;
516}
517
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500518static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700519display_create_shm_surface_from_pool(struct display *display,
520 struct rectangle *rectangle,
521 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522{
523 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400524 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400525 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700526 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400527 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528
529 data = malloc(sizeof *data);
530 if (data == NULL)
531 return NULL;
532
533 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
534 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700535 length = stride * rectangle->height;
536 data->pool = NULL;
537 map = shm_pool_allocate(pool, length, &offset);
538
539 if (!map) {
540 free(data);
541 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400542 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400544 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400545 CAIRO_FORMAT_ARGB32,
546 rectangle->width,
547 rectangle->height,
548 stride);
549
550 cairo_surface_set_user_data (surface, &surface_data_key,
551 data, shm_surface_data_destroy);
552
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500554 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500556 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700558 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400559 rectangle->width,
560 rectangle->height,
561 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700563 return surface;
564}
565
566static cairo_surface_t *
567display_create_shm_surface(struct display *display,
568 struct rectangle *rectangle, uint32_t flags,
569 struct window *window)
570{
571 struct shm_surface_data *data;
572 struct shm_pool *pool;
573 cairo_surface_t *surface;
574
575 if (window && window->pool) {
576 shm_pool_reset(window->pool);
577 surface = display_create_shm_surface_from_pool(display,
578 rectangle,
579 flags,
580 window->pool);
581 if (surface)
582 return surface;
583 }
584
585 pool = shm_pool_create(display,
586 data_length_for_shm_surface(rectangle));
587 if (!pool)
588 return NULL;
589
590 surface =
591 display_create_shm_surface_from_pool(display, rectangle,
592 flags, pool);
593
594 if (!surface) {
595 shm_pool_destroy(pool);
596 return NULL;
597 }
598
599 /* make sure we destroy the pool when the surface is destroyed */
600 data = cairo_surface_get_user_data(surface, &surface_data_key);
601 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602
603 return surface;
604}
605
nobled7b87cb02011-02-01 18:51:47 +0000606static int
607check_size(struct rectangle *rect)
608{
609 if (rect->width && rect->height)
610 return 0;
611
612 fprintf(stderr, "tried to create surface of "
613 "width: %d, height: %d\n", rect->width, rect->height);
614 return -1;
615}
616
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617cairo_surface_t *
618display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400620 struct rectangle *rectangle,
621 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622{
nobled7b87cb02011-02-01 18:51:47 +0000623 if (check_size(rectangle) < 0)
624 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500625#ifdef HAVE_CAIRO_EGL
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300626 if (display->dpy && !(flags & SURFACE_SHM))
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400627 return display_create_egl_window_surface(display,
628 surface,
629 flags,
630 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400632 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633}
634
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300635static const char *cursors[] = {
636 "bottom_left_corner",
637 "bottom_right_corner",
638 "bottom_side",
639 "grabbing",
640 "left_ptr",
641 "left_side",
642 "right_side",
643 "top_left_corner",
644 "top_right_corner",
645 "top_side",
646 "xterm",
647 "hand1",
648};
649
650static void
651create_cursors(struct display *display)
652{
653 unsigned int i;
654
655 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
656 display->cursors =
657 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
658
659 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
660 display->cursors[i] =
661 wl_cursor_theme_get_cursor(display->cursor_theme,
662 cursors[i]);
663}
664
665static void
666destroy_cursors(struct display *display)
667{
668 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +0800669 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300670}
671
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300672struct wl_cursor_image *
673display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674{
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300675 struct wl_cursor *cursor =
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300676 wl_cursor_theme_get_cursor(display->cursor_theme,
677 cursors[pointer]);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300679 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400680}
681
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200683window_get_resize_dx_dy(struct window *window, int *x, int *y)
684{
685 if (window->resize_edges & WINDOW_RESIZING_LEFT)
686 *x = window->server_allocation.width - window->allocation.width;
687 else
688 *x = 0;
689
690 if (window->resize_edges & WINDOW_RESIZING_TOP)
691 *y = window->server_allocation.height -
692 window->allocation.height;
693 else
694 *y = 0;
695
696 window->resize_edges = 0;
697}
698
699static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500700window_attach_surface(struct window *window)
701{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400702 struct display *display = window->display;
703 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000704#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100705 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000706#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500707 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100708
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400709 if (window->type == TYPE_NONE) {
710 window->type = TYPE_TOPLEVEL;
711 if (display->shell)
712 wl_shell_surface_set_toplevel(window->shell_surface);
713 }
714
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100715 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000716#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100717 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
718 data = cairo_surface_get_user_data(window->cairo_surface,
719 &surface_data_key);
720
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100721 cairo_gl_surface_swapbuffers(window->cairo_surface);
722 wl_egl_window_get_attached_size(data->window,
723 &window->server_allocation.width,
724 &window->server_allocation.height);
725 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100727 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728 buffer =
729 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400730 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200732 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100733 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400734 wl_surface_damage(window->surface, 0, 0,
735 window->allocation.width,
736 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100737 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400738 cairo_surface_destroy(window->cairo_surface);
739 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100740 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741 default:
742 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500744
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500745 if (window->input_region) {
746 wl_surface_set_input_region(window->surface,
747 window->input_region);
748 wl_region_destroy(window->input_region);
749 window->input_region = NULL;
750 }
751
752 if (window->opaque_region) {
753 wl_surface_set_opaque_region(window->surface,
754 window->opaque_region);
755 wl_region_destroy(window->opaque_region);
756 window->opaque_region = NULL;
757 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500758}
759
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500760void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400761window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500762{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400763 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100764 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500765}
766
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400767void
768window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500770 cairo_surface_reference(surface);
771
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400772 if (window->cairo_surface != NULL)
773 cairo_surface_destroy(window->cairo_surface);
774
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500775 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400776}
777
Benjamin Franzke22d54812011-07-16 19:50:32 +0000778#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100779static void
780window_resize_cairo_window_surface(struct window *window)
781{
782 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200783 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784
785 data = cairo_surface_get_user_data(window->cairo_surface,
786 &surface_data_key);
787
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200788 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 wl_egl_window_resize(data->window,
790 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200791 window->allocation.height,
792 x,y);
793
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100794 cairo_gl_surface_set_size(window->cairo_surface,
795 window->allocation.width,
796 window->allocation.height);
797}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000798#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400800struct display *
801window_get_display(struct window *window)
802{
803 return window->display;
804}
805
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400806void
807window_create_surface(struct window *window)
808{
809 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400810 uint32_t flags = 0;
811
812 if (!window->transparent)
813 flags = SURFACE_OPAQUE;
814
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500816#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
818 if (window->cairo_surface) {
819 window_resize_cairo_window_surface(window);
820 return;
821 }
822 surface = display_create_surface(window->display,
823 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826#endif
827 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400828 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400829 &window->allocation,
830 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800832 default:
833 surface = NULL;
834 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836
837 window_set_surface(window, surface);
838 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400839}
840
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200841static void frame_destroy(struct frame *frame);
842
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400843void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500844window_destroy(struct window *window)
845{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200846 struct display *display = window->display;
847 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +0100848 struct window_output *window_output;
849 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200850
851 if (window->redraw_scheduled)
852 wl_list_remove(&window->redraw_task.link);
853
854 wl_list_for_each(input, &display->input_list, link) {
855 if (input->pointer_focus == window)
856 input->pointer_focus = NULL;
857 if (input->keyboard_focus == window)
858 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500859 if (input->focus_widget &&
860 input->focus_widget->window == window)
861 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200862 }
863
Rob Bradford7507b572012-05-15 17:55:34 +0100864 wl_list_for_each_safe(window_output, window_output_tmp,
865 &window->window_output_list, link) {
866 free (window_output);
867 }
868
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500869 if (window->input_region)
870 wl_region_destroy(window->input_region);
871 if (window->opaque_region)
872 wl_region_destroy(window->opaque_region);
873
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200874 if (window->frame)
875 frame_destroy(window->frame);
876
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200877 if (window->shell_surface)
878 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500879 wl_surface_destroy(window->surface);
880 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200881
882 if (window->cairo_surface != NULL)
883 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200884
885 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500886 free(window);
887}
888
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500889static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500890widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400891{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500892 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400893
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500894 wl_list_for_each(child, &widget->child_list, link) {
895 target = widget_find_widget(child, x, y);
896 if (target)
897 return target;
898 }
899
900 if (widget->allocation.x <= x &&
901 x < widget->allocation.x + widget->allocation.width &&
902 widget->allocation.y <= y &&
903 y < widget->allocation.y + widget->allocation.height) {
904 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400905 }
906
907 return NULL;
908}
909
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500910static struct widget *
911widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400912{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500913 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400914
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500915 widget = malloc(sizeof *widget);
916 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500917 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500918 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500919 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500920 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500921 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300922 widget->tooltip = NULL;
923 widget->tooltip_count = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924
925 return widget;
926}
927
928struct widget *
929window_add_widget(struct window *window, void *data)
930{
931 window->widget = widget_create(window, data);
932 wl_list_init(&window->widget->link);
933
934 return window->widget;
935}
936
937struct widget *
938widget_add_widget(struct widget *parent, void *data)
939{
940 struct widget *widget;
941
942 widget = widget_create(parent->window, data);
943 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400944
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500945 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946}
947
948void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500949widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400950{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200951 struct display *display = widget->window->display;
952 struct input *input;
953
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300954 if (widget->tooltip) {
955 free(widget->tooltip);
956 widget->tooltip = NULL;
957 }
958
Pekka Paalanene156fb62012-01-19 13:51:38 +0200959 wl_list_for_each(input, &display->input_list, link) {
960 if (input->focus_widget == widget)
961 input->focus_widget = NULL;
962 }
963
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500964 wl_list_remove(&widget->link);
965 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400966}
967
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400968void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500969widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400970{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500971 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400972}
973
974void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500975widget_set_size(struct widget *widget, int32_t width, int32_t height)
976{
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977 widget->allocation.width = width;
978 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500979}
980
981void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500982widget_set_allocation(struct widget *widget,
983 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400984{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985 widget->allocation.x = x;
986 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +0200987 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400988}
989
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500990void
991widget_set_transparent(struct widget *widget, int transparent)
992{
993 widget->opaque = !transparent;
994}
995
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000}
1001
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001003widget_set_resize_handler(struct widget *widget,
1004 widget_resize_handler_t handler)
1005{
1006 widget->resize_handler = handler;
1007}
1008
1009void
1010widget_set_redraw_handler(struct widget *widget,
1011 widget_redraw_handler_t handler)
1012{
1013 widget->redraw_handler = handler;
1014}
1015
1016void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001017widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001018{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001019 widget->enter_handler = handler;
1020}
1021
1022void
1023widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1024{
1025 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001026}
1027
1028void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001029widget_set_motion_handler(struct widget *widget,
1030 widget_motion_handler_t handler)
1031{
1032 widget->motion_handler = handler;
1033}
1034
1035void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001036widget_set_button_handler(struct widget *widget,
1037 widget_button_handler_t handler)
1038{
1039 widget->button_handler = handler;
1040}
1041
1042void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001043widget_schedule_redraw(struct widget *widget)
1044{
1045 window_schedule_redraw(widget->window);
1046}
1047
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001048cairo_surface_t *
1049window_get_surface(struct window *window)
1050{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001051 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001052}
1053
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001054struct wl_surface *
1055window_get_wl_surface(struct window *window)
1056{
1057 return window->surface;
1058}
1059
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001060struct wl_shell_surface *
1061window_get_wl_shell_surface(struct window *window)
1062{
1063 return window->shell_surface;
1064}
1065
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001066static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001067tooltip_redraw_handler(struct widget *widget, void *data)
1068{
1069 cairo_t *cr;
1070 const int32_t r = 3;
1071 struct tooltip *tooltip = data;
1072 int32_t width, height;
1073 struct window *window = widget->window;
1074
1075 cr = cairo_create(window->cairo_surface);
1076 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1077 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1078 cairo_paint(cr);
1079
1080 width = window->allocation.width;
1081 height = window->allocation.height;
1082 rounded_rect(cr, 0, 0, width, height, r);
1083
1084 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1085 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1086 cairo_fill(cr);
1087
1088 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1089 cairo_move_to(cr, 10, 16);
1090 cairo_show_text(cr, tooltip->entry);
1091 cairo_destroy(cr);
1092}
1093
1094static cairo_text_extents_t
1095get_text_extents(struct tooltip *tooltip)
1096{
1097 struct window *window;
1098 cairo_t *cr;
1099 cairo_text_extents_t extents;
1100
1101 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1102 * created yet */
1103 window = tooltip->widget->window->parent;
1104 cr = cairo_create(window->cairo_surface);
1105 cairo_text_extents(cr, tooltip->entry, &extents);
1106 cairo_destroy(cr);
1107
1108 return extents;
1109}
1110
1111static int
1112window_create_tooltip(struct tooltip *tooltip)
1113{
1114 struct widget *parent = tooltip->parent;
1115 struct display *display = parent->window->display;
1116 struct window *window;
1117 const int offset_y = 27;
1118 const int margin = 3;
1119 cairo_text_extents_t extents;
1120
1121 if (tooltip->widget)
1122 return 0;
1123
1124 window = window_create_transient(display, parent->window, tooltip->x,
1125 tooltip->y + offset_y,
1126 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1127 if (!window)
1128 return -1;
1129
1130 tooltip->window = window;
1131 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1132
1133 extents = get_text_extents(tooltip);
1134 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1135 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1136
1137 return 0;
1138}
1139
1140void
1141widget_destroy_tooltip(struct widget *parent)
1142{
1143 struct tooltip *tooltip = parent->tooltip;
1144
1145 parent->tooltip_count = 0;
1146 if (!tooltip)
1147 return;
1148
1149 if (tooltip->widget) {
1150 widget_destroy(tooltip->widget);
1151 window_destroy(tooltip->window);
1152 tooltip->widget = NULL;
1153 tooltip->window = NULL;
1154 }
1155
1156 close(tooltip->tooltip_fd);
1157 free(tooltip->entry);
1158 free(tooltip);
1159 parent->tooltip = NULL;
1160}
1161
1162static void
1163tooltip_func(struct task *task, uint32_t events)
1164{
1165 struct tooltip *tooltip =
1166 container_of(task, struct tooltip, tooltip_task);
1167 uint64_t exp;
1168
1169 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1170 window_create_tooltip(tooltip);
1171}
1172
1173#define TOOLTIP_TIMEOUT 500
1174static int
1175tooltip_timer_reset(struct tooltip *tooltip)
1176{
1177 struct itimerspec its;
1178
1179 its.it_interval.tv_sec = 0;
1180 its.it_interval.tv_nsec = 0;
1181 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1182 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1183 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1184 fprintf(stderr, "could not set timerfd\n: %m");
1185 return -1;
1186 }
1187
1188 return 0;
1189}
1190
1191int
1192widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1193{
1194 struct tooltip *tooltip = parent->tooltip;
1195
1196 parent->tooltip_count++;
1197 if (tooltip) {
1198 tooltip->x = x;
1199 tooltip->y = y;
1200 tooltip_timer_reset(tooltip);
1201 return 0;
1202 }
1203
1204 /* the handler might be triggered too fast via input device motion, so
1205 * we need this check here to make sure tooltip is fully initialized */
1206 if (parent->tooltip_count > 1)
1207 return 0;
1208
1209 tooltip = malloc(sizeof *tooltip);
1210 if (!tooltip)
1211 return -1;
1212
1213 parent->tooltip = tooltip;
1214 tooltip->parent = parent;
1215 tooltip->widget = NULL;
1216 tooltip->window = NULL;
1217 tooltip->x = x;
1218 tooltip->y = y;
1219 tooltip->entry = strdup(entry);
1220 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1221 if (tooltip->tooltip_fd < 0) {
1222 fprintf(stderr, "could not create timerfd\n: %m");
1223 return -1;
1224 }
1225
1226 tooltip->tooltip_task.run = tooltip_func;
1227 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1228 EPOLLIN, &tooltip->tooltip_task);
1229 tooltip_timer_reset(tooltip);
1230
1231 return 0;
1232}
1233
1234static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235frame_resize_handler(struct widget *widget,
1236 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001237{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 struct frame *frame = data;
1239 struct widget *child = frame->child;
1240 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001241 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001242 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001243 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001244 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001246 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001248 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001249 decoration_width = (t->width + t->margin) * 2;
1250 decoration_height = t->width +
1251 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001253 allocation.x = t->width + t->margin;
1254 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001255 allocation.width = width - decoration_width;
1256 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001257
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001258 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001259
1260 wl_list_for_each(button, &frame->buttons_list, link)
1261 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001262 } else {
1263 decoration_width = 0;
1264 decoration_height = 0;
1265
1266 allocation.x = 0;
1267 allocation.y = 0;
1268 allocation.width = width;
1269 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001270 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001271
1272 wl_list_for_each(button, &frame->buttons_list, link)
1273 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001274 }
1275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001276 widget_set_allocation(child, allocation.x, allocation.y,
1277 allocation.width, allocation.height);
1278
1279 if (child->resize_handler)
1280 child->resize_handler(child,
1281 allocation.width,
1282 allocation.height,
1283 child->user_data);
1284
Scott Moreauf7e498c2012-05-14 11:39:29 -06001285 width = child->allocation.width + decoration_width;
1286 height = child->allocation.height + decoration_height;
1287
1288 widget->window->input_region =
1289 wl_compositor_create_region(display->compositor);
1290 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001291 t->margin, t->margin,
1292 width - 2 * t->margin,
1293 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001294
1295 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001296
1297 if (child->opaque) {
1298 widget->window->opaque_region =
1299 wl_compositor_create_region(display->compositor);
1300 wl_region_add(widget->window->opaque_region,
1301 opaque_margin, opaque_margin,
1302 widget->allocation.width - 2 * opaque_margin,
1303 widget->allocation.height - 2 * opaque_margin);
1304 }
Martin Minarik1998b152012-05-10 02:04:35 +02001305
1306 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001307 x_r = frame->widget->allocation.width - t->width - t->margin;
1308 x_l = t->width + t->margin;
1309 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001310 wl_list_for_each(button, &frame->buttons_list, link) {
1311 const int button_padding = 4;
1312 w = cairo_image_surface_get_width(button->icon);
1313 h = cairo_image_surface_get_height(button->icon);
1314
1315 if (button->decoration == FRAME_BUTTON_FANCY)
1316 w += 10;
1317
1318 if (button->align == FRAME_BUTTON_LEFT) {
1319 widget_set_allocation(button->widget,
1320 x_l, y , w + 1, h + 1);
1321 x_l += w;
1322 x_l += button_padding;
1323 } else {
1324 x_r -= w;
1325 widget_set_allocation(button->widget,
1326 x_r, y , w + 1, h + 1);
1327 x_r -= button_padding;
1328 }
1329 }
1330}
1331
1332static int
1333frame_button_enter_handler(struct widget *widget,
1334 struct input *input, float x, float y, void *data)
1335{
1336 struct frame_button *frame_button = data;
1337
1338 widget_schedule_redraw(frame_button->widget);
1339 frame_button->state = FRAME_BUTTON_OVER;
1340
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001341 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02001342}
1343
1344static void
1345frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1346{
1347 struct frame_button *frame_button = data;
1348
1349 widget_schedule_redraw(frame_button->widget);
1350 frame_button->state = FRAME_BUTTON_DEFAULT;
1351}
1352
1353static void
1354frame_button_button_handler(struct widget *widget,
1355 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001356 uint32_t button,
1357 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02001358{
1359 struct frame_button *frame_button = data;
1360 struct window *window = widget->window;
1361
1362 if (button != BTN_LEFT)
1363 return;
1364
1365 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01001366 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02001367 frame_button->state = FRAME_BUTTON_ACTIVE;
1368 widget_schedule_redraw(frame_button->widget);
1369
1370 if (frame_button->type == FRAME_BUTTON_ICON)
1371 window_show_frame_menu(window, input, time);
1372 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001373 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02001374 frame_button->state = FRAME_BUTTON_DEFAULT;
1375 widget_schedule_redraw(frame_button->widget);
1376 break;
1377 }
1378
1379 switch (frame_button->type) {
1380 case FRAME_BUTTON_CLOSE:
1381 if (window->close_handler)
1382 window->close_handler(window->parent,
1383 window->user_data);
1384 else
1385 display_exit(window->display);
1386 break;
1387 case FRAME_BUTTON_MINIMIZE:
1388 fprintf(stderr,"Minimize stub\n");
1389 break;
1390 case FRAME_BUTTON_MAXIMIZE:
1391 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1392 break;
1393 default:
1394 /* Unknown operation */
1395 break;
1396 }
1397}
1398
1399static void
1400frame_button_redraw_handler(struct widget *widget, void *data)
1401{
1402 struct frame_button *frame_button = data;
1403 cairo_t *cr;
1404 int width, height, x, y;
1405 struct window *window = widget->window;
1406
1407 x = widget->allocation.x;
1408 y = widget->allocation.y;
1409 width = widget->allocation.width;
1410 height = widget->allocation.height;
1411
1412 if (!width)
1413 return;
1414 if (!height)
1415 return;
1416 if (widget->opaque)
1417 return;
1418
1419 cr = cairo_create(window->cairo_surface);
1420
1421 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1422 cairo_set_line_width(cr, 1);
1423
1424 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1425 cairo_rectangle (cr, x, y, 25, 16);
1426
1427 cairo_stroke_preserve(cr);
1428
1429 switch (frame_button->state) {
1430 case FRAME_BUTTON_DEFAULT:
1431 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1432 break;
1433 case FRAME_BUTTON_OVER:
1434 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1435 break;
1436 case FRAME_BUTTON_ACTIVE:
1437 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1438 break;
1439 }
1440
1441 cairo_fill (cr);
1442
1443 x += 4;
1444 }
1445
1446 cairo_set_source_surface(cr, frame_button->icon, x, y);
1447 cairo_paint(cr);
1448
1449 cairo_destroy(cr);
1450}
1451
1452static struct widget *
1453frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1454 enum frame_button_align align, enum frame_button_decoration style)
1455{
1456 struct frame_button *frame_button;
1457 const char *icon = data;
1458
1459 frame_button = malloc (sizeof *frame_button);
1460 memset(frame_button, 0, sizeof *frame_button);
1461
1462 frame_button->icon = cairo_image_surface_create_from_png(icon);
1463 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1464 frame_button->frame = frame;
1465 frame_button->type = type;
1466 frame_button->align = align;
1467 frame_button->decoration = style;
1468
1469 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1470
1471 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1472 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1473 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1474 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1475 return frame_button->widget;
1476}
1477
1478static void
1479frame_button_destroy(struct frame_button *frame_button)
1480{
1481 widget_destroy(frame_button->widget);
1482 wl_list_remove(&frame_button->link);
1483 cairo_surface_destroy(frame_button->icon);
1484 free(frame_button);
1485
1486 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001487}
1488
1489static void
1490frame_redraw_handler(struct widget *widget, void *data)
1491{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001492 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001494 struct theme *t = window->display->theme;
1495 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001496
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001497 if (window->type == TYPE_FULLSCREEN)
1498 return;
1499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001500 cr = cairo_create(window->cairo_surface);
1501
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001502 if (window->keyboard_device)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04001503 flags |= THEME_FRAME_ACTIVE;
1504 theme_render_frame(t, cr, widget->allocation.width,
1505 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001506
1507 cairo_destroy(cr);
1508}
1509
1510static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001511frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001512{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001513 struct theme *t = frame->widget->window->display->theme;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001514 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001516 location = theme_get_location(t, input->sx, input->sy,
1517 frame->widget->allocation.width,
1518 frame->widget->allocation.height);
1519
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001520 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001521 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001522 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001523 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001524 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001525 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001526 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001527 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001528 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001529 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001530 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001532 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001533 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001534 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001535 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001536 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001537 case THEME_LOCATION_EXTERIOR:
1538 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001539 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001540 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001541 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001542}
1543
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001544static void
1545frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001546{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001547 switch (index) {
1548 case 0: /* close */
1549 if (window->close_handler)
1550 window->close_handler(window->parent,
1551 window->user_data);
1552 else
1553 display_exit(window->display);
1554 break;
1555 case 1: /* fullscreen */
1556 /* we don't have a way to get out of fullscreen for now */
1557 window_set_fullscreen(window, 1);
1558 break;
1559 case 2: /* rotate */
1560 case 3: /* scale */
1561 break;
1562 }
1563}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001564
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001565void
1566window_show_frame_menu(struct window *window,
1567 struct input *input, uint32_t time)
1568{
1569 int32_t x, y;
1570
1571 static const char *entries[] = {
1572 "Close", "Fullscreen", "Rotate", "Scale"
1573 };
1574
1575 input_get_position(input, &x, &y);
1576 window_show_menu(window->display, input, time, window,
1577 x - 10, y - 10, frame_menu_func, entries, 4);
1578}
1579
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580static int
1581frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001582 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001583{
1584 return frame_get_pointer_image_for_location(data, input);
1585}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001586
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001587static int
1588frame_motion_handler(struct widget *widget,
1589 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001590 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001591{
1592 return frame_get_pointer_image_for_location(data, input);
1593}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001594
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001595static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596frame_button_handler(struct widget *widget,
1597 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001598 uint32_t button, enum wl_pointer_button_state state,
1599 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600
1601{
1602 struct frame *frame = data;
1603 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001604 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001605 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001606
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001607 location = theme_get_location(display->theme, input->sx, input->sy,
1608 frame->widget->allocation.width,
1609 frame->widget->allocation.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001610
Daniel Stone4dbadb12012-05-30 16:31:51 +01001611 if (window->display->shell && button == BTN_LEFT &&
1612 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001613 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001614 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 if (!window->shell_surface)
1616 break;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001617 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001618 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001620 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001621 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001622 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001623 case THEME_LOCATION_RESIZING_TOP:
1624 case THEME_LOCATION_RESIZING_BOTTOM:
1625 case THEME_LOCATION_RESIZING_LEFT:
1626 case THEME_LOCATION_RESIZING_RIGHT:
1627 case THEME_LOCATION_RESIZING_TOP_LEFT:
1628 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1629 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1630 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001631 if (!window->shell_surface)
1632 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001633 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001634
1635 if (!display->dpy) {
1636 /* If we're using shm, allocate a big
1637 pool to create buffers out of while
1638 we resize. We should probably base
1639 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001640 window->pool =
1641 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001642 }
1643
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001645 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001646 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001647 break;
1648 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01001649 } else if (button == BTN_RIGHT &&
1650 state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001651 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001652 }
1653}
1654
1655struct widget *
1656frame_create(struct window *window, void *data)
1657{
1658 struct frame *frame;
1659
1660 frame = malloc(sizeof *frame);
1661 memset(frame, 0, sizeof *frame);
1662
1663 frame->widget = window_add_widget(window, frame);
1664 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001665
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001666 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1667 widget_set_resize_handler(frame->widget, frame_resize_handler);
1668 widget_set_enter_handler(frame->widget, frame_enter_handler);
1669 widget_set_motion_handler(frame->widget, frame_motion_handler);
1670 widget_set_button_handler(frame->widget, frame_button_handler);
1671
Martin Minarik1998b152012-05-10 02:04:35 +02001672 /* Create empty list for frame buttons */
1673 wl_list_init(&frame->buttons_list);
1674
1675 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1676 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1677
1678 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1679 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680
1681 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1682 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1683
1684 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1685 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001687 window->frame = frame;
1688
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001689 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001690}
1691
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001692static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001693frame_destroy(struct frame *frame)
1694{
Martin Minarik1998b152012-05-10 02:04:35 +02001695 struct frame_button *button, *tmp;
1696
1697 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1698 frame_button_destroy(button);
1699
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001700 /* frame->child must be destroyed by the application */
1701 widget_destroy(frame->widget);
1702 free(frame);
1703}
1704
1705static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001706input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001707 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001709 struct widget *old, *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001710 int pointer = CURSOR_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001712 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001713 return;
1714
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001715 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001716 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001717 widget = old;
1718 if (input->grab)
1719 widget = input->grab;
1720 if (widget->leave_handler)
1721 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001722 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001723 }
1724
1725 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001726 widget = focus;
1727 if (input->grab)
1728 widget = input->grab;
1729 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øgsbergb6323512012-01-11 00:04:42 -05001732 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001733
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001734 input_set_pointer_image(input, input->pointer_enter_serial,
1735 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001736 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001737}
1738
1739static void
Daniel Stone37816df2012-05-16 18:45:18 +01001740pointer_handle_motion(void *data, struct wl_pointer *pointer,
1741 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001742{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001743 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001744 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001745 struct widget *widget;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001746 int cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001747 float sx = wl_fixed_to_double(sx_w);
1748 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001749
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001750 input->sx = sx;
1751 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001752
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001754 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001755 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001756 }
1757
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001758 if (input->grab)
1759 widget = input->grab;
1760 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001761 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001762 if (widget && widget->motion_handler)
Daniel Stone37816df2012-05-16 18:45:18 +01001763 cursor = widget->motion_handler(input->focus_widget,
1764 input, time, sx, sy,
1765 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001766
Daniel Stone37816df2012-05-16 18:45:18 +01001767 input_set_pointer_image(input, time, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001768}
1769
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001770void
1771input_grab(struct input *input, struct widget *widget, uint32_t button)
1772{
1773 input->grab = widget;
1774 input->grab_button = button;
1775}
1776
1777void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001778input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001779{
1780 struct widget *widget;
1781
1782 input->grab = NULL;
1783 if (input->pointer_focus) {
1784 widget = widget_find_widget(input->pointer_focus->widget,
1785 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001786 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001787 }
1788}
1789
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001790static void
Daniel Stone37816df2012-05-16 18:45:18 +01001791pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001792 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001793{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001794 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001795 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001796 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001797
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001798 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001799 if (input->focus_widget && input->grab == NULL &&
1800 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001801 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001802
Neil Roberts6b28aad2012-01-23 19:11:18 +00001803 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001804 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001805 (*widget->button_handler)(widget,
1806 input, time,
1807 button, state,
1808 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001809
Daniel Stone4dbadb12012-05-30 16:31:51 +01001810 if (input->grab && input->grab_button == button &&
1811 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001812 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001813}
1814
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001815static void
Daniel Stone37816df2012-05-16 18:45:18 +01001816pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001817 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001818{
1819}
1820
1821static void
Daniel Stone37816df2012-05-16 18:45:18 +01001822keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001823 uint32_t serial, uint32_t time, uint32_t key,
1824 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001825{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001826 struct input *input = data;
1827 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001828 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001829 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001830 const xkb_keysym_t *syms;
1831 xkb_keysym_t sym;
1832 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001833
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001834 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001835 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001836 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001837 return;
1838
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001839 if (state)
1840 window->send_cursor_position = 1;
1841
Daniel Stone97f68542012-05-30 16:32:01 +01001842 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001843
Daniel Stone97f68542012-05-30 16:32:01 +01001844 mask = xkb_state_serialize_mods(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001845 XKB_STATE_DEPRESSED |
Kristian Høgsberg70163132012-05-08 15:55:39 -04001846 XKB_STATE_LATCHED);
1847 input->modifiers = 0;
Daniel Stone97f68542012-05-30 16:32:01 +01001848 if (mask & input->xkb.control_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001849 input->modifiers |= MOD_CONTROL_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001850 if (mask & input->xkb.alt_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001851 input->modifiers |= MOD_ALT_MASK;
Daniel Stone97f68542012-05-30 16:32:01 +01001852 if (mask & input->xkb.shift_mask)
Kristian Høgsberg70163132012-05-08 15:55:39 -04001853 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001854
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001855 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001856 input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01001857 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001858 window_set_maximized(window,
1859 window->type != TYPE_MAXIMIZED);
1860 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001861 if (num_syms == 1)
1862 sym = syms[0];
1863 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001864 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001865
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001866 (*window->key_handler)(window, input, time, key,
1867 sym, state, window->user_data);
1868 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001869}
1870
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001871static void
Daniel Stone351eb612012-05-31 15:27:47 -04001872keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1873 uint32_t serial, uint32_t mods_depressed,
1874 uint32_t mods_latched, uint32_t mods_locked,
1875 uint32_t group)
1876{
1877 struct input *input = data;
1878
Daniel Stone97f68542012-05-30 16:32:01 +01001879 xkb_state_update_mask(input->xkb.state,
Daniel Stone351eb612012-05-31 15:27:47 -04001880 mods_depressed,
1881 mods_latched,
1882 mods_locked,
1883 0,
1884 0,
1885 group);
1886}
1887
1888static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001889input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001890{
1891 struct window *window = input->pointer_focus;
1892
1893 if (!window)
1894 return;
1895
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001896 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001897
Pekka Paalanene1207c72011-12-16 12:02:09 +02001898 input->pointer_focus = NULL;
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03001899 input->current_cursor = CURSOR_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001900}
1901
1902static void
Daniel Stone37816df2012-05-16 18:45:18 +01001903pointer_handle_enter(void *data, struct wl_pointer *pointer,
1904 uint32_t serial, struct wl_surface *surface,
1905 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001906{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001907 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001908 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001909 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001910 float sx = wl_fixed_to_double(sx_w);
1911 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001912
Martin Minarik1998b152012-05-10 02:04:35 +02001913 if (!surface) {
1914 /* enter event for a window we've just destroyed */
1915 return;
1916 }
1917
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 input->display->serial = serial;
1919 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001920 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001921 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001922
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001923 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001924 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001925 window->pool = NULL;
1926 /* Schedule a redraw to free the pool */
1927 window_schedule_redraw(window);
1928 }
1929
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001930 input->sx = sx;
1931 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001932
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001933 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001934 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001935}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001936
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001937static void
Daniel Stone37816df2012-05-16 18:45:18 +01001938pointer_handle_leave(void *data, struct wl_pointer *pointer,
1939 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001940{
1941 struct input *input = data;
1942
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001943 input->display->serial = serial;
1944 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001945}
1946
1947static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001948input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001949{
1950 struct window *window = input->keyboard_focus;
1951
1952 if (!window)
1953 return;
1954
1955 window->keyboard_device = NULL;
1956 if (window->keyboard_focus_handler)
1957 (*window->keyboard_focus_handler)(window, NULL,
1958 window->user_data);
1959
1960 input->keyboard_focus = NULL;
1961}
1962
1963static void
Daniel Stone37816df2012-05-16 18:45:18 +01001964keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1965 uint32_t serial, struct wl_surface *surface,
1966 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001967{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001968 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001969 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001970
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001971 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001972 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001973
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001974 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001975 window->keyboard_device = input;
1976 if (window->keyboard_focus_handler)
1977 (*window->keyboard_focus_handler)(window,
1978 window->keyboard_device,
1979 window->user_data);
1980}
1981
1982static void
Daniel Stone37816df2012-05-16 18:45:18 +01001983keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1984 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001985{
1986 struct input *input = data;
1987
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001988 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001989 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001990}
1991
Daniel Stone37816df2012-05-16 18:45:18 +01001992static const struct wl_pointer_listener pointer_listener = {
1993 pointer_handle_enter,
1994 pointer_handle_leave,
1995 pointer_handle_motion,
1996 pointer_handle_button,
1997 pointer_handle_axis,
1998};
1999
2000static const struct wl_keyboard_listener keyboard_listener = {
2001 keyboard_handle_enter,
2002 keyboard_handle_leave,
2003 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002004 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002005};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002006
2007static void
Daniel Stone37816df2012-05-16 18:45:18 +01002008seat_handle_capabilities(void *data, struct wl_seat *seat,
2009 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002010{
Daniel Stone37816df2012-05-16 18:45:18 +01002011 struct input *input = data;
2012
2013 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2014 input->pointer = wl_seat_get_pointer(seat);
2015 wl_pointer_set_user_data(input->pointer, input);
2016 wl_pointer_add_listener(input->pointer, &pointer_listener,
2017 input);
2018 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2019 wl_pointer_destroy(input->pointer);
2020 input->pointer = NULL;
2021 }
2022
2023 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2024 input->keyboard = wl_seat_get_keyboard(seat);
2025 wl_keyboard_set_user_data(input->keyboard, input);
2026 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2027 input);
2028 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2029 wl_keyboard_destroy(input->keyboard);
2030 input->keyboard = NULL;
2031 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002032}
2033
Daniel Stone37816df2012-05-16 18:45:18 +01002034static const struct wl_seat_listener seat_listener = {
2035 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002036};
2037
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002038void
2039input_get_position(struct input *input, int32_t *x, int32_t *y)
2040{
2041 *x = input->sx;
2042 *y = input->sy;
2043}
2044
Daniel Stone37816df2012-05-16 18:45:18 +01002045struct wl_seat *
2046input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002047{
Daniel Stone37816df2012-05-16 18:45:18 +01002048 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002049}
2050
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002051uint32_t
2052input_get_modifiers(struct input *input)
2053{
2054 return input->modifiers;
2055}
2056
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002057struct widget *
2058input_get_focus_widget(struct input *input)
2059{
2060 return input->focus_widget;
2061}
2062
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002063struct data_offer {
2064 struct wl_data_offer *offer;
2065 struct input *input;
2066 struct wl_array types;
2067 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002068
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002069 struct task io_task;
2070 int fd;
2071 data_func_t func;
2072 int32_t x, y;
2073 void *user_data;
2074};
2075
2076static void
2077data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2078{
2079 struct data_offer *offer = data;
2080 char **p;
2081
2082 p = wl_array_add(&offer->types, sizeof *p);
2083 *p = strdup(type);
2084}
2085
2086static const struct wl_data_offer_listener data_offer_listener = {
2087 data_offer_offer,
2088};
2089
2090static void
2091data_offer_destroy(struct data_offer *offer)
2092{
2093 char **p;
2094
2095 offer->refcount--;
2096 if (offer->refcount == 0) {
2097 wl_data_offer_destroy(offer->offer);
2098 for (p = offer->types.data; *p; p++)
2099 free(*p);
2100 wl_array_release(&offer->types);
2101 free(offer);
2102 }
2103}
2104
2105static void
2106data_device_data_offer(void *data,
2107 struct wl_data_device *data_device, uint32_t id)
2108{
2109 struct data_offer *offer;
2110
2111 offer = malloc(sizeof *offer);
2112
2113 wl_array_init(&offer->types);
2114 offer->refcount = 1;
2115 offer->input = data;
2116
2117 /* FIXME: Generate typesafe wrappers for this */
2118 offer->offer = (struct wl_data_offer *)
2119 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2120 id, &wl_data_offer_interface);
2121
2122 wl_data_offer_add_listener(offer->offer,
2123 &data_offer_listener, offer);
2124}
2125
2126static void
2127data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002129 wl_fixed_t x_w, wl_fixed_t y_w,
2130 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002131{
2132 struct input *input = data;
2133 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002134 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002135 float x = wl_fixed_to_double(x_w);
2136 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002137 char **p;
2138
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002139 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002140 window = wl_surface_get_user_data(surface);
2141 input->pointer_focus = window;
2142
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002143 if (offer) {
2144 input->drag_offer = wl_data_offer_get_user_data(offer);
2145
2146 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2147 *p = NULL;
2148
2149 types_data = input->drag_offer->types.data;
2150 } else {
2151 input->drag_offer = NULL;
2152 types_data = NULL;
2153 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002154
2155 window = input->pointer_focus;
2156 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002157 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002158 window->user_data);
2159}
2160
2161static void
2162data_device_leave(void *data, struct wl_data_device *data_device)
2163{
2164 struct input *input = data;
2165
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002166 if (input->drag_offer) {
2167 data_offer_destroy(input->drag_offer);
2168 input->drag_offer = NULL;
2169 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002170}
2171
2172static void
2173data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002174 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175{
2176 struct input *input = data;
2177 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002178 float x = wl_fixed_to_double(x_w);
2179 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002180 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181
2182 input->sx = x;
2183 input->sy = y;
2184
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002185 if (input->drag_offer)
2186 types_data = input->drag_offer->types.data;
2187 else
2188 types_data = NULL;
2189
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002190 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03002191 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002192 window->user_data);
2193}
2194
2195static void
2196data_device_drop(void *data, struct wl_data_device *data_device)
2197{
2198 struct input *input = data;
2199 struct window *window = input->pointer_focus;
2200
2201 if (window->drop_handler)
2202 window->drop_handler(window, input,
2203 input->sx, input->sy, window->user_data);
2204}
2205
2206static void
2207data_device_selection(void *data,
2208 struct wl_data_device *wl_data_device,
2209 struct wl_data_offer *offer)
2210{
2211 struct input *input = data;
2212 char **p;
2213
2214 if (input->selection_offer)
2215 data_offer_destroy(input->selection_offer);
2216
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002217 if (offer) {
2218 input->selection_offer = wl_data_offer_get_user_data(offer);
2219 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2220 *p = NULL;
2221 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002222}
2223
2224static const struct wl_data_device_listener data_device_listener = {
2225 data_device_data_offer,
2226 data_device_enter,
2227 data_device_leave,
2228 data_device_motion,
2229 data_device_drop,
2230 data_device_selection
2231};
2232
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002233void
2234input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2235{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002236 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002237 struct wl_cursor *cursor;
2238 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002240 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002241 return;
2242
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03002243 cursor = input->display->cursors[pointer];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002244 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002245 return;
2246
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002247 image = cursor->images[0];
2248 buffer = wl_cursor_image_get_buffer(image);
2249 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002250 return;
2251
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002252 input->current_cursor = pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002253 wl_pointer_attach(input->pointer, time, buffer,
2254 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002255}
2256
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257struct wl_data_device *
2258input_get_data_device(struct input *input)
2259{
2260 return input->data_device;
2261}
2262
2263void
2264input_set_selection(struct input *input,
2265 struct wl_data_source *source, uint32_t time)
2266{
2267 wl_data_device_set_selection(input->data_device, source, time);
2268}
2269
2270void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002271input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002272{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002273 wl_data_offer_accept(input->drag_offer->offer,
2274 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002275}
2276
2277static void
2278offer_io_func(struct task *task, uint32_t events)
2279{
2280 struct data_offer *offer =
2281 container_of(task, struct data_offer, io_task);
2282 unsigned int len;
2283 char buffer[4096];
2284
2285 len = read(offer->fd, buffer, sizeof buffer);
2286 offer->func(buffer, len,
2287 offer->x, offer->y, offer->user_data);
2288
2289 if (len == 0) {
2290 close(offer->fd);
2291 data_offer_destroy(offer);
2292 }
2293}
2294
2295static void
2296data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2297 data_func_t func, void *user_data)
2298{
2299 int p[2];
2300
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002301 if (pipe2(p, O_CLOEXEC) == -1)
2302 return;
2303
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002304 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2305 close(p[1]);
2306
2307 offer->io_task.run = offer_io_func;
2308 offer->fd = p[0];
2309 offer->func = func;
2310 offer->refcount++;
2311 offer->user_data = user_data;
2312
2313 display_watch_fd(offer->input->display,
2314 offer->fd, EPOLLIN, &offer->io_task);
2315}
2316
2317void
2318input_receive_drag_data(struct input *input, const char *mime_type,
2319 data_func_t func, void *data)
2320{
2321 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2322 input->drag_offer->x = input->sx;
2323 input->drag_offer->y = input->sy;
2324}
2325
2326int
2327input_receive_selection_data(struct input *input, const char *mime_type,
2328 data_func_t func, void *data)
2329{
2330 char **p;
2331
2332 if (input->selection_offer == NULL)
2333 return -1;
2334
2335 for (p = input->selection_offer->types.data; *p; p++)
2336 if (strcmp(mime_type, *p) == 0)
2337 break;
2338
2339 if (*p == NULL)
2340 return -1;
2341
2342 data_offer_receive_data(input->selection_offer,
2343 mime_type, func, data);
2344 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002345}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002346
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002347int
2348input_receive_selection_data_to_fd(struct input *input,
2349 const char *mime_type, int fd)
2350{
2351 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2352
2353 return 0;
2354}
2355
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002356void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002357window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002358{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002359 if (!window->shell_surface)
2360 return;
2361
Daniel Stone37816df2012-05-16 18:45:18 +01002362 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002363}
2364
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002365static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002366idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002367{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002368 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002369
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002370 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002371 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002372 widget_set_allocation(widget,
2373 window->pending_allocation.x,
2374 window->pending_allocation.y,
2375 window->pending_allocation.width,
2376 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002377
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002378 if (window->input_region) {
2379 wl_region_destroy(window->input_region);
2380 window->input_region = NULL;
2381 }
2382
2383 if (window->opaque_region) {
2384 wl_region_destroy(window->opaque_region);
2385 window->opaque_region = NULL;
2386 }
2387
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002388 if (widget->resize_handler)
2389 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002390 widget->allocation.width,
2391 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002392 widget->user_data);
2393
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002394 if (window->allocation.width != widget->allocation.width ||
2395 window->allocation.height != widget->allocation.height) {
2396 window->allocation = widget->allocation;
2397 window_schedule_redraw(window);
2398 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002399}
2400
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002401void
2402window_schedule_resize(struct window *window, int width, int height)
2403{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002404 window->pending_allocation.x = 0;
2405 window->pending_allocation.y = 0;
2406 window->pending_allocation.width = width;
2407 window->pending_allocation.height = height;
2408
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002409 window->resize_needed = 1;
2410 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002411}
2412
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002413void
2414widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2415{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002416 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002417}
2418
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002419static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002420handle_ping(void *data, struct wl_shell_surface *shell_surface,
2421 uint32_t serial)
2422{
2423 wl_shell_surface_pong(shell_surface, serial);
2424}
2425
2426static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002427handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002428 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002429{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002430 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002431
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002432 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002433 return;
2434
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002435 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002436 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002437}
2438
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002439static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002440menu_destroy(struct menu *menu)
2441{
2442 widget_destroy(menu->widget);
2443 window_destroy(menu->window);
2444 free(menu);
2445}
2446
2447static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002448handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2449{
2450 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002451 struct menu *menu = window->widget->user_data;
2452
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002453 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002454 * device. Or just use wl_callback. And this really needs to
2455 * be a window vfunc that the menu can set. And we need the
2456 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002457
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002458 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002459 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002460 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002461}
2462
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002463static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002464 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002465 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002466 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002467};
2468
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002469void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002470window_get_allocation(struct window *window,
2471 struct rectangle *allocation)
2472{
2473 *allocation = window->allocation;
2474}
2475
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002476static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002477widget_redraw(struct widget *widget)
2478{
2479 struct widget *child;
2480
2481 if (widget->redraw_handler)
2482 widget->redraw_handler(widget, widget->user_data);
2483 wl_list_for_each(child, &widget->child_list, link)
2484 widget_redraw(child);
2485}
2486
2487static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002488frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2489{
2490 struct window *window = data;
2491
2492 wl_callback_destroy(callback);
2493 window->redraw_scheduled = 0;
2494 if (window->redraw_needed)
2495 window_schedule_redraw(window);
2496}
2497
2498static const struct wl_callback_listener listener = {
2499 frame_callback
2500};
2501
2502static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002503idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002504{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002505 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002506 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002507
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002508 if (window->resize_needed)
2509 idle_resize(window);
2510
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002511 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002512 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002513 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002514 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002515 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002516
2517 callback = wl_surface_frame(window->surface);
2518 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002519}
2520
2521void
2522window_schedule_redraw(struct window *window)
2523{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002524 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002525 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002526 window->redraw_task.run = idle_redraw;
2527 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002528 window->redraw_scheduled = 1;
2529 }
2530}
2531
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002532void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002533window_set_custom(struct window *window)
2534{
2535 window->type = TYPE_CUSTOM;
2536}
2537
2538void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002539window_set_fullscreen(struct window *window, int fullscreen)
2540{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002541 if (!window->display->shell)
2542 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002543
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002544 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002545 return;
2546
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002547 if (fullscreen) {
2548 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002549 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002550 wl_shell_surface_set_fullscreen(window->shell_surface,
2551 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2552 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002553 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002554 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002555 wl_shell_surface_set_toplevel(window->shell_surface);
2556 window_schedule_resize(window,
2557 window->saved_allocation.width,
2558 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002559 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002560}
2561
2562void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002563window_set_maximized(struct window *window, int maximized)
2564{
2565 if (!window->display->shell)
2566 return;
2567
2568 if ((window->type == TYPE_MAXIMIZED) == maximized)
2569 return;
2570
2571 if (window->type == TYPE_TOPLEVEL) {
2572 window->saved_allocation = window->allocation;
2573 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2574 window->type = TYPE_MAXIMIZED;
2575 } else {
2576 wl_shell_surface_set_toplevel(window->shell_surface);
2577 window->type = TYPE_TOPLEVEL;
2578 window_schedule_resize(window,
2579 window->saved_allocation.width,
2580 window->saved_allocation.height);
2581 }
2582}
2583
2584void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002585window_set_user_data(struct window *window, void *data)
2586{
2587 window->user_data = data;
2588}
2589
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002590void *
2591window_get_user_data(struct window *window)
2592{
2593 return window->user_data;
2594}
2595
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002596void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002597window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002598 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002599{
2600 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002601}
2602
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002603void
2604window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002605 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002606{
2607 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002608}
2609
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002610void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002611window_set_data_handler(struct window *window, window_data_handler_t handler)
2612{
2613 window->data_handler = handler;
2614}
2615
2616void
2617window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2618{
2619 window->drop_handler = handler;
2620}
2621
2622void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002623window_set_close_handler(struct window *window,
2624 window_close_handler_t handler)
2625{
2626 window->close_handler = handler;
2627}
2628
2629void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002630window_set_title(struct window *window, const char *title)
2631{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002632 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002633 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002634 if (window->shell_surface)
2635 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002636}
2637
2638const char *
2639window_get_title(struct window *window)
2640{
2641 return window->title;
2642}
2643
2644void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002645window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2646{
2647 struct text_cursor_position *text_cursor_position =
2648 window->display->text_cursor_position;
2649
2650 if (!window->send_cursor_position || !text_cursor_position)
2651 return;
2652
2653 text_cursor_position_notify(text_cursor_position,
2654 window->surface, x, y);
2655
2656 window->send_cursor_position = 0;
2657}
2658
2659void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002660window_damage(struct window *window, int32_t x, int32_t y,
2661 int32_t width, int32_t height)
2662{
2663 wl_surface_damage(window->surface, x, y, width, height);
2664}
2665
Casey Dahlin9074db52012-04-19 22:50:09 -04002666static void
2667surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002668 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002669{
Rob Bradford7507b572012-05-15 17:55:34 +01002670 struct window *window = data;
2671 struct output *output;
2672 struct output *output_found = NULL;
2673 struct window_output *window_output;
2674
2675 wl_list_for_each(output, &window->display->output_list, link) {
2676 if (output->output == wl_output) {
2677 output_found = output;
2678 break;
2679 }
2680 }
2681
2682 if (!output_found)
2683 return;
2684
2685 window_output = malloc (sizeof *window_output);
2686 window_output->output = output_found;
2687
2688 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002689}
2690
2691static void
2692surface_leave(void *data,
2693 struct wl_surface *wl_surface, struct wl_output *output)
2694{
Rob Bradford7507b572012-05-15 17:55:34 +01002695 struct window *window = data;
2696 struct window_output *window_output;
2697 struct window_output *window_output_found = NULL;
2698
2699 wl_list_for_each(window_output, &window->window_output_list, link) {
2700 if (window_output->output->output == output) {
2701 window_output_found = window_output;
2702 break;
2703 }
2704 }
2705
2706 if (window_output_found) {
2707 wl_list_remove(&window_output_found->link);
2708 free(window_output_found);
2709 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002710}
2711
2712static const struct wl_surface_listener surface_listener = {
2713 surface_enter,
2714 surface_leave
2715};
2716
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002717static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002718window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002719{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002720 struct window *window;
2721
2722 window = malloc(sizeof *window);
2723 if (window == NULL)
2724 return NULL;
2725
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002726 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002727 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002728 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002729 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002730 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002731 if (display->shell) {
2732 window->shell_surface =
2733 wl_shell_get_shell_surface(display->shell,
2734 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002735 if (window->title)
2736 wl_shell_surface_set_title(window->shell_surface,
2737 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002738 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002739 window->allocation.x = 0;
2740 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002741 window->allocation.width = 0;
2742 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002743 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002744 window->transparent = 1;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002745 window->send_cursor_position = 0;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002746 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002747 window->input_region = NULL;
2748 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002749
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002750 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002751#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002752 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002753#else
2754 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2755#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002756 else
2757 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002758
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002759 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002760 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002761 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002762
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002763 if (window->shell_surface) {
2764 wl_shell_surface_set_user_data(window->shell_surface, window);
2765 wl_shell_surface_add_listener(window->shell_surface,
2766 &shell_surface_listener, window);
2767 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002768
Rob Bradford7507b572012-05-15 17:55:34 +01002769 wl_list_init (&window->window_output_list);
2770
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002771 return window;
2772}
2773
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002774struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002775window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002776{
2777 struct window *window;
2778
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002779 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002780 if (!window)
2781 return NULL;
2782
2783 return window;
2784}
2785
2786struct window *
2787window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03002788 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002789{
2790 struct window *window;
2791
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002792 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002793 if (!window)
2794 return NULL;
2795
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002796 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002797 window->x = x;
2798 window->y = y;
2799
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002800 if (display->shell)
2801 wl_shell_surface_set_transient(window->shell_surface,
2802 window->parent->shell_surface,
Tiago Vignattidec76582012-05-21 16:47:46 +03002803 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002804
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002805 return window;
2806}
2807
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002808static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002809menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002810{
2811 int next;
2812
2813 next = (sy - 8) / 20;
2814 if (menu->current != next) {
2815 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002816 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002817 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002818}
2819
2820static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002821menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002822 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002823 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002824{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002825 struct menu *menu = data;
2826
2827 if (widget == menu->widget)
2828 menu_set_item(data, y);
2829
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002830 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002831}
2832
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002833static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002834menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002835 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002836{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002837 struct menu *menu = data;
2838
2839 if (widget == menu->widget)
2840 menu_set_item(data, y);
2841
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002842 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002843}
2844
2845static void
2846menu_leave_handler(struct widget *widget, struct input *input, void *data)
2847{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002848 struct menu *menu = data;
2849
2850 if (widget == menu->widget)
2851 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002852}
2853
2854static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002855menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002856 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 uint32_t button, enum wl_pointer_button_state state,
2858 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002859
2860{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002861 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002862
Daniel Stone4dbadb12012-05-30 16:31:51 +01002863 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2864 time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002865 /* Either relase after press-drag-release or
2866 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002867 menu->func(menu->window->parent,
2868 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002869 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002870 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002871 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002872}
2873
2874static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002875menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002876{
2877 cairo_t *cr;
2878 const int32_t r = 3, margin = 3;
2879 struct menu *menu = data;
2880 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002881 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002882
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002883 cr = cairo_create(window->cairo_surface);
2884 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2885 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2886 cairo_paint(cr);
2887
2888 width = window->allocation.width;
2889 height = window->allocation.height;
2890 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002891
2892 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002893 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2894 cairo_fill(cr);
2895
2896 for (i = 0; i < menu->count; i++) {
2897 if (i == menu->current) {
2898 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2899 cairo_rectangle(cr, margin, i * 20 + margin,
2900 width - 2 * margin, 20);
2901 cairo_fill(cr);
2902 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2903 cairo_move_to(cr, 10, i * 20 + 16);
2904 cairo_show_text(cr, menu->entries[i]);
2905 } else {
2906 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2907 cairo_move_to(cr, 10, i * 20 + 16);
2908 cairo_show_text(cr, menu->entries[i]);
2909 }
2910 }
2911
2912 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002913}
2914
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002915void
2916window_show_menu(struct display *display,
2917 struct input *input, uint32_t time, struct window *parent,
2918 int32_t x, int32_t y,
2919 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002920{
2921 struct window *window;
2922 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002923 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002924
2925 menu = malloc(sizeof *menu);
2926 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002927 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002928
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002929 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002930 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002931 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002932
2933 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002934 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002935 menu->entries = entries;
2936 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002937 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002938 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002939 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002940 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002941 window->type = TYPE_MENU;
2942 window->x = x;
2943 window->y = y;
2944
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002945 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01002946 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002947 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002948 window->parent->shell_surface,
2949 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002950
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002951 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002952 widget_set_enter_handler(menu->widget, menu_enter_handler);
2953 widget_set_leave_handler(menu->widget, menu_leave_handler);
2954 widget_set_motion_handler(menu->widget, menu_motion_handler);
2955 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002956
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002957 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002958 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002959}
2960
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002961void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002962window_set_buffer_type(struct window *window, enum window_buffer_type type)
2963{
2964 window->buffer_type = type;
2965}
2966
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002967
2968static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002969display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002970 struct wl_output *wl_output,
2971 int x, int y,
2972 int physical_width,
2973 int physical_height,
2974 int subpixel,
2975 const char *make,
2976 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002977{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002978 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002979
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002980 output->allocation.x = x;
2981 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002982}
2983
2984static void
2985display_handle_mode(void *data,
2986 struct wl_output *wl_output,
2987 uint32_t flags,
2988 int width,
2989 int height,
2990 int refresh)
2991{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002992 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002993 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002994
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002995 if (flags & WL_OUTPUT_MODE_CURRENT) {
2996 output->allocation.width = width;
2997 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002998 if (display->output_configure_handler)
2999 (*display->output_configure_handler)(
3000 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003001 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003002}
3003
3004static const struct wl_output_listener output_listener = {
3005 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003006 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003007};
3008
3009static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003010display_add_output(struct display *d, uint32_t id)
3011{
3012 struct output *output;
3013
3014 output = malloc(sizeof *output);
3015 if (output == NULL)
3016 return;
3017
3018 memset(output, 0, sizeof *output);
3019 output->display = d;
3020 output->output =
3021 wl_display_bind(d->display, id, &wl_output_interface);
3022 wl_list_insert(d->output_list.prev, &output->link);
3023
3024 wl_output_add_listener(output->output, &output_listener, output);
3025}
3026
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003027static void
3028output_destroy(struct output *output)
3029{
3030 if (output->destroy_handler)
3031 (*output->destroy_handler)(output, output->user_data);
3032
3033 wl_output_destroy(output->output);
3034 wl_list_remove(&output->link);
3035 free(output);
3036}
3037
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003038void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003039display_set_output_configure_handler(struct display *display,
3040 display_output_handler_t handler)
3041{
3042 struct output *output;
3043
3044 display->output_configure_handler = handler;
3045 if (!handler)
3046 return;
3047
3048 wl_list_for_each(output, &display->output_list, link)
3049 (*display->output_configure_handler)(output,
3050 display->user_data);
3051}
3052
3053void
3054output_set_user_data(struct output *output, void *data)
3055{
3056 output->user_data = data;
3057}
3058
3059void *
3060output_get_user_data(struct output *output)
3061{
3062 return output->user_data;
3063}
3064
3065void
3066output_set_destroy_handler(struct output *output,
3067 display_output_handler_t handler)
3068{
3069 output->destroy_handler = handler;
3070 /* FIXME: implement this, once we have way to remove outputs */
3071}
3072
3073void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003074output_get_allocation(struct output *output, struct rectangle *allocation)
3075{
3076 *allocation = output->allocation;
3077}
3078
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003079struct wl_output *
3080output_get_wl_output(struct output *output)
3081{
3082 return output->output;
3083}
3084
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003085static void
Daniel Stone97f68542012-05-30 16:32:01 +01003086init_xkb(struct input *input)
3087{
3088 input->xkb.names.rules = "evdev";
3089 input->xkb.names.model = "pc105";
3090 input->xkb.names.layout = (char *) option_xkb_layout;
3091 input->xkb.names.variant = (char *) option_xkb_variant;
3092 input->xkb.names.options = (char *) option_xkb_options;
3093
3094 input->xkb.keymap = xkb_map_new_from_names(input->display->xkb_context,
3095 &input->xkb.names, 0);
3096 if (!input->xkb.keymap) {
3097 fprintf(stderr, "Failed to compile keymap\n");
3098 exit(1);
3099 }
3100
3101 input->xkb.state = xkb_state_new(input->xkb.keymap);
3102 if (!input->xkb.state) {
3103 fprintf(stderr, "Failed to create XKB state\n");
3104 exit(1);
3105 }
3106
3107 input->xkb.control_mask =
3108 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3109 input->xkb.alt_mask =
3110 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3111 input->xkb.shift_mask =
3112 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3113
3114}
3115
3116static void
3117fini_xkb(struct input *input)
3118{
3119 xkb_state_unref(input->xkb.state);
3120 xkb_map_unref(input->xkb.keymap);
3121}
3122
3123static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003124display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003125{
3126 struct input *input;
3127
3128 input = malloc(sizeof *input);
3129 if (input == NULL)
3130 return;
3131
3132 memset(input, 0, sizeof *input);
3133 input->display = d;
Daniel Stone37816df2012-05-16 18:45:18 +01003134 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003135 input->pointer_focus = NULL;
3136 input->keyboard_focus = NULL;
3137 wl_list_insert(d->input_list.prev, &input->link);
3138
Daniel Stone97f68542012-05-30 16:32:01 +01003139 init_xkb(input);
3140
Daniel Stone37816df2012-05-16 18:45:18 +01003141 wl_seat_add_listener(input->seat, &seat_listener, input);
3142 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003143
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003144 input->data_device =
3145 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01003146 input->seat);
3147 wl_data_device_add_listener(input->data_device, &data_device_listener,
3148 input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003149}
3150
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003151static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003152input_destroy(struct input *input)
3153{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003154 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003155 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003156
3157 if (input->drag_offer)
3158 data_offer_destroy(input->drag_offer);
3159
3160 if (input->selection_offer)
3161 data_offer_destroy(input->selection_offer);
3162
3163 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01003164 fini_xkb(input);
3165
Pekka Paalanene1207c72011-12-16 12:02:09 +02003166 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01003167 wl_seat_destroy(input->seat);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003168 free(input);
3169}
3170
3171static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003172display_handle_global(struct wl_display *display, uint32_t id,
3173 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003174{
3175 struct display *d = data;
3176
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003177 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003178 d->compositor =
3179 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003180 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003181 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01003182 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003183 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003184 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003185 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003186 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003187 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003188 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3189 d->data_device_manager =
3190 wl_display_bind(display, id,
3191 &wl_data_device_manager_interface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003192 } else if (strcmp(interface, "text_cursor_position") == 0) {
3193 d->text_cursor_position =
3194 wl_display_bind(display, id,
3195 &text_cursor_position_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003196 }
3197}
3198
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003199#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003200static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003201init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003202{
3203 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003204 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003205
Rob Clark6396ed32012-03-11 19:48:41 -05003206#ifdef USE_CAIRO_GLESV2
3207# define GL_BIT EGL_OPENGL_ES2_BIT
3208#else
3209# define GL_BIT EGL_OPENGL_BIT
3210#endif
3211
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003212 static const EGLint argb_cfg_attribs[] = {
3213 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003214 EGL_RED_SIZE, 1,
3215 EGL_GREEN_SIZE, 1,
3216 EGL_BLUE_SIZE, 1,
3217 EGL_ALPHA_SIZE, 1,
3218 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003219 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003220 EGL_NONE
3221 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003222
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003223#ifdef USE_CAIRO_GLESV2
3224 static const EGLint context_attribs[] = {
3225 EGL_CONTEXT_CLIENT_VERSION, 2,
3226 EGL_NONE
3227 };
3228 EGLint api = EGL_OPENGL_ES_API;
3229#else
3230 EGLint *context_attribs = NULL;
3231 EGLint api = EGL_OPENGL_API;
3232#endif
3233
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003234 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003235 if (!eglInitialize(d->dpy, &major, &minor)) {
3236 fprintf(stderr, "failed to initialize display\n");
3237 return -1;
3238 }
3239
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003240 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003241 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3242 return -1;
3243 }
3244
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003245 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3246 &d->argb_config, 1, &n) || n != 1) {
3247 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003248 return -1;
3249 }
3250
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003251 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003252 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003253 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003254 fprintf(stderr, "failed to create context\n");
3255 return -1;
3256 }
3257
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003258 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003259 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003260 return -1;
3261 }
3262
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003263#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003264 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3265 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3266 fprintf(stderr, "failed to get cairo egl argb device\n");
3267 return -1;
3268 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003269#endif
3270
3271 return 0;
3272}
3273
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003274static void
3275fini_egl(struct display *display)
3276{
3277#ifdef HAVE_CAIRO_EGL
3278 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003279#endif
3280
3281 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3282 EGL_NO_CONTEXT);
3283
3284 eglTerminate(display->dpy);
3285 eglReleaseThread();
3286}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003287#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003288
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003289static int
3290event_mask_update(uint32_t mask, void *data)
3291{
3292 struct display *d = data;
3293
3294 d->mask = mask;
3295
3296 return 0;
3297}
3298
3299static void
3300handle_display_data(struct task *task, uint32_t events)
3301{
3302 struct display *display =
3303 container_of(task, struct display, display_task);
3304
3305 wl_display_iterate(display->display, display->mask);
3306}
3307
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003308struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003309display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003310{
3311 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003312
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003313 argc = parse_options(xkb_options,
3314 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003315
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003316 d = malloc(sizeof *d);
3317 if (d == NULL)
3318 return NULL;
3319
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003320 memset(d, 0, sizeof *d);
3321
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003322 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003323 if (d->display == NULL) {
3324 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003325 return NULL;
3326 }
3327
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03003328 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003329 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3330 d->display_task.run = handle_display_data;
3331 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3332
3333 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003334 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003335 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003336
Daniel Stone97f68542012-05-30 16:32:01 +01003337 d->xkb_context = xkb_context_new(0);
3338 if (!d->xkb_context) {
3339 fprintf(stderr, "failed to initialize XKB context\n");
3340 return NULL;
3341 }
3342
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003343 /* Set up listener so we'll catch all events. */
3344 wl_display_add_global_listener(d->display,
3345 display_handle_global, d);
3346
3347 /* Process connection events. */
3348 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003349#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003350 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003351 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003352#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003353
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003354 d->image_target_texture_2d =
3355 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3356 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3357 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3358
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003359 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003360
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003361 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003362
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003363 wl_list_init(&d->window_list);
3364
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003365 return d;
3366}
3367
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003368static void
3369display_destroy_outputs(struct display *display)
3370{
3371 struct output *tmp;
3372 struct output *output;
3373
3374 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3375 output_destroy(output);
3376}
3377
Pekka Paalanene1207c72011-12-16 12:02:09 +02003378static void
3379display_destroy_inputs(struct display *display)
3380{
3381 struct input *tmp;
3382 struct input *input;
3383
3384 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3385 input_destroy(input);
3386}
3387
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003388void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003389display_destroy(struct display *display)
3390{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003391 if (!wl_list_empty(&display->window_list))
3392 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3393
3394 if (!wl_list_empty(&display->deferred_list))
3395 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3396
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003397 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003398 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003399
Daniel Stone97f68542012-05-30 16:32:01 +01003400 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003401
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04003402 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03003403 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003404
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003405#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003406 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003407#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003408
Pekka Paalanenc2052982011-12-16 11:41:32 +02003409 if (display->shell)
3410 wl_shell_destroy(display->shell);
3411
3412 if (display->shm)
3413 wl_shm_destroy(display->shm);
3414
3415 if (display->data_device_manager)
3416 wl_data_device_manager_destroy(display->data_device_manager);
3417
3418 wl_compositor_destroy(display->compositor);
3419
3420 close(display->epoll_fd);
3421
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003422 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003423 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003424 free(display);
3425}
3426
3427void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003428display_set_user_data(struct display *display, void *data)
3429{
3430 display->user_data = data;
3431}
3432
3433void *
3434display_get_user_data(struct display *display)
3435{
3436 return display->user_data;
3437}
3438
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003439struct wl_display *
3440display_get_display(struct display *display)
3441{
3442 return display->display;
3443}
3444
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003445struct output *
3446display_get_output(struct display *display)
3447{
3448 return container_of(display->output_list.next, struct output, link);
3449}
3450
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003451struct wl_compositor *
3452display_get_compositor(struct display *display)
3453{
3454 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003455}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003456
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003457uint32_t
3458display_get_serial(struct display *display)
3459{
3460 return display->serial;
3461}
3462
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003463EGLDisplay
3464display_get_egl_display(struct display *d)
3465{
3466 return d->dpy;
3467}
3468
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003469struct wl_data_source *
3470display_create_data_source(struct display *display)
3471{
3472 return wl_data_device_manager_create_data_source(display->data_device_manager);
3473}
3474
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003475EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003476display_get_argb_egl_config(struct display *d)
3477{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003478 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003479}
3480
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003481struct wl_shell *
3482display_get_shell(struct display *display)
3483{
3484 return display->shell;
3485}
3486
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003487int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003488display_acquire_window_surface(struct display *display,
3489 struct window *window,
3490 EGLContext ctx)
3491{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003492#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003493 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003494 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003495
3496 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003497 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003498 device = cairo_surface_get_device(window->cairo_surface);
3499 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003500 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003501
Benjamin Franzke0c991632011-09-27 21:57:31 +02003502 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003503 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003504 ctx = display->argb_ctx;
3505 else
3506 assert(0);
3507 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003508
3509 data = cairo_surface_get_user_data(window->cairo_surface,
3510 &surface_data_key);
3511
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003512 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003513 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003514 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3515 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003516
3517 return 0;
3518#else
3519 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003520#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003521}
3522
3523void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003524display_release_window_surface(struct display *display,
3525 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003526{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003527#ifdef HAVE_CAIRO_EGL
3528 cairo_device_t *device;
3529
3530 device = cairo_surface_get_device(window->cairo_surface);
3531 if (!device)
3532 return;
3533
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003534 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003535 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003536 cairo_device_release(device);
3537#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003538}
3539
3540void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003541display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003542{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003543 wl_list_insert(&display->deferred_list, &task->link);
3544}
3545
3546void
3547display_watch_fd(struct display *display,
3548 int fd, uint32_t events, struct task *task)
3549{
3550 struct epoll_event ep;
3551
3552 ep.events = events;
3553 ep.data.ptr = task;
3554 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3555}
3556
3557void
3558display_run(struct display *display)
3559{
3560 struct task *task;
3561 struct epoll_event ep[16];
3562 int i, count;
3563
Pekka Paalanen826d7952011-12-15 10:14:07 +02003564 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003565 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003566 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003567
3568 while (!wl_list_empty(&display->deferred_list)) {
3569 task = container_of(display->deferred_list.next,
3570 struct task, link);
3571 wl_list_remove(&task->link);
3572 task->run(task, 0);
3573 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003574
3575 if (!display->running)
3576 break;
3577
3578 wl_display_flush(display->display);
3579
3580 count = epoll_wait(display->epoll_fd,
3581 ep, ARRAY_LENGTH(ep), -1);
3582 for (i = 0; i < count; i++) {
3583 task = ep[i].data.ptr;
3584 task->run(task, ep[i].events);
3585 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003586 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003587}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003588
3589void
3590display_exit(struct display *display)
3591{
3592 display->running = 0;
3593}