blob: 652447f33bc781454916410083bebccd31d3f37c [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øgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.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>
Kristian Høgsberg70163132012-05-08 15:55:39 -040059#include <X11/keysym.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050060#include <X11/X.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030061#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040062
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050063#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020064#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040065#include "cairo-util.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 Oliveira1493d2a2012-05-03 12:29:46 +030069struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070070struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030071
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050073 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050074 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040076 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040077 struct wl_data_device_manager *data_device_manager;
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øgsbergdcb71b62010-06-15 17:16:35 -040096 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050097 int frame_radius;
Kristian Høgsberg70163132012-05-08 15:55:39 -040098
99 struct {
100 struct xkb_rule_names names;
101 struct xkb_keymap *keymap;
102 struct xkb_state *state;
103 struct xkb_context *context;
104 xkb_mod_mask_t control_mask;
105 xkb_mod_mask_t alt_mask;
106 xkb_mod_mask_t shift_mask;
107 } xkb;
108
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300109 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700110 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400111
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200115
116 display_output_handler_t output_configure_handler;
117
118 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500119};
120
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400122 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400124 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500125 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400126 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500127 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128 TYPE_CUSTOM
129};
130
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131struct window {
132 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500133 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500134 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200135 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500136 struct wl_region *input_region;
137 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500138 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500139 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500140 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500141 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400142 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400143 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400144 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400145 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400146 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400147 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400148 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400149 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400150 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500151
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400152 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500153
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700154 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400155
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500156 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500157 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400158 window_data_handler_t data_handler;
159 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500160 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200162 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500163 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500164
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500165 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400166 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167};
168
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500169struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500170 struct window *window;
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;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400182};
183
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400184struct input {
185 struct display *display;
186 struct wl_input_device *input_device;
187 struct window *pointer_focus;
188 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300189 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400190 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400191 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400192 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400194
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500195 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500196 struct widget *grab;
197 uint32_t grab_button;
198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400199 struct wl_data_device *data_device;
200 struct data_offer *drag_offer;
201 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400202};
203
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500204struct output {
205 struct display *display;
206 struct wl_output *output;
207 struct rectangle allocation;
208 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200209
210 display_output_handler_t destroy_handler;
211 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500212};
213
Martin Minarik1998b152012-05-10 02:04:35 +0200214enum frame_button_action {
215 FRAME_BUTTON_NULL = 0,
216 FRAME_BUTTON_ICON = 1,
217 FRAME_BUTTON_CLOSE = 2,
218 FRAME_BUTTON_MINIMIZE = 3,
219 FRAME_BUTTON_MAXIMIZE = 4,
220};
221
222enum frame_button_pointer {
223 FRAME_BUTTON_DEFAULT = 0,
224 FRAME_BUTTON_OVER = 1,
225 FRAME_BUTTON_ACTIVE = 2,
226};
227
228enum frame_button_align {
229 FRAME_BUTTON_RIGHT = 0,
230 FRAME_BUTTON_LEFT = 1,
231};
232
233enum frame_button_decoration {
234 FRAME_BUTTON_NONE = 0,
235 FRAME_BUTTON_FANCY = 1,
236};
237
238struct frame_button {
239 struct widget *widget;
240 struct frame *frame;
241 cairo_surface_t *icon;
242 enum frame_button_action type;
243 enum frame_button_pointer state;
244 struct wl_list link; /* buttons_list */
245 enum frame_button_align align;
246 enum frame_button_decoration decoration;
247};
248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500249struct frame {
250 struct widget *widget;
251 struct widget *child;
252 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400253 int width;
254 int titlebar_height;
Martin Minarik1998b152012-05-10 02:04:35 +0200255 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500256};
257
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500258struct menu {
259 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500260 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500261 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500262 const char **entries;
263 uint32_t time;
264 int current;
265 int count;
266 menu_func_t func;
267};
268
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300269struct cursor_image {
270 cairo_surface_t *surface;
271 int width, height;
272 int hotspot_x, hotspot_y;
273 int delay;
274};
275
276struct cursor {
277 int n_images;
278 struct cursor_image *images;
279};
280
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700281struct shm_pool {
282 struct wl_shm_pool *pool;
283 size_t size;
284 size_t used;
285 void *data;
286};
287
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400288enum {
289 POINTER_DEFAULT = 100,
290 POINTER_UNSET
291};
292
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500293enum window_location {
294 WINDOW_INTERIOR = 0,
295 WINDOW_RESIZING_TOP = 1,
296 WINDOW_RESIZING_BOTTOM = 2,
297 WINDOW_RESIZING_LEFT = 4,
298 WINDOW_RESIZING_TOP_LEFT = 5,
299 WINDOW_RESIZING_BOTTOM_LEFT = 6,
300 WINDOW_RESIZING_RIGHT = 8,
301 WINDOW_RESIZING_TOP_RIGHT = 9,
302 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
303 WINDOW_RESIZING_MASK = 15,
304 WINDOW_EXTERIOR = 16,
305 WINDOW_TITLEBAR = 17,
306 WINDOW_CLIENT_AREA = 18,
307};
308
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400309const char *option_xkb_layout = "us";
310const char *option_xkb_variant = "";
311const char *option_xkb_options = "";
312
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400313static const struct weston_option xkb_options[] = {
314 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
315 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
316 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400317};
318
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319static const cairo_user_data_key_t surface_data_key;
320struct surface_data {
321 struct wl_buffer *buffer;
322};
323
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500324#define MULT(_d,c,a,t) \
325 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
326
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500327#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100329struct egl_window_surface_data {
330 struct display *display;
331 struct wl_surface *surface;
332 struct wl_egl_window *window;
333 EGLSurface surf;
334};
335
336static void
337egl_window_surface_data_destroy(void *p)
338{
339 struct egl_window_surface_data *data = p;
340 struct display *d = data->display;
341
342 eglDestroySurface(d->dpy, data->surf);
343 wl_egl_window_destroy(data->window);
344 data->surface = NULL;
345
346 free(p);
347}
348
349static cairo_surface_t *
350display_create_egl_window_surface(struct display *display,
351 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400352 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100353 struct rectangle *rectangle)
354{
355 cairo_surface_t *cairo_surface;
356 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200358 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100359
360 data = malloc(sizeof *data);
361 if (data == NULL)
362 return NULL;
363
364 data->display = display;
365 data->surface = surface;
366
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500367 config = display->argb_config;
368 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400370 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100371 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400372 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100373
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500375 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100376
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100378 data->surf,
379 rectangle->width,
380 rectangle->height);
381
382 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
383 data, egl_window_surface_data_destroy);
384
385 return cairo_surface;
386}
387
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388#endif
389
390struct wl_buffer *
391display_get_buffer_for_surface(struct display *display,
392 cairo_surface_t *surface)
393{
394 struct surface_data *data;
395
396 data = cairo_surface_get_user_data (surface, &surface_data_key);
397
398 return data->buffer;
399}
400
401struct shm_surface_data {
402 struct surface_data data;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700403 struct shm_pool *pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404};
405
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500406static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700407shm_pool_destroy(struct shm_pool *pool);
408
409static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410shm_surface_data_destroy(void *p)
411{
412 struct shm_surface_data *data = p;
413
414 wl_buffer_destroy(data->data.buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415 if (data->pool)
416 shm_pool_destroy(data->pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417}
418
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300419static void
420shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
421{
422 void *dest = cairo_image_surface_get_data(surface);
423
424 memcpy(dest, data, count);
425}
426
Kristian Høgsberg16626282012-04-03 11:21:27 -0400427static struct wl_shm_pool *
428make_shm_pool(struct display *display, int size, void **data)
429{
430 char filename[] = "/tmp/wayland-shm-XXXXXX";
431 struct wl_shm_pool *pool;
432 int fd;
433
434 fd = mkstemp(filename);
435 if (fd < 0) {
436 fprintf(stderr, "open %s failed: %m\n", filename);
437 return NULL;
438 }
439 if (ftruncate(fd, size) < 0) {
440 fprintf(stderr, "ftruncate failed: %m\n");
441 close(fd);
442 return NULL;
443 }
444
445 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
446 unlink(filename);
447
448 if (*data == MAP_FAILED) {
449 fprintf(stderr, "mmap failed: %m\n");
450 close(fd);
451 return NULL;
452 }
453
454 pool = wl_shm_create_pool(display->shm, fd, size);
455
456 close(fd);
457
458 return pool;
459}
460
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700461static struct shm_pool *
462shm_pool_create(struct display *display, size_t size)
463{
464 struct shm_pool *pool = malloc(sizeof *pool);
465
466 if (!pool)
467 return NULL;
468
469 pool->pool = make_shm_pool(display, size, &pool->data);
470 if (!pool->pool) {
471 free(pool);
472 return NULL;
473 }
474
475 pool->size = size;
476 pool->used = 0;
477
478 return pool;
479}
480
481static void *
482shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
483{
484 if (pool->used + size > pool->size)
485 return NULL;
486
487 *offset = pool->used;
488 pool->used += size;
489
490 return (char *) pool->data + *offset;
491}
492
493/* destroy the pool. this does not unmap the memory though */
494static void
495shm_pool_destroy(struct shm_pool *pool)
496{
497 munmap(pool->data, pool->size);
498 wl_shm_pool_destroy(pool->pool);
499 free(pool);
500}
501
502/* Start allocating from the beginning of the pool again */
503static void
504shm_pool_reset(struct shm_pool *pool)
505{
506 pool->used = 0;
507}
508
509static int
510data_length_for_shm_surface(struct rectangle *rect)
511{
512 int stride;
513
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
515 rect->width);
516 return stride * rect->height;
517}
518
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500519static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700520display_create_shm_surface_from_pool(struct display *display,
521 struct rectangle *rectangle,
522 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523{
524 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400525 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400526 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700527 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400528 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529
530 data = malloc(sizeof *data);
531 if (data == NULL)
532 return NULL;
533
534 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
535 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700536 length = stride * rectangle->height;
537 data->pool = NULL;
538 map = shm_pool_allocate(pool, length, &offset);
539
540 if (!map) {
541 free(data);
542 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400543 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400545 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546 CAIRO_FORMAT_ARGB32,
547 rectangle->width,
548 rectangle->height,
549 stride);
550
551 cairo_surface_set_user_data (surface, &surface_data_key,
552 data, shm_surface_data_destroy);
553
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400554 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500555 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400556 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500557 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400558
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700559 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
Kristian Høgsberg16626282012-04-03 11:21:27 -0400560 rectangle->width,
561 rectangle->height,
562 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700564 return surface;
565}
566
567static cairo_surface_t *
568display_create_shm_surface(struct display *display,
569 struct rectangle *rectangle, uint32_t flags,
570 struct window *window)
571{
572 struct shm_surface_data *data;
573 struct shm_pool *pool;
574 cairo_surface_t *surface;
575
576 if (window && window->pool) {
577 shm_pool_reset(window->pool);
578 surface = display_create_shm_surface_from_pool(display,
579 rectangle,
580 flags,
581 window->pool);
582 if (surface)
583 return surface;
584 }
585
586 pool = shm_pool_create(display,
587 data_length_for_shm_surface(rectangle));
588 if (!pool)
589 return NULL;
590
591 surface =
592 display_create_shm_surface_from_pool(display, rectangle,
593 flags, pool);
594
595 if (!surface) {
596 shm_pool_destroy(pool);
597 return NULL;
598 }
599
600 /* make sure we destroy the pool when the surface is destroyed */
601 data = cairo_surface_get_user_data(surface, &surface_data_key);
602 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603
604 return surface;
605}
606
nobled7b87cb02011-02-01 18:51:47 +0000607static int
608check_size(struct rectangle *rect)
609{
610 if (rect->width && rect->height)
611 return 0;
612
613 fprintf(stderr, "tried to create surface of "
614 "width: %d, height: %d\n", rect->width, rect->height);
615 return -1;
616}
617
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618cairo_surface_t *
619display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400621 struct rectangle *rectangle,
622 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623{
nobled7b87cb02011-02-01 18:51:47 +0000624 if (check_size(rectangle) < 0)
625 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500626#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg5990fbb2012-04-10 16:55:11 -0400627 if (display->dpy)
628 return display_create_egl_window_surface(display,
629 surface,
630 flags,
631 rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400633 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634}
635
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300636static const char *cursors[] = {
637 "bottom_left_corner",
638 "bottom_right_corner",
639 "bottom_side",
640 "grabbing",
641 "left_ptr",
642 "left_side",
643 "right_side",
644 "top_left_corner",
645 "top_right_corner",
646 "top_side",
647 "xterm",
648 "hand1",
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400649};
650
651static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300652create_cursor_from_images(struct display *display, struct cursor *cursor,
653 XcursorImages *images)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300655 int i;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400656 struct rectangle rect;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300657 XcursorImage *image;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300659 cursor->images = malloc(images->nimage * sizeof *cursor->images);
660 cursor->n_images = images->nimage;
661
662 for (i = 0; i < images->nimage; i++) {
663 image = images->images[i];
664
665 rect.width = image->width;
666 rect.height = image->height;
667
668 cursor->images[i].surface =
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700669 display_create_shm_surface_from_pool(display, &rect, 0,
670 display->cursor_shm_pool);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300671
672 shm_surface_write(cursor->images[i].surface,
673 (unsigned char *) image->pixels,
674 image->width * image->height * sizeof image->pixels[0]);
675
676 cursor->images[i].width = image->width;
677 cursor->images[i].height = image->height;
678 cursor->images[i].hotspot_x = image->xhot;
679 cursor->images[i].hotspot_y = image->yhot;
680 cursor->images[i].delay = image->delay;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400681 }
682
683}
684
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685static size_t
686data_length_for_cursor_images(XcursorImages *images)
687{
688 int i;
689 size_t length = 0;
690 struct rectangle rect;
691
692 for (i = 0; i < images->nimage; i++) {
693 rect.width = images->images[i]->width;
694 rect.height = images->images[i]->height;
695 length += data_length_for_shm_surface(&rect);
696 }
697
698 return length;
699}
700
Pekka Paalanen325bb602011-12-19 10:31:45 +0200701static void
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300702create_cursors(struct display *display)
703{
704 int i, count;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700705 size_t pool_size = 0;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300706 struct cursor *cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700707 XcursorImages **images;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300708
709 count = ARRAY_LENGTH(cursors);
710 display->cursors = malloc(count * sizeof *display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700711 images = malloc(count * sizeof images[0]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300712
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700713 for (i = 0; i < count; i++) {
714 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
Rafal Mielniczuk87e4c932012-05-08 22:10:44 +0200715 if (!images[i]) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300716 fprintf(stderr, "Error loading cursor: %s\n",
717 cursors[i]);
718 continue;
719 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720 pool_size += data_length_for_cursor_images(images[i]);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300721 }
722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723 display->cursor_shm_pool = shm_pool_create(display, pool_size);
724
725 for (i = 0; i < count; i++) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726 cursor = &display->cursors[i];
Dima Ryazanovff1c2d72012-05-08 21:02:33 -0700727
728 if (!images[i]) {
729 cursor->n_images = 0;
730 cursor->images = NULL;
731 continue;
732 }
733
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734 create_cursor_from_images(display, cursor, images[i]);
735
736 XcursorImagesDestroy(images[i]);
737 }
738
739 free(images);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300740}
741
742static void
743destroy_cursor_images(struct cursor *cursor)
744{
745 int i;
746
747 for (i = 0; i < cursor->n_images; i++)
748 if (cursor->images[i].surface)
749 cairo_surface_destroy(cursor->images[i].surface);
750
751 free(cursor->images);
752}
753
754static void
755destroy_cursors(struct display *display)
Pekka Paalanen325bb602011-12-19 10:31:45 +0200756{
757 int i, count;
758
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300759 count = ARRAY_LENGTH(cursors);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200760 for (i = 0; i < count; ++i) {
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300761 destroy_cursor_images(&display->cursors[i]);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200762 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700763
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300764 free(display->cursors);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700765 shm_pool_destroy(display->cursor_shm_pool);
Pekka Paalanen325bb602011-12-19 10:31:45 +0200766}
767
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400768cairo_surface_t *
769display_get_pointer_surface(struct display *display, int pointer,
770 int *width, int *height,
771 int *hotspot_x, int *hotspot_y)
772{
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300773 struct cursor *cursor = &display->cursors[pointer];
774 cairo_surface_t *surface = cursor->images[0].surface;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400775
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300776 /* FIXME returning information for the first image. Something better
777 * is needed for animated cursors */
778
nobledf8475c92011-01-05 17:41:55 +0000779 *width = cairo_image_surface_get_width(surface);
780 *height = cairo_image_surface_get_height(surface);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300781
782 *hotspot_x = cursor->images[0].hotspot_x;
783 *hotspot_y = cursor->images[0].hotspot_y;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400784
785 return cairo_surface_reference(surface);
786}
787
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400788static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200789window_get_resize_dx_dy(struct window *window, int *x, int *y)
790{
791 if (window->resize_edges & WINDOW_RESIZING_LEFT)
792 *x = window->server_allocation.width - window->allocation.width;
793 else
794 *x = 0;
795
796 if (window->resize_edges & WINDOW_RESIZING_TOP)
797 *y = window->server_allocation.height -
798 window->allocation.height;
799 else
800 *y = 0;
801
802 window->resize_edges = 0;
803}
804
805static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500806window_attach_surface(struct window *window)
807{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400808 struct display *display = window->display;
809 struct wl_buffer *buffer;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000810#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100811 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500813 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400815 if (window->type == TYPE_NONE) {
816 window->type = TYPE_TOPLEVEL;
817 if (display->shell)
818 wl_shell_surface_set_toplevel(window->shell_surface);
819 }
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 buffer =
835 display_get_buffer_for_surface(display,
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400836 window->cairo_surface);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200838 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 wl_surface_attach(window->surface, buffer, x, y);
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 window->server_allocation = window->allocation;
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400844 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = NULL;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100846 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847 default:
848 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100849 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500850
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500851 if (window->input_region) {
852 wl_surface_set_input_region(window->surface,
853 window->input_region);
854 wl_region_destroy(window->input_region);
855 window->input_region = NULL;
856 }
857
858 if (window->opaque_region) {
859 wl_surface_set_opaque_region(window->surface,
860 window->opaque_region);
861 wl_region_destroy(window->opaque_region);
862 window->opaque_region = NULL;
863 }
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500864}
865
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500866void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400867window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500868{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400869 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100870 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871}
872
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400873void
874window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500876 cairo_surface_reference(surface);
877
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400878 if (window->cairo_surface != NULL)
879 cairo_surface_destroy(window->cairo_surface);
880
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500881 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882}
883
Benjamin Franzke22d54812011-07-16 19:50:32 +0000884#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100885static void
886window_resize_cairo_window_surface(struct window *window)
887{
888 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200889 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100890
891 data = cairo_surface_get_user_data(window->cairo_surface,
892 &surface_data_key);
893
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200894 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895 wl_egl_window_resize(data->window,
896 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 window->allocation.height,
898 x,y);
899
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900 cairo_gl_surface_set_size(window->cairo_surface,
901 window->allocation.width,
902 window->allocation.height);
903}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000904#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400906struct display *
907window_get_display(struct window *window)
908{
909 return window->display;
910}
911
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400912void
913window_create_surface(struct window *window)
914{
915 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400916 uint32_t flags = 0;
917
918 if (!window->transparent)
919 flags = SURFACE_OPAQUE;
920
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500922#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100923 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
924 if (window->cairo_surface) {
925 window_resize_cairo_window_surface(window);
926 return;
927 }
928 surface = display_create_surface(window->display,
929 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400930 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932#endif
933 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400934 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400935 &window->allocation,
936 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800938 default:
939 surface = NULL;
940 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942
943 window_set_surface(window, surface);
944 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945}
946
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200947static void frame_destroy(struct frame *frame);
948
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400949void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500950window_destroy(struct window *window)
951{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200952 struct display *display = window->display;
953 struct input *input;
954
955 if (window->redraw_scheduled)
956 wl_list_remove(&window->redraw_task.link);
957
958 wl_list_for_each(input, &display->input_list, link) {
959 if (input->pointer_focus == window)
960 input->pointer_focus = NULL;
961 if (input->keyboard_focus == window)
962 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500963 if (input->focus_widget &&
964 input->focus_widget->window == window)
965 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200966 }
967
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500968 if (window->input_region)
969 wl_region_destroy(window->input_region);
970 if (window->opaque_region)
971 wl_region_destroy(window->opaque_region);
972
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200973 if (window->frame)
974 frame_destroy(window->frame);
975
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200976 if (window->shell_surface)
977 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500978 wl_surface_destroy(window->surface);
979 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200980
981 if (window->cairo_surface != NULL)
982 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200983
984 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500985 free(window);
986}
987
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500988static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500989widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400990{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500991 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400992
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500993 wl_list_for_each(child, &widget->child_list, link) {
994 target = widget_find_widget(child, x, y);
995 if (target)
996 return target;
997 }
998
999 if (widget->allocation.x <= x &&
1000 x < widget->allocation.x + widget->allocation.width &&
1001 widget->allocation.y <= y &&
1002 y < widget->allocation.y + widget->allocation.height) {
1003 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004 }
1005
1006 return NULL;
1007}
1008
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001009static struct widget *
1010widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001013
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014 widget = malloc(sizeof *widget);
1015 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001016 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001017 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001018 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001019 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001020 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021
1022 return widget;
1023}
1024
1025struct widget *
1026window_add_widget(struct window *window, void *data)
1027{
1028 window->widget = widget_create(window, data);
1029 wl_list_init(&window->widget->link);
1030
1031 return window->widget;
1032}
1033
1034struct widget *
1035widget_add_widget(struct widget *parent, void *data)
1036{
1037 struct widget *widget;
1038
1039 widget = widget_create(parent->window, data);
1040 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043}
1044
1045void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001046widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001047{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001048 struct display *display = widget->window->display;
1049 struct input *input;
1050
1051 wl_list_for_each(input, &display->input_list, link) {
1052 if (input->focus_widget == widget)
1053 input->focus_widget = NULL;
1054 }
1055
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001056 wl_list_remove(&widget->link);
1057 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058}
1059
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064}
1065
1066void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001067widget_set_size(struct widget *widget, int32_t width, int32_t height)
1068{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001069 widget->allocation.width = width;
1070 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001071}
1072
1073void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074widget_set_allocation(struct widget *widget,
1075 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077 widget->allocation.x = x;
1078 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001079 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001082void
1083widget_set_transparent(struct widget *widget, int transparent)
1084{
1085 widget->opaque = !transparent;
1086}
1087
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001090{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001091 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092}
1093
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001094void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001095widget_set_resize_handler(struct widget *widget,
1096 widget_resize_handler_t handler)
1097{
1098 widget->resize_handler = handler;
1099}
1100
1101void
1102widget_set_redraw_handler(struct widget *widget,
1103 widget_redraw_handler_t handler)
1104{
1105 widget->redraw_handler = handler;
1106}
1107
1108void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001109widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001110{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001111 widget->enter_handler = handler;
1112}
1113
1114void
1115widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1116{
1117 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001118}
1119
1120void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001121widget_set_motion_handler(struct widget *widget,
1122 widget_motion_handler_t handler)
1123{
1124 widget->motion_handler = handler;
1125}
1126
1127void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001128widget_set_button_handler(struct widget *widget,
1129 widget_button_handler_t handler)
1130{
1131 widget->button_handler = handler;
1132}
1133
1134void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001135widget_schedule_redraw(struct widget *widget)
1136{
1137 window_schedule_redraw(widget->window);
1138}
1139
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001140cairo_surface_t *
1141window_get_surface(struct window *window)
1142{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001143 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001144}
1145
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001146struct wl_surface *
1147window_get_wl_surface(struct window *window)
1148{
1149 return window->surface;
1150}
1151
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001152struct wl_shell_surface *
1153window_get_wl_shell_surface(struct window *window)
1154{
1155 return window->shell_surface;
1156}
1157
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001158static void
1159frame_resize_handler(struct widget *widget,
1160 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001162 struct frame *frame = data;
1163 struct widget *child = frame->child;
1164 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001165 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001166 struct frame_button * button;
1167 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001169 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001170
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001171 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001172 decoration_width = (frame->width + frame->margin) * 2;
1173 decoration_height = frame->width +
1174 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001175
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001176 allocation.x = frame->width + frame->margin;
1177 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001178 allocation.width = width - decoration_width;
1179 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001180
1181 widget->window->input_region =
1182 wl_compositor_create_region(display->compositor);
1183 wl_region_add(widget->window->input_region,
1184 frame->margin, frame->margin,
1185 width - 2 * frame->margin,
1186 height - 2 * frame->margin);
1187
1188 opaque_margin = frame->margin + display->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001189
1190 wl_list_for_each(button, &frame->buttons_list, link)
1191 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001192 } else {
1193 decoration_width = 0;
1194 decoration_height = 0;
1195
1196 allocation.x = 0;
1197 allocation.y = 0;
1198 allocation.width = width;
1199 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001200 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001201
1202 wl_list_for_each(button, &frame->buttons_list, link)
1203 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001204 }
1205
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001206 widget_set_allocation(child, allocation.x, allocation.y,
1207 allocation.width, allocation.height);
1208
1209 if (child->resize_handler)
1210 child->resize_handler(child,
1211 allocation.width,
1212 allocation.height,
1213 child->user_data);
1214
1215 widget_set_allocation(widget, 0, 0,
1216 child->allocation.width + decoration_width,
1217 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001218
1219 if (child->opaque) {
1220 widget->window->opaque_region =
1221 wl_compositor_create_region(display->compositor);
1222 wl_region_add(widget->window->opaque_region,
1223 opaque_margin, opaque_margin,
1224 widget->allocation.width - 2 * opaque_margin,
1225 widget->allocation.height - 2 * opaque_margin);
1226 }
Martin Minarik1998b152012-05-10 02:04:35 +02001227
1228 /* frame internal buttons */
1229 x_r = frame->widget->allocation.width - frame->width - frame->margin;
1230 x_l = frame->width + frame->margin;
1231 y = frame->width + frame->margin;
1232 wl_list_for_each(button, &frame->buttons_list, link) {
1233 const int button_padding = 4;
1234 w = cairo_image_surface_get_width(button->icon);
1235 h = cairo_image_surface_get_height(button->icon);
1236
1237 if (button->decoration == FRAME_BUTTON_FANCY)
1238 w += 10;
1239
1240 if (button->align == FRAME_BUTTON_LEFT) {
1241 widget_set_allocation(button->widget,
1242 x_l, y , w + 1, h + 1);
1243 x_l += w;
1244 x_l += button_padding;
1245 } else {
1246 x_r -= w;
1247 widget_set_allocation(button->widget,
1248 x_r, y , w + 1, h + 1);
1249 x_r -= button_padding;
1250 }
1251 }
1252}
1253
1254static int
1255frame_button_enter_handler(struct widget *widget,
1256 struct input *input, float x, float y, void *data)
1257{
1258 struct frame_button *frame_button = data;
1259
1260 widget_schedule_redraw(frame_button->widget);
1261 frame_button->state = FRAME_BUTTON_OVER;
1262
1263 return POINTER_LEFT_PTR;
1264}
1265
1266static void
1267frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1268{
1269 struct frame_button *frame_button = data;
1270
1271 widget_schedule_redraw(frame_button->widget);
1272 frame_button->state = FRAME_BUTTON_DEFAULT;
1273}
1274
1275static void
1276frame_button_button_handler(struct widget *widget,
1277 struct input *input, uint32_t time,
1278 uint32_t button, uint32_t state, void *data)
1279{
1280 struct frame_button *frame_button = data;
1281 struct window *window = widget->window;
1282
1283 if (button != BTN_LEFT)
1284 return;
1285
1286 switch (state) {
1287 case 1:
1288 frame_button->state = FRAME_BUTTON_ACTIVE;
1289 widget_schedule_redraw(frame_button->widget);
1290
1291 if (frame_button->type == FRAME_BUTTON_ICON)
1292 window_show_frame_menu(window, input, time);
1293 return;
1294 case 0:
1295 frame_button->state = FRAME_BUTTON_DEFAULT;
1296 widget_schedule_redraw(frame_button->widget);
1297 break;
1298 }
1299
1300 switch (frame_button->type) {
1301 case FRAME_BUTTON_CLOSE:
1302 if (window->close_handler)
1303 window->close_handler(window->parent,
1304 window->user_data);
1305 else
1306 display_exit(window->display);
1307 break;
1308 case FRAME_BUTTON_MINIMIZE:
1309 fprintf(stderr,"Minimize stub\n");
1310 break;
1311 case FRAME_BUTTON_MAXIMIZE:
1312 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1313 break;
1314 default:
1315 /* Unknown operation */
1316 break;
1317 }
1318}
1319
1320static void
1321frame_button_redraw_handler(struct widget *widget, void *data)
1322{
1323 struct frame_button *frame_button = data;
1324 cairo_t *cr;
1325 int width, height, x, y;
1326 struct window *window = widget->window;
1327
1328 x = widget->allocation.x;
1329 y = widget->allocation.y;
1330 width = widget->allocation.width;
1331 height = widget->allocation.height;
1332
1333 if (!width)
1334 return;
1335 if (!height)
1336 return;
1337 if (widget->opaque)
1338 return;
1339
1340 cr = cairo_create(window->cairo_surface);
1341
1342 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1343 cairo_set_line_width(cr, 1);
1344
1345 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1346 cairo_rectangle (cr, x, y, 25, 16);
1347
1348 cairo_stroke_preserve(cr);
1349
1350 switch (frame_button->state) {
1351 case FRAME_BUTTON_DEFAULT:
1352 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1353 break;
1354 case FRAME_BUTTON_OVER:
1355 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1356 break;
1357 case FRAME_BUTTON_ACTIVE:
1358 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1359 break;
1360 }
1361
1362 cairo_fill (cr);
1363
1364 x += 4;
1365 }
1366
1367 cairo_set_source_surface(cr, frame_button->icon, x, y);
1368 cairo_paint(cr);
1369
1370 cairo_destroy(cr);
1371}
1372
1373static struct widget *
1374frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1375 enum frame_button_align align, enum frame_button_decoration style)
1376{
1377 struct frame_button *frame_button;
1378 const char *icon = data;
1379
1380 frame_button = malloc (sizeof *frame_button);
1381 memset(frame_button, 0, sizeof *frame_button);
1382
1383 frame_button->icon = cairo_image_surface_create_from_png(icon);
1384 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1385 frame_button->frame = frame;
1386 frame_button->type = type;
1387 frame_button->align = align;
1388 frame_button->decoration = style;
1389
1390 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1391
1392 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1393 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1394 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1395 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1396 return frame_button->widget;
1397}
1398
1399static void
1400frame_button_destroy(struct frame_button *frame_button)
1401{
1402 widget_destroy(frame_button->widget);
1403 wl_list_remove(&frame_button->link);
1404 cairo_surface_destroy(frame_button->icon);
1405 free(frame_button);
1406
1407 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001408}
1409
1410static void
1411frame_redraw_handler(struct widget *widget, void *data)
1412{
1413 struct frame *frame = data;
1414 cairo_t *cr;
1415 cairo_text_extents_t extents;
1416 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001417 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001418 struct window *window = widget->window;
1419
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001420 if (window->type == TYPE_FULLSCREEN)
1421 return;
1422
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001423 width = widget->allocation.width;
1424 height = widget->allocation.height;
1425
1426 cr = cairo_create(window->cairo_surface);
1427
1428 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1429 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1430 cairo_paint(cr);
1431
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001432 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001433 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001434 2, 2, width + 8, height + 8,
1435 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001436
1437 if (window->keyboard_device)
1438 source = window->display->active_frame;
1439 else
1440 source = window->display->inactive_frame;
1441
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001442 tile_source(cr, source,
1443 frame->margin, frame->margin,
1444 width - frame->margin * 2, height - frame->margin * 2,
1445 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001446
1447 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001448 cairo_select_font_face(cr, "sans",
1449 CAIRO_FONT_SLANT_NORMAL,
1450 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001451 cairo_set_font_size(cr, 14);
1452 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001453 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001454 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001455 if (window->keyboard_device) {
1456 cairo_move_to(cr, x + 1, y + 1);
1457 cairo_set_source_rgb(cr, 1, 1, 1);
1458 cairo_show_text(cr, window->title);
1459 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001460 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001461 cairo_show_text(cr, window->title);
1462 } else {
1463 cairo_move_to(cr, x, y);
1464 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1465 cairo_show_text(cr, window->title);
1466 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001467
1468 cairo_destroy(cr);
1469}
1470
1471static int
1472frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1473{
1474 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001475 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001476 const int grip_size = 8;
1477
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001478 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001480 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001481 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001483 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001484 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001485 hlocation = WINDOW_RESIZING_RIGHT;
1486 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001487 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001488
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001489 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001490 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001491 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001492 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001493 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001494 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001495 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001496 vlocation = WINDOW_RESIZING_BOTTOM;
1497 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001498 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001499
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001500 location = vlocation | hlocation;
1501 if (location & WINDOW_EXTERIOR)
1502 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001504 location = WINDOW_TITLEBAR;
1505 else if (location == WINDOW_INTERIOR)
1506 location = WINDOW_CLIENT_AREA;
1507
1508 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001509}
1510
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001511static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001512frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001513{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001514 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001516 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001517 switch (location) {
1518 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001519 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001520 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001521 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001522 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001523 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001524 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001525 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001526 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001527 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001528 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001529 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001530 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001531 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001532 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001533 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001534 case WINDOW_EXTERIOR:
1535 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001537 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539}
1540
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001541static void
1542frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001543{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001544 switch (index) {
1545 case 0: /* close */
1546 if (window->close_handler)
1547 window->close_handler(window->parent,
1548 window->user_data);
1549 else
1550 display_exit(window->display);
1551 break;
1552 case 1: /* fullscreen */
1553 /* we don't have a way to get out of fullscreen for now */
1554 window_set_fullscreen(window, 1);
1555 break;
1556 case 2: /* rotate */
1557 case 3: /* scale */
1558 break;
1559 }
1560}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001561
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001562void
1563window_show_frame_menu(struct window *window,
1564 struct input *input, uint32_t time)
1565{
1566 int32_t x, y;
1567
1568 static const char *entries[] = {
1569 "Close", "Fullscreen", "Rotate", "Scale"
1570 };
1571
1572 input_get_position(input, &x, &y);
1573 window_show_menu(window->display, input, time, window,
1574 x - 10, y - 10, frame_menu_func, entries, 4);
1575}
1576
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001577static int
1578frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001579 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001580{
1581 return frame_get_pointer_image_for_location(data, input);
1582}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001584static int
1585frame_motion_handler(struct widget *widget,
1586 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001587 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001588{
1589 return frame_get_pointer_image_for_location(data, input);
1590}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001591
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001592static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593frame_button_handler(struct widget *widget,
1594 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001595 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596
1597{
1598 struct frame *frame = data;
1599 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001600 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001601 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001602
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001603 location = frame_get_pointer_location(frame, input->sx, input->sy);
1604
1605 if (window->display->shell && button == BTN_LEFT && state == 1) {
1606 switch (location) {
1607 case WINDOW_TITLEBAR:
1608 if (!window->shell_surface)
1609 break;
1610 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001611 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612 wl_shell_surface_move(window->shell_surface,
1613 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001614 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001615 break;
1616 case WINDOW_RESIZING_TOP:
1617 case WINDOW_RESIZING_BOTTOM:
1618 case WINDOW_RESIZING_LEFT:
1619 case WINDOW_RESIZING_RIGHT:
1620 case WINDOW_RESIZING_TOP_LEFT:
1621 case WINDOW_RESIZING_TOP_RIGHT:
1622 case WINDOW_RESIZING_BOTTOM_LEFT:
1623 case WINDOW_RESIZING_BOTTOM_RIGHT:
1624 if (!window->shell_surface)
1625 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001626 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001627
1628 if (!display->dpy) {
1629 /* If we're using shm, allocate a big
1630 pool to create buffers out of while
1631 we resize. We should probably base
1632 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001633 window->pool =
1634 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001635 }
1636
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001637 wl_shell_surface_resize(window->shell_surface,
1638 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001640 break;
1641 }
1642 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001643 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001644 }
1645}
1646
1647struct widget *
1648frame_create(struct window *window, void *data)
1649{
1650 struct frame *frame;
1651
1652 frame = malloc(sizeof *frame);
1653 memset(frame, 0, sizeof *frame);
1654
1655 frame->widget = window_add_widget(window, frame);
1656 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001657 frame->margin = 32;
Martin Minarik1998b152012-05-10 02:04:35 +02001658 frame->width = 6;
1659 frame->titlebar_height = 27;
1660
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001661 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1662 widget_set_resize_handler(frame->widget, frame_resize_handler);
1663 widget_set_enter_handler(frame->widget, frame_enter_handler);
1664 widget_set_motion_handler(frame->widget, frame_motion_handler);
1665 widget_set_button_handler(frame->widget, frame_button_handler);
1666
Martin Minarik1998b152012-05-10 02:04:35 +02001667 /* Create empty list for frame buttons */
1668 wl_list_init(&frame->buttons_list);
1669
1670 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1671 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1672
1673 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1674 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1675
1676 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1677 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1678
1679 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1680 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1681
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001682 window->frame = frame;
1683
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001684 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001685}
1686
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001687static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001688frame_destroy(struct frame *frame)
1689{
Martin Minarik1998b152012-05-10 02:04:35 +02001690 struct frame_button *button, *tmp;
1691
1692 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1693 frame_button_destroy(button);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 /* frame->child must be destroyed by the application */
1696 widget_destroy(frame->widget);
1697 free(frame);
1698}
1699
1700static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001701input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001702 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001704 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001705 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001707 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708 return;
1709
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001710 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001711 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001712 widget = old;
1713 if (input->grab)
1714 widget = input->grab;
1715 if (widget->leave_handler)
1716 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001718 }
1719
1720 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001721 widget = focus;
1722 if (input->grab)
1723 widget = input->grab;
1724 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001726 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001727 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001728
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001729 input_set_pointer_image(input, input->pointer_enter_serial,
1730 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001731 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001732}
1733
1734static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001735input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001736 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001737{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001738 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001739 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001740 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001741 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001742 float sx = wl_fixed_to_double(sx_w);
1743 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001744
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001745 input->sx = sx;
1746 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001748 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001749 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001750 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001751 }
1752
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001753 if (input->grab)
1754 widget = input->grab;
1755 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001756 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001757 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001758 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001759 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001760 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001761
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001762 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001763}
1764
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001765void
1766input_grab(struct input *input, struct widget *widget, uint32_t button)
1767{
1768 input->grab = widget;
1769 input->grab_button = button;
1770}
1771
1772void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001774{
1775 struct widget *widget;
1776
1777 input->grab = NULL;
1778 if (input->pointer_focus) {
1779 widget = widget_find_widget(input->pointer_focus->widget,
1780 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001781 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001782 }
1783}
1784
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001785static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001786input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001787 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001788 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001789{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001790 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001791 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001792
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001793 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001794 if (input->focus_widget && input->grab == NULL && state)
1795 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001796
Neil Roberts6b28aad2012-01-23 19:11:18 +00001797 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001798 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001799 (*widget->button_handler)(widget,
1800 input, time,
1801 button, state,
1802 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001803
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001804 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001805 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001806}
1807
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001808static void
Scott Moreau210d0792012-03-22 10:47:01 -06001809input_handle_axis(void *data,
1810 struct wl_input_device *input_device,
1811 uint32_t time, uint32_t axis, int32_t value)
1812{
1813}
1814
1815static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001816input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001817 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001818{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001819 struct input *input = data;
1820 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001821 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001822 uint32_t code, num_syms;
1823 const xkb_keysym_t *syms;
1824 xkb_keysym_t sym;
1825 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001826
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001827 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001828 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001829 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001830 return;
1831
Kristian Høgsberg70163132012-05-08 15:55:39 -04001832 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1833 xkb_state_update_key(d->xkb.state, code,
1834 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001835
Kristian Høgsberg70163132012-05-08 15:55:39 -04001836 mask = xkb_state_serialise_mods(d->xkb.state,
1837 XKB_STATE_DEPRESSED |
1838 XKB_STATE_LATCHED);
1839 input->modifiers = 0;
1840 if (mask & input->display->xkb.control_mask)
1841 input->modifiers |= MOD_CONTROL_MASK;
1842 if (mask & input->display->xkb.alt_mask)
1843 input->modifiers |= MOD_ALT_MASK;
1844 if (mask & input->display->xkb.shift_mask)
1845 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001846
Kristian Høgsberg70163132012-05-08 15:55:39 -04001847 if (num_syms == 1 && syms[0] == XK_F5 &&
1848 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001849 if (state)
1850 window_set_maximized(window,
1851 window->type != TYPE_MAXIMIZED);
1852 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001853 if (num_syms == 1)
1854 sym = syms[0];
1855 else
1856 sym = NoSymbol;
1857
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001858 (*window->key_handler)(window, input, time, key,
1859 sym, state, window->user_data);
1860 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001861}
1862
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001863static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001864input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001865{
1866 struct window *window = input->pointer_focus;
1867
1868 if (!window)
1869 return;
1870
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001871 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001872
Pekka Paalanene1207c72011-12-16 12:02:09 +02001873 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001874 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001875}
1876
1877static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001878input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001879 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001880 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001881 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001882{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001883 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001884 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001885 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001886 float sx = wl_fixed_to_double(sx_w);
1887 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001888
Martin Minarik1998b152012-05-10 02:04:35 +02001889 if (!surface) {
1890 /* enter event for a window we've just destroyed */
1891 return;
1892 }
1893
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001894 input->display->serial = serial;
1895 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001896 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001897 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001898
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001899 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001900 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001901 window->pool = NULL;
1902 /* Schedule a redraw to free the pool */
1903 window_schedule_redraw(window);
1904 }
1905
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001906 input->sx = sx;
1907 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001908
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001910 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001911}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001912
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001913static void
1914input_handle_pointer_leave(void *data,
1915 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001916 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001917{
1918 struct input *input = data;
1919
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001920 input->display->serial = serial;
1921 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001922}
1923
1924static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001925input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001926{
1927 struct window *window = input->keyboard_focus;
1928
1929 if (!window)
1930 return;
1931
1932 window->keyboard_device = NULL;
1933 if (window->keyboard_focus_handler)
1934 (*window->keyboard_focus_handler)(window, NULL,
1935 window->user_data);
1936
1937 input->keyboard_focus = NULL;
1938}
1939
1940static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001941input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001942 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001943 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001944 struct wl_surface *surface,
1945 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001946{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001947 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001948 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001949
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001950 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001951 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001952
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001953 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001954 window->keyboard_device = input;
1955 if (window->keyboard_focus_handler)
1956 (*window->keyboard_focus_handler)(window,
1957 window->keyboard_device,
1958 window->user_data);
1959}
1960
1961static void
1962input_handle_keyboard_leave(void *data,
1963 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001964 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001965 struct wl_surface *surface)
1966{
1967 struct input *input = data;
1968
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001969 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001970 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001971}
1972
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001973static void
1974input_handle_touch_down(void *data,
1975 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001976 uint32_t serial, uint32_t time,
1977 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001978 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001979{
1980}
1981
1982static void
1983input_handle_touch_up(void *data,
1984 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001985 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001986{
1987}
1988
1989static void
1990input_handle_touch_motion(void *data,
1991 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001992 uint32_t time, int32_t id,
1993 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001994{
1995}
1996
1997static void
1998input_handle_touch_frame(void *data,
1999 struct wl_input_device *wl_input_device)
2000{
2001}
2002
2003static void
2004input_handle_touch_cancel(void *data,
2005 struct wl_input_device *wl_input_device)
2006{
2007}
2008
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002009static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002010 input_handle_motion,
2011 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06002012 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002013 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002014 input_handle_pointer_enter,
2015 input_handle_pointer_leave,
2016 input_handle_keyboard_enter,
2017 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002018 input_handle_touch_down,
2019 input_handle_touch_up,
2020 input_handle_touch_motion,
2021 input_handle_touch_frame,
2022 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002023};
2024
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002025void
2026input_get_position(struct input *input, int32_t *x, int32_t *y)
2027{
2028 *x = input->sx;
2029 *y = input->sy;
2030}
2031
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002032struct wl_input_device *
2033input_get_input_device(struct input *input)
2034{
2035 return input->input_device;
2036}
2037
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002038uint32_t
2039input_get_modifiers(struct input *input)
2040{
2041 return input->modifiers;
2042}
2043
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002044struct widget *
2045input_get_focus_widget(struct input *input)
2046{
2047 return input->focus_widget;
2048}
2049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002050struct data_offer {
2051 struct wl_data_offer *offer;
2052 struct input *input;
2053 struct wl_array types;
2054 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002055
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002056 struct task io_task;
2057 int fd;
2058 data_func_t func;
2059 int32_t x, y;
2060 void *user_data;
2061};
2062
2063static void
2064data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2065{
2066 struct data_offer *offer = data;
2067 char **p;
2068
2069 p = wl_array_add(&offer->types, sizeof *p);
2070 *p = strdup(type);
2071}
2072
2073static const struct wl_data_offer_listener data_offer_listener = {
2074 data_offer_offer,
2075};
2076
2077static void
2078data_offer_destroy(struct data_offer *offer)
2079{
2080 char **p;
2081
2082 offer->refcount--;
2083 if (offer->refcount == 0) {
2084 wl_data_offer_destroy(offer->offer);
2085 for (p = offer->types.data; *p; p++)
2086 free(*p);
2087 wl_array_release(&offer->types);
2088 free(offer);
2089 }
2090}
2091
2092static void
2093data_device_data_offer(void *data,
2094 struct wl_data_device *data_device, uint32_t id)
2095{
2096 struct data_offer *offer;
2097
2098 offer = malloc(sizeof *offer);
2099
2100 wl_array_init(&offer->types);
2101 offer->refcount = 1;
2102 offer->input = data;
2103
2104 /* FIXME: Generate typesafe wrappers for this */
2105 offer->offer = (struct wl_data_offer *)
2106 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2107 id, &wl_data_offer_interface);
2108
2109 wl_data_offer_add_listener(offer->offer,
2110 &data_offer_listener, offer);
2111}
2112
2113static void
2114data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002115 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002116 wl_fixed_t x_w, wl_fixed_t y_w,
2117 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002118{
2119 struct input *input = data;
2120 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002121 float x = wl_fixed_to_double(x_w);
2122 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002123 char **p;
2124
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002125 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002126 input->drag_offer = wl_data_offer_get_user_data(offer);
2127 window = wl_surface_get_user_data(surface);
2128 input->pointer_focus = window;
2129
2130 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2131 *p = NULL;
2132
2133 window = input->pointer_focus;
2134 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002135 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002136 input->drag_offer->types.data,
2137 window->user_data);
2138}
2139
2140static void
2141data_device_leave(void *data, struct wl_data_device *data_device)
2142{
2143 struct input *input = data;
2144
2145 data_offer_destroy(input->drag_offer);
2146 input->drag_offer = NULL;
2147}
2148
2149static void
2150data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002151 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002152{
2153 struct input *input = data;
2154 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002155 float x = wl_fixed_to_double(x_w);
2156 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002157
2158 input->sx = x;
2159 input->sy = y;
2160
2161 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002162 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002163 input->drag_offer->types.data,
2164 window->user_data);
2165}
2166
2167static void
2168data_device_drop(void *data, struct wl_data_device *data_device)
2169{
2170 struct input *input = data;
2171 struct window *window = input->pointer_focus;
2172
2173 if (window->drop_handler)
2174 window->drop_handler(window, input,
2175 input->sx, input->sy, window->user_data);
2176}
2177
2178static void
2179data_device_selection(void *data,
2180 struct wl_data_device *wl_data_device,
2181 struct wl_data_offer *offer)
2182{
2183 struct input *input = data;
2184 char **p;
2185
2186 if (input->selection_offer)
2187 data_offer_destroy(input->selection_offer);
2188
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002189 if (offer) {
2190 input->selection_offer = wl_data_offer_get_user_data(offer);
2191 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2192 *p = NULL;
2193 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002194}
2195
2196static const struct wl_data_device_listener data_device_listener = {
2197 data_device_data_offer,
2198 data_device_enter,
2199 data_device_leave,
2200 data_device_motion,
2201 data_device_drop,
2202 data_device_selection
2203};
2204
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205void
2206input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2207{
2208 struct display *display = input->display;
2209 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002210 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002212 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213 return;
2214
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002215 if (display->cursors[pointer].n_images == 0)
2216 return;
2217
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002218 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002220 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002221 return;
2222
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002223 input->current_cursor = pointer;
2224 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002226 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002227}
2228
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002229struct wl_data_device *
2230input_get_data_device(struct input *input)
2231{
2232 return input->data_device;
2233}
2234
2235void
2236input_set_selection(struct input *input,
2237 struct wl_data_source *source, uint32_t time)
2238{
2239 wl_data_device_set_selection(input->data_device, source, time);
2240}
2241
2242void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002243input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002244{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002245 wl_data_offer_accept(input->drag_offer->offer,
2246 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002247}
2248
2249static void
2250offer_io_func(struct task *task, uint32_t events)
2251{
2252 struct data_offer *offer =
2253 container_of(task, struct data_offer, io_task);
2254 unsigned int len;
2255 char buffer[4096];
2256
2257 len = read(offer->fd, buffer, sizeof buffer);
2258 offer->func(buffer, len,
2259 offer->x, offer->y, offer->user_data);
2260
2261 if (len == 0) {
2262 close(offer->fd);
2263 data_offer_destroy(offer);
2264 }
2265}
2266
2267static void
2268data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2269 data_func_t func, void *user_data)
2270{
2271 int p[2];
2272
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002273 if (pipe2(p, O_CLOEXEC) == -1)
2274 return;
2275
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002276 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2277 close(p[1]);
2278
2279 offer->io_task.run = offer_io_func;
2280 offer->fd = p[0];
2281 offer->func = func;
2282 offer->refcount++;
2283 offer->user_data = user_data;
2284
2285 display_watch_fd(offer->input->display,
2286 offer->fd, EPOLLIN, &offer->io_task);
2287}
2288
2289void
2290input_receive_drag_data(struct input *input, const char *mime_type,
2291 data_func_t func, void *data)
2292{
2293 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2294 input->drag_offer->x = input->sx;
2295 input->drag_offer->y = input->sy;
2296}
2297
2298int
2299input_receive_selection_data(struct input *input, const char *mime_type,
2300 data_func_t func, void *data)
2301{
2302 char **p;
2303
2304 if (input->selection_offer == NULL)
2305 return -1;
2306
2307 for (p = input->selection_offer->types.data; *p; p++)
2308 if (strcmp(mime_type, *p) == 0)
2309 break;
2310
2311 if (*p == NULL)
2312 return -1;
2313
2314 data_offer_receive_data(input->selection_offer,
2315 mime_type, func, data);
2316 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002317}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002318
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002319int
2320input_receive_selection_data_to_fd(struct input *input,
2321 const char *mime_type, int fd)
2322{
2323 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2324
2325 return 0;
2326}
2327
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002328void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002329window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002330{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002331 if (!window->shell_surface)
2332 return;
2333
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002334 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002335 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002336}
2337
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002338static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002339idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002340{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002341 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002342
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002343 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002344 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002345 widget_set_allocation(widget,
2346 window->pending_allocation.x,
2347 window->pending_allocation.y,
2348 window->pending_allocation.width,
2349 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002350
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002351 if (window->input_region) {
2352 wl_region_destroy(window->input_region);
2353 window->input_region = NULL;
2354 }
2355
2356 if (window->opaque_region) {
2357 wl_region_destroy(window->opaque_region);
2358 window->opaque_region = NULL;
2359 }
2360
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002361 if (widget->resize_handler)
2362 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002363 widget->allocation.width,
2364 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002365 widget->user_data);
2366
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002367 if (window->allocation.width != widget->allocation.width ||
2368 window->allocation.height != widget->allocation.height) {
2369 window->allocation = widget->allocation;
2370 window_schedule_redraw(window);
2371 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002372}
2373
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002374void
2375window_schedule_resize(struct window *window, int width, int height)
2376{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002377 window->pending_allocation.x = 0;
2378 window->pending_allocation.y = 0;
2379 window->pending_allocation.width = width;
2380 window->pending_allocation.height = height;
2381
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002382 window->resize_needed = 1;
2383 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002384}
2385
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002386void
2387widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2388{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002390}
2391
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002392static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002393handle_ping(void *data, struct wl_shell_surface *shell_surface,
2394 uint32_t serial)
2395{
2396 wl_shell_surface_pong(shell_surface, serial);
2397}
2398
2399static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002400handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002401 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002402{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002403 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002404
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002405 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002406 return;
2407
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002408 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002409 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002410}
2411
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002412static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002413menu_destroy(struct menu *menu)
2414{
2415 widget_destroy(menu->widget);
2416 window_destroy(menu->window);
2417 free(menu);
2418}
2419
2420static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002421handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2422{
2423 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002424 struct menu *menu = window->widget->user_data;
2425
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002426 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002427 * device. Or just use wl_callback. And this really needs to
2428 * be a window vfunc that the menu can set. And we need the
2429 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002430
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002431 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002432 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002433 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002434}
2435
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002436static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002437 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002438 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002439 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002440};
2441
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002442void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002443window_get_allocation(struct window *window,
2444 struct rectangle *allocation)
2445{
2446 *allocation = window->allocation;
2447}
2448
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002449static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002450widget_redraw(struct widget *widget)
2451{
2452 struct widget *child;
2453
2454 if (widget->redraw_handler)
2455 widget->redraw_handler(widget, widget->user_data);
2456 wl_list_for_each(child, &widget->child_list, link)
2457 widget_redraw(child);
2458}
2459
2460static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002461frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2462{
2463 struct window *window = data;
2464
2465 wl_callback_destroy(callback);
2466 window->redraw_scheduled = 0;
2467 if (window->redraw_needed)
2468 window_schedule_redraw(window);
2469}
2470
2471static const struct wl_callback_listener listener = {
2472 frame_callback
2473};
2474
2475static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002476idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002477{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002478 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002479 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002480
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002481 if (window->resize_needed)
2482 idle_resize(window);
2483
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002484 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002485 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002486 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002487 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002488 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002489
2490 callback = wl_surface_frame(window->surface);
2491 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002492}
2493
2494void
2495window_schedule_redraw(struct window *window)
2496{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002497 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002498 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002499 window->redraw_task.run = idle_redraw;
2500 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002501 window->redraw_scheduled = 1;
2502 }
2503}
2504
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002505void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002506window_set_custom(struct window *window)
2507{
2508 window->type = TYPE_CUSTOM;
2509}
2510
2511void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002512window_set_fullscreen(struct window *window, int fullscreen)
2513{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002514 if (!window->display->shell)
2515 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002516
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002517 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002518 return;
2519
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002520 if (fullscreen) {
2521 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002522 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002523 wl_shell_surface_set_fullscreen(window->shell_surface,
2524 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2525 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002526 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002527 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002528 wl_shell_surface_set_toplevel(window->shell_surface);
2529 window_schedule_resize(window,
2530 window->saved_allocation.width,
2531 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002532 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002533}
2534
2535void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002536window_set_maximized(struct window *window, int maximized)
2537{
2538 if (!window->display->shell)
2539 return;
2540
2541 if ((window->type == TYPE_MAXIMIZED) == maximized)
2542 return;
2543
2544 if (window->type == TYPE_TOPLEVEL) {
2545 window->saved_allocation = window->allocation;
2546 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2547 window->type = TYPE_MAXIMIZED;
2548 } else {
2549 wl_shell_surface_set_toplevel(window->shell_surface);
2550 window->type = TYPE_TOPLEVEL;
2551 window_schedule_resize(window,
2552 window->saved_allocation.width,
2553 window->saved_allocation.height);
2554 }
2555}
2556
2557void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002558window_set_user_data(struct window *window, void *data)
2559{
2560 window->user_data = data;
2561}
2562
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002563void *
2564window_get_user_data(struct window *window)
2565{
2566 return window->user_data;
2567}
2568
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002569void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002570window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002571 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002572{
2573 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002574}
2575
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002576void
2577window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002578 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002579{
2580 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002581}
2582
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002583void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002584window_set_data_handler(struct window *window, window_data_handler_t handler)
2585{
2586 window->data_handler = handler;
2587}
2588
2589void
2590window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2591{
2592 window->drop_handler = handler;
2593}
2594
2595void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002596window_set_close_handler(struct window *window,
2597 window_close_handler_t handler)
2598{
2599 window->close_handler = handler;
2600}
2601
2602void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002603window_set_title(struct window *window, const char *title)
2604{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002605 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002606 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002607 if (window->shell_surface)
2608 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002609}
2610
2611const char *
2612window_get_title(struct window *window)
2613{
2614 return window->title;
2615}
2616
2617void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002618window_damage(struct window *window, int32_t x, int32_t y,
2619 int32_t width, int32_t height)
2620{
2621 wl_surface_damage(window->surface, x, y, width, height);
2622}
2623
Casey Dahlin9074db52012-04-19 22:50:09 -04002624static void
2625surface_enter(void *data,
2626 struct wl_surface *wl_surface, struct wl_output *output)
2627{
2628}
2629
2630static void
2631surface_leave(void *data,
2632 struct wl_surface *wl_surface, struct wl_output *output)
2633{
2634}
2635
2636static const struct wl_surface_listener surface_listener = {
2637 surface_enter,
2638 surface_leave
2639};
2640
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002641static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002642window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002643{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002644 struct window *window;
2645
2646 window = malloc(sizeof *window);
2647 if (window == NULL)
2648 return NULL;
2649
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002650 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002651 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002652 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002653 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002654 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002655 if (display->shell) {
2656 window->shell_surface =
2657 wl_shell_get_shell_surface(display->shell,
2658 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002659 if (window->title)
2660 wl_shell_surface_set_title(window->shell_surface,
2661 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002662 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002663 window->allocation.x = 0;
2664 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002665 window->allocation.width = 0;
2666 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002667 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002668 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002669 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002670 window->input_region = NULL;
2671 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002672
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002673 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002674#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002675 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002676#else
2677 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2678#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002679 else
2680 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002681
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002683 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002684 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002685
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002686 if (window->shell_surface) {
2687 wl_shell_surface_set_user_data(window->shell_surface, window);
2688 wl_shell_surface_add_listener(window->shell_surface,
2689 &shell_surface_listener, window);
2690 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002691
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002692 return window;
2693}
2694
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002695struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002696window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002697{
2698 struct window *window;
2699
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002700 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002701 if (!window)
2702 return NULL;
2703
2704 return window;
2705}
2706
2707struct window *
2708window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002709 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002710{
2711 struct window *window;
2712
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002713 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002714 if (!window)
2715 return NULL;
2716
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002717 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002718 window->x = x;
2719 window->y = y;
2720
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002721 if (display->shell)
2722 wl_shell_surface_set_transient(window->shell_surface,
2723 window->parent->shell_surface,
2724 window->x, window->y, 0);
2725
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002726 return window;
2727}
2728
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002729static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002730menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002731{
2732 int next;
2733
2734 next = (sy - 8) / 20;
2735 if (menu->current != next) {
2736 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002737 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002738 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002739}
2740
2741static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002742menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002743 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002744 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002745{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002746 struct menu *menu = data;
2747
2748 if (widget == menu->widget)
2749 menu_set_item(data, y);
2750
2751 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002752}
2753
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002754static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002755menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002756 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002757{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002758 struct menu *menu = data;
2759
2760 if (widget == menu->widget)
2761 menu_set_item(data, y);
2762
2763 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002764}
2765
2766static void
2767menu_leave_handler(struct widget *widget, struct input *input, void *data)
2768{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002769 struct menu *menu = data;
2770
2771 if (widget == menu->widget)
2772 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002773}
2774
2775static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002776menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002777 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002778 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002779
2780{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002781 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002782
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002783 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002784 /* Either relase after press-drag-release or
2785 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002786 menu->func(menu->window->parent,
2787 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002788 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002789 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002790 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002791}
2792
2793static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002794menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002795{
2796 cairo_t *cr;
2797 const int32_t r = 3, margin = 3;
2798 struct menu *menu = data;
2799 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002800 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002801
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002802 cr = cairo_create(window->cairo_surface);
2803 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2804 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2805 cairo_paint(cr);
2806
2807 width = window->allocation.width;
2808 height = window->allocation.height;
2809 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002810
2811 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002812 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2813 cairo_fill(cr);
2814
2815 for (i = 0; i < menu->count; i++) {
2816 if (i == menu->current) {
2817 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2818 cairo_rectangle(cr, margin, i * 20 + margin,
2819 width - 2 * margin, 20);
2820 cairo_fill(cr);
2821 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2822 cairo_move_to(cr, 10, i * 20 + 16);
2823 cairo_show_text(cr, menu->entries[i]);
2824 } else {
2825 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2826 cairo_move_to(cr, 10, i * 20 + 16);
2827 cairo_show_text(cr, menu->entries[i]);
2828 }
2829 }
2830
2831 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002832}
2833
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002834void
2835window_show_menu(struct display *display,
2836 struct input *input, uint32_t time, struct window *parent,
2837 int32_t x, int32_t y,
2838 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002839{
2840 struct window *window;
2841 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002842 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002843
2844 menu = malloc(sizeof *menu);
2845 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002846 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002847
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002848 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002849 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002850 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002851
2852 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002853 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002854 menu->entries = entries;
2855 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002856 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002857 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002858 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002859 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002860 window->type = TYPE_MENU;
2861 window->x = x;
2862 window->y = y;
2863
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002864 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002865 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2866 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002867 window->parent->shell_surface,
2868 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002869
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002870 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002871 widget_set_enter_handler(menu->widget, menu_enter_handler);
2872 widget_set_leave_handler(menu->widget, menu_leave_handler);
2873 widget_set_motion_handler(menu->widget, menu_motion_handler);
2874 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002875
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002876 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002877 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002878}
2879
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002880void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002881window_set_buffer_type(struct window *window, enum window_buffer_type type)
2882{
2883 window->buffer_type = type;
2884}
2885
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002886
2887static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002888display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002889 struct wl_output *wl_output,
2890 int x, int y,
2891 int physical_width,
2892 int physical_height,
2893 int subpixel,
2894 const char *make,
2895 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002896{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002897 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002898
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002899 output->allocation.x = x;
2900 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002901}
2902
2903static void
2904display_handle_mode(void *data,
2905 struct wl_output *wl_output,
2906 uint32_t flags,
2907 int width,
2908 int height,
2909 int refresh)
2910{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002911 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002912 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002913
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002914 if (flags & WL_OUTPUT_MODE_CURRENT) {
2915 output->allocation.width = width;
2916 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002917 if (display->output_configure_handler)
2918 (*display->output_configure_handler)(
2919 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002920 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002921}
2922
2923static const struct wl_output_listener output_listener = {
2924 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002925 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002926};
2927
2928static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002929display_add_output(struct display *d, uint32_t id)
2930{
2931 struct output *output;
2932
2933 output = malloc(sizeof *output);
2934 if (output == NULL)
2935 return;
2936
2937 memset(output, 0, sizeof *output);
2938 output->display = d;
2939 output->output =
2940 wl_display_bind(d->display, id, &wl_output_interface);
2941 wl_list_insert(d->output_list.prev, &output->link);
2942
2943 wl_output_add_listener(output->output, &output_listener, output);
2944}
2945
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002946static void
2947output_destroy(struct output *output)
2948{
2949 if (output->destroy_handler)
2950 (*output->destroy_handler)(output, output->user_data);
2951
2952 wl_output_destroy(output->output);
2953 wl_list_remove(&output->link);
2954 free(output);
2955}
2956
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002957void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002958display_set_output_configure_handler(struct display *display,
2959 display_output_handler_t handler)
2960{
2961 struct output *output;
2962
2963 display->output_configure_handler = handler;
2964 if (!handler)
2965 return;
2966
2967 wl_list_for_each(output, &display->output_list, link)
2968 (*display->output_configure_handler)(output,
2969 display->user_data);
2970}
2971
2972void
2973output_set_user_data(struct output *output, void *data)
2974{
2975 output->user_data = data;
2976}
2977
2978void *
2979output_get_user_data(struct output *output)
2980{
2981 return output->user_data;
2982}
2983
2984void
2985output_set_destroy_handler(struct output *output,
2986 display_output_handler_t handler)
2987{
2988 output->destroy_handler = handler;
2989 /* FIXME: implement this, once we have way to remove outputs */
2990}
2991
2992void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002993output_get_allocation(struct output *output, struct rectangle *allocation)
2994{
2995 *allocation = output->allocation;
2996}
2997
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002998struct wl_output *
2999output_get_wl_output(struct output *output)
3000{
3001 return output->output;
3002}
3003
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003004static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003005display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003006{
3007 struct input *input;
3008
3009 input = malloc(sizeof *input);
3010 if (input == NULL)
3011 return;
3012
3013 memset(input, 0, sizeof *input);
3014 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003015 input->input_device =
3016 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003017 input->pointer_focus = NULL;
3018 input->keyboard_focus = NULL;
3019 wl_list_insert(d->input_list.prev, &input->link);
3020
3021 wl_input_device_add_listener(input->input_device,
3022 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003023 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003024
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003025 input->data_device =
3026 wl_data_device_manager_get_data_device(d->data_device_manager,
3027 input->input_device);
3028 wl_data_device_add_listener(input->data_device,
3029 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003030}
3031
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003032static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003033input_destroy(struct input *input)
3034{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003035 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003036 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003037
3038 if (input->drag_offer)
3039 data_offer_destroy(input->drag_offer);
3040
3041 if (input->selection_offer)
3042 data_offer_destroy(input->selection_offer);
3043
3044 wl_data_device_destroy(input->data_device);
3045 wl_list_remove(&input->link);
3046 wl_input_device_destroy(input->input_device);
3047 free(input);
3048}
3049
3050static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003051display_handle_global(struct wl_display *display, uint32_t id,
3052 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003053{
3054 struct display *d = data;
3055
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003056 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003057 d->compositor =
3058 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003059 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003060 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003061 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003062 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003063 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003064 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003065 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003066 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003067 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3068 d->data_device_manager =
3069 wl_display_bind(display, id,
3070 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003071 }
3072}
3073
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003074static void
3075display_render_frame(struct display *d)
3076{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003077 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003078 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003079
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003080 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003081 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3082 cr = cairo_create(d->shadow);
3083 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3084 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003085 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003086 cairo_fill(cr);
3087 cairo_destroy(cr);
3088 blur_surface(d->shadow, 64);
3089
3090 d->active_frame =
3091 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3092 cr = cairo_create(d->active_frame);
3093 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003094
3095 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
3096 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
3097 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
3098 cairo_set_source(cr, pattern);
3099 cairo_pattern_destroy(pattern);
3100
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003101 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003102 cairo_fill(cr);
3103 cairo_destroy(cr);
3104
3105 d->inactive_frame =
3106 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3107 cr = cairo_create(d->inactive_frame);
3108 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04003109 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04003110 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003111 cairo_fill(cr);
3112 cairo_destroy(cr);
3113}
3114
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003115static void
3116init_xkb(struct display *d)
3117{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003118 d->xkb.names.rules = "evdev";
3119 d->xkb.names.model = "pc105";
3120 d->xkb.names.layout = (char *) option_xkb_layout;
3121 d->xkb.names.variant = (char *) option_xkb_variant;
3122 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003123
Kristian Høgsberg70163132012-05-08 15:55:39 -04003124 d->xkb.context = xkb_context_new();
3125 if (!d->xkb.context) {
3126 fprintf(stderr, "Failed to create XKB context\n");
3127 exit(1);
3128 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003129
Kristian Høgsberg70163132012-05-08 15:55:39 -04003130 d->xkb.keymap =
3131 xkb_map_new_from_names(d->xkb.context, &d->xkb.names);
3132 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003133 fprintf(stderr, "Failed to compile keymap\n");
3134 exit(1);
3135 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003136
3137 d->xkb.state = xkb_state_new(d->xkb.keymap);
3138 if (!d->xkb.state) {
3139 fprintf(stderr, "Failed to create XKB state\n");
3140 exit(1);
3141 }
3142
3143 d->xkb.control_mask =
3144 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3145 d->xkb.alt_mask =
3146 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3147 d->xkb.shift_mask =
3148 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3149
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003150}
3151
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003152static void
3153fini_xkb(struct display *display)
3154{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003155 xkb_state_unref(display->xkb.state);
3156 xkb_map_unref(display->xkb.keymap);
3157 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003158}
3159
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003160#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003161static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003162init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003163{
3164 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003165 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003166
Rob Clark6396ed32012-03-11 19:48:41 -05003167#ifdef USE_CAIRO_GLESV2
3168# define GL_BIT EGL_OPENGL_ES2_BIT
3169#else
3170# define GL_BIT EGL_OPENGL_BIT
3171#endif
3172
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003173 static const EGLint argb_cfg_attribs[] = {
3174 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003175 EGL_RED_SIZE, 1,
3176 EGL_GREEN_SIZE, 1,
3177 EGL_BLUE_SIZE, 1,
3178 EGL_ALPHA_SIZE, 1,
3179 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003180 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003181 EGL_NONE
3182 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003183
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003184#ifdef USE_CAIRO_GLESV2
3185 static const EGLint context_attribs[] = {
3186 EGL_CONTEXT_CLIENT_VERSION, 2,
3187 EGL_NONE
3188 };
3189 EGLint api = EGL_OPENGL_ES_API;
3190#else
3191 EGLint *context_attribs = NULL;
3192 EGLint api = EGL_OPENGL_API;
3193#endif
3194
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003195 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003196 if (!eglInitialize(d->dpy, &major, &minor)) {
3197 fprintf(stderr, "failed to initialize display\n");
3198 return -1;
3199 }
3200
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003201 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003202 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3203 return -1;
3204 }
3205
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003206 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3207 &d->argb_config, 1, &n) || n != 1) {
3208 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003209 return -1;
3210 }
3211
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003212 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003213 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003214 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003215 fprintf(stderr, "failed to create context\n");
3216 return -1;
3217 }
3218
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003219 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003220 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003221 return -1;
3222 }
3223
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003224#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003225 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3226 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3227 fprintf(stderr, "failed to get cairo egl argb device\n");
3228 return -1;
3229 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003230#endif
3231
3232 return 0;
3233}
3234
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003235static void
3236fini_egl(struct display *display)
3237{
3238#ifdef HAVE_CAIRO_EGL
3239 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003240#endif
3241
3242 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3243 EGL_NO_CONTEXT);
3244
3245 eglTerminate(display->dpy);
3246 eglReleaseThread();
3247}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003248#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003249
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003250static int
3251event_mask_update(uint32_t mask, void *data)
3252{
3253 struct display *d = data;
3254
3255 d->mask = mask;
3256
3257 return 0;
3258}
3259
3260static void
3261handle_display_data(struct task *task, uint32_t events)
3262{
3263 struct display *display =
3264 container_of(task, struct display, display_task);
3265
3266 wl_display_iterate(display->display, display->mask);
3267}
3268
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003269struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003270display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003271{
3272 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003273
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003274 argc = parse_options(xkb_options,
3275 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003276
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003277 d = malloc(sizeof *d);
3278 if (d == NULL)
3279 return NULL;
3280
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003281 memset(d, 0, sizeof *d);
3282
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003283 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003284 if (d->display == NULL) {
3285 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003286 return NULL;
3287 }
3288
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003289 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3290 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3291 d->display_task.run = handle_display_data;
3292 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3293
3294 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003295 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003296 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003297
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003298 /* Set up listener so we'll catch all events. */
3299 wl_display_add_global_listener(d->display,
3300 display_handle_global, d);
3301
3302 /* Process connection events. */
3303 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003304#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003305 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003306 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003307#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003308
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003309 d->image_target_texture_2d =
3310 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3311 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3312 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3313
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003314 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003315
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003316 display_render_frame(d);
3317
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003318 wl_list_init(&d->window_list);
3319
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003320 init_xkb(d);
3321
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003322 return d;
3323}
3324
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003325static void
3326display_destroy_outputs(struct display *display)
3327{
3328 struct output *tmp;
3329 struct output *output;
3330
3331 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3332 output_destroy(output);
3333}
3334
Pekka Paalanene1207c72011-12-16 12:02:09 +02003335static void
3336display_destroy_inputs(struct display *display)
3337{
3338 struct input *tmp;
3339 struct input *input;
3340
3341 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3342 input_destroy(input);
3343}
3344
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003345void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003346display_destroy(struct display *display)
3347{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003348 if (!wl_list_empty(&display->window_list))
3349 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3350
3351 if (!wl_list_empty(&display->deferred_list))
3352 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3353
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003354 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003355 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003356
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003357 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003358
3359 cairo_surface_destroy(display->active_frame);
3360 cairo_surface_destroy(display->inactive_frame);
3361 cairo_surface_destroy(display->shadow);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003362 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003363
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003364#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003365 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003366#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003367
Pekka Paalanenc2052982011-12-16 11:41:32 +02003368 if (display->shell)
3369 wl_shell_destroy(display->shell);
3370
3371 if (display->shm)
3372 wl_shm_destroy(display->shm);
3373
3374 if (display->data_device_manager)
3375 wl_data_device_manager_destroy(display->data_device_manager);
3376
3377 wl_compositor_destroy(display->compositor);
3378
3379 close(display->epoll_fd);
3380
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003381 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003382 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003383 free(display);
3384}
3385
3386void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003387display_set_user_data(struct display *display, void *data)
3388{
3389 display->user_data = data;
3390}
3391
3392void *
3393display_get_user_data(struct display *display)
3394{
3395 return display->user_data;
3396}
3397
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003398struct wl_display *
3399display_get_display(struct display *display)
3400{
3401 return display->display;
3402}
3403
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003404struct output *
3405display_get_output(struct display *display)
3406{
3407 return container_of(display->output_list.next, struct output, link);
3408}
3409
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003410struct wl_compositor *
3411display_get_compositor(struct display *display)
3412{
3413 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003414}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003415
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003416uint32_t
3417display_get_serial(struct display *display)
3418{
3419 return display->serial;
3420}
3421
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003422EGLDisplay
3423display_get_egl_display(struct display *d)
3424{
3425 return d->dpy;
3426}
3427
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003428struct wl_data_source *
3429display_create_data_source(struct display *display)
3430{
3431 return wl_data_device_manager_create_data_source(display->data_device_manager);
3432}
3433
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003434EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003435display_get_argb_egl_config(struct display *d)
3436{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003437 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003438}
3439
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003440struct wl_shell *
3441display_get_shell(struct display *display)
3442{
3443 return display->shell;
3444}
3445
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003446int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003447display_acquire_window_surface(struct display *display,
3448 struct window *window,
3449 EGLContext ctx)
3450{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003451#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003452 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003453 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003454
3455 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003456 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003457 device = cairo_surface_get_device(window->cairo_surface);
3458 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003459 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003460
Benjamin Franzke0c991632011-09-27 21:57:31 +02003461 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003462 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003463 ctx = display->argb_ctx;
3464 else
3465 assert(0);
3466 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003467
3468 data = cairo_surface_get_user_data(window->cairo_surface,
3469 &surface_data_key);
3470
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003471 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003472 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003473 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3474 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003475
3476 return 0;
3477#else
3478 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003479#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003480}
3481
3482void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003483display_release_window_surface(struct display *display,
3484 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003485{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003486#ifdef HAVE_CAIRO_EGL
3487 cairo_device_t *device;
3488
3489 device = cairo_surface_get_device(window->cairo_surface);
3490 if (!device)
3491 return;
3492
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003493 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003494 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003495 cairo_device_release(device);
3496#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003497}
3498
3499void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003500display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003501{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003502 wl_list_insert(&display->deferred_list, &task->link);
3503}
3504
3505void
3506display_watch_fd(struct display *display,
3507 int fd, uint32_t events, struct task *task)
3508{
3509 struct epoll_event ep;
3510
3511 ep.events = events;
3512 ep.data.ptr = task;
3513 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3514}
3515
3516void
3517display_run(struct display *display)
3518{
3519 struct task *task;
3520 struct epoll_event ep[16];
3521 int i, count;
3522
Pekka Paalanen826d7952011-12-15 10:14:07 +02003523 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003524 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003525 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003526
3527 while (!wl_list_empty(&display->deferred_list)) {
3528 task = container_of(display->deferred_list.next,
3529 struct task, link);
3530 wl_list_remove(&task->link);
3531 task->run(task, 0);
3532 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003533
3534 if (!display->running)
3535 break;
3536
3537 wl_display_flush(display->display);
3538
3539 count = epoll_wait(display->epoll_fd,
3540 ep, ARRAY_LENGTH(ep), -1);
3541 for (i = 0; i < count; i++) {
3542 task = ep[i].data.ptr;
3543 task->run(task, ep[i].events);
3544 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003545 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003546}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003547
3548void
3549display_exit(struct display *display)
3550{
3551 display->running = 0;
3552}