blob: dfc19e797cb54d2911484656b269542f81fa25e7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040039
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040040#include <pixman.h>
41
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Rob Clark6396ed32012-03-11 19:48:41 -050044#ifdef USE_CAIRO_GLESV2
45#include <GLES2/gl2.h>
46#include <GLES2/gl2ext.h>
47#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040048#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050049#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040050#include <EGL/egl.h>
51#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040052
Kristian Høgsberg8def2642011-01-14 17:41:33 -050053#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050056
Daniel Stone9d4f0302012-02-15 16:33:21 +000057#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030058#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040059
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020061#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040062#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050065
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030066struct cursor;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070067struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050070 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050071 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040072 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040073 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040074 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040075 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050076 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020077 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020078 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040079 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040080
81 int display_fd;
82 uint32_t mask;
83 struct task display_task;
84
85 int epoll_fd;
86 struct wl_list deferred_list;
87
Pekka Paalanen826d7952011-12-15 10:14:07 +020088 int running;
89
Kristian Høgsberg478d9262010-06-08 20:34:11 -040090 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040091 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050092 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -040093
94 struct theme theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -040095
96 struct {
97 struct xkb_rule_names names;
98 struct xkb_keymap *keymap;
99 struct xkb_state *state;
100 struct xkb_context *context;
101 xkb_mod_mask_t control_mask;
102 xkb_mod_mask_t alt_mask;
103 xkb_mod_mask_t shift_mask;
104 } xkb;
105
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300106 struct cursor *cursors;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700107 struct shm_pool *cursor_shm_pool;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400108
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500109 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
110 PFNEGLCREATEIMAGEKHRPROC create_image;
111 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200112
113 display_output_handler_t output_configure_handler;
114
115 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500116};
117
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400119 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400121 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500122 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500124 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400125 TYPE_CUSTOM
126};
Rob Bradford7507b572012-05-15 17:55:34 +0100127
128struct window_output {
129 struct output *output;
130 struct wl_list link;
131};
132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500133struct window {
134 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500135 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100136 struct wl_list window_output_list;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500137 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500141 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500142 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500143 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500144 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400145 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400146 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400147 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400148 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400149 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400150 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400151 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400152 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400153 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500154
Kristian Høgsberg6e2a8d72012-04-10 11:23:13 -0400155 cairo_surface_t *cairo_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500156
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700157 struct shm_pool *pool;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400158
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500159 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500160 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400161 window_data_handler_t data_handler;
162 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500163 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200165 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500166 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500167
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500168 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400169 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500170};
171
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500172struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500173 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500174 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400175 struct wl_list link;
176 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500177 widget_resize_handler_t resize_handler;
178 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500179 widget_enter_handler_t enter_handler;
180 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500181 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500182 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400183 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500184 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400185};
186
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400187struct input {
188 struct display *display;
189 struct wl_input_device *input_device;
190 struct window *pointer_focus;
191 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300192 int current_cursor;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400193 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400194 uint32_t pointer_enter_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400195 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400196 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500198 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct widget *grab;
200 uint32_t grab_button;
201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400205};
206
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500207struct output {
208 struct display *display;
209 struct wl_output *output;
210 struct rectangle allocation;
211 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200212
213 display_output_handler_t destroy_handler;
214 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500215};
216
Martin Minarik1998b152012-05-10 02:04:35 +0200217enum frame_button_action {
218 FRAME_BUTTON_NULL = 0,
219 FRAME_BUTTON_ICON = 1,
220 FRAME_BUTTON_CLOSE = 2,
221 FRAME_BUTTON_MINIMIZE = 3,
222 FRAME_BUTTON_MAXIMIZE = 4,
223};
224
225enum frame_button_pointer {
226 FRAME_BUTTON_DEFAULT = 0,
227 FRAME_BUTTON_OVER = 1,
228 FRAME_BUTTON_ACTIVE = 2,
229};
230
231enum frame_button_align {
232 FRAME_BUTTON_RIGHT = 0,
233 FRAME_BUTTON_LEFT = 1,
234};
235
236enum frame_button_decoration {
237 FRAME_BUTTON_NONE = 0,
238 FRAME_BUTTON_FANCY = 1,
239};
240
241struct frame_button {
242 struct widget *widget;
243 struct frame *frame;
244 cairo_surface_t *icon;
245 enum frame_button_action type;
246 enum frame_button_pointer state;
247 struct wl_list link; /* buttons_list */
248 enum frame_button_align align;
249 enum frame_button_decoration decoration;
250};
251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500252struct frame {
253 struct widget *widget;
254 struct widget *child;
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;
Rob Bradford7507b572012-05-15 17:55:34 +0100954 struct window_output *window_output;
955 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200956
957 if (window->redraw_scheduled)
958 wl_list_remove(&window->redraw_task.link);
959
960 wl_list_for_each(input, &display->input_list, link) {
961 if (input->pointer_focus == window)
962 input->pointer_focus = NULL;
963 if (input->keyboard_focus == window)
964 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500965 if (input->focus_widget &&
966 input->focus_widget->window == window)
967 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200968 }
969
Rob Bradford7507b572012-05-15 17:55:34 +0100970 wl_list_for_each_safe(window_output, window_output_tmp,
971 &window->window_output_list, link) {
972 free (window_output);
973 }
974
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500975 if (window->input_region)
976 wl_region_destroy(window->input_region);
977 if (window->opaque_region)
978 wl_region_destroy(window->opaque_region);
979
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200980 if (window->frame)
981 frame_destroy(window->frame);
982
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200983 if (window->shell_surface)
984 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500985 wl_surface_destroy(window->surface);
986 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200987
988 if (window->cairo_surface != NULL)
989 cairo_surface_destroy(window->cairo_surface);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200990
991 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500992 free(window);
993}
994
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500995static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500996widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500998 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001000 wl_list_for_each(child, &widget->child_list, link) {
1001 target = widget_find_widget(child, x, y);
1002 if (target)
1003 return target;
1004 }
1005
1006 if (widget->allocation.x <= x &&
1007 x < widget->allocation.x + widget->allocation.width &&
1008 widget->allocation.y <= y &&
1009 y < widget->allocation.y + widget->allocation.height) {
1010 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011 }
1012
1013 return NULL;
1014}
1015
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001016static struct widget *
1017widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001019 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001021 widget = malloc(sizeof *widget);
1022 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001023 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001025 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001026 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001027 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001028
1029 return widget;
1030}
1031
1032struct widget *
1033window_add_widget(struct window *window, void *data)
1034{
1035 window->widget = widget_create(window, data);
1036 wl_list_init(&window->widget->link);
1037
1038 return window->widget;
1039}
1040
1041struct widget *
1042widget_add_widget(struct widget *parent, void *data)
1043{
1044 struct widget *widget;
1045
1046 widget = widget_create(parent->window, data);
1047 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001049 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001050}
1051
1052void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001055 struct display *display = widget->window->display;
1056 struct input *input;
1057
1058 wl_list_for_each(input, &display->input_list, link) {
1059 if (input->focus_widget == widget)
1060 input->focus_widget = NULL;
1061 }
1062
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001063 wl_list_remove(&widget->link);
1064 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065}
1066
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001069{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071}
1072
1073void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001074widget_set_size(struct widget *widget, int32_t width, int32_t height)
1075{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001076 widget->allocation.width = width;
1077 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001078}
1079
1080void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001081widget_set_allocation(struct widget *widget,
1082 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001083{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084 widget->allocation.x = x;
1085 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001086 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001089void
1090widget_set_transparent(struct widget *widget, int transparent)
1091{
1092 widget->opaque = !transparent;
1093}
1094
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001101void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001102widget_set_resize_handler(struct widget *widget,
1103 widget_resize_handler_t handler)
1104{
1105 widget->resize_handler = handler;
1106}
1107
1108void
1109widget_set_redraw_handler(struct widget *widget,
1110 widget_redraw_handler_t handler)
1111{
1112 widget->redraw_handler = handler;
1113}
1114
1115void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001116widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001117{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118 widget->enter_handler = handler;
1119}
1120
1121void
1122widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1123{
1124 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001125}
1126
1127void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001128widget_set_motion_handler(struct widget *widget,
1129 widget_motion_handler_t handler)
1130{
1131 widget->motion_handler = handler;
1132}
1133
1134void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001135widget_set_button_handler(struct widget *widget,
1136 widget_button_handler_t handler)
1137{
1138 widget->button_handler = handler;
1139}
1140
1141void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001142widget_schedule_redraw(struct widget *widget)
1143{
1144 window_schedule_redraw(widget->window);
1145}
1146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001147cairo_surface_t *
1148window_get_surface(struct window *window)
1149{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001150 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151}
1152
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001153struct wl_surface *
1154window_get_wl_surface(struct window *window)
1155{
1156 return window->surface;
1157}
1158
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159struct wl_shell_surface *
1160window_get_wl_shell_surface(struct window *window)
1161{
1162 return window->shell_surface;
1163}
1164
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165static void
1166frame_resize_handler(struct widget *widget,
1167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 struct frame *frame = data;
1170 struct widget *child = frame->child;
1171 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001172 struct display *display = widget->window->display;
Martin Minarik1998b152012-05-10 02:04:35 +02001173 struct frame_button * button;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001174 struct theme *t = &display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02001175 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001176 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001177 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001178
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001179 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001180 decoration_width = (t->width + t->margin) * 2;
1181 decoration_height = t->width +
1182 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001184 allocation.x = t->width + t->margin;
1185 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001186 allocation.width = width - decoration_width;
1187 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001188
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001189 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02001190
1191 wl_list_for_each(button, &frame->buttons_list, link)
1192 button->widget->opaque = 0;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001193 } else {
1194 decoration_width = 0;
1195 decoration_height = 0;
1196
1197 allocation.x = 0;
1198 allocation.y = 0;
1199 allocation.width = width;
1200 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001201 opaque_margin = 0;
Martin Minarik1998b152012-05-10 02:04:35 +02001202
1203 wl_list_for_each(button, &frame->buttons_list, link)
1204 button->widget->opaque = 1;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001205 }
1206
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001207 widget_set_allocation(child, allocation.x, allocation.y,
1208 allocation.width, allocation.height);
1209
1210 if (child->resize_handler)
1211 child->resize_handler(child,
1212 allocation.width,
1213 allocation.height,
1214 child->user_data);
1215
Scott Moreauf7e498c2012-05-14 11:39:29 -06001216 width = child->allocation.width + decoration_width;
1217 height = child->allocation.height + decoration_height;
1218
1219 widget->window->input_region =
1220 wl_compositor_create_region(display->compositor);
1221 wl_region_add(widget->window->input_region,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001222 t->margin, t->margin,
1223 width - 2 * t->margin,
1224 height - 2 * t->margin);
Scott Moreauf7e498c2012-05-14 11:39:29 -06001225
1226 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001227
1228 if (child->opaque) {
1229 widget->window->opaque_region =
1230 wl_compositor_create_region(display->compositor);
1231 wl_region_add(widget->window->opaque_region,
1232 opaque_margin, opaque_margin,
1233 widget->allocation.width - 2 * opaque_margin,
1234 widget->allocation.height - 2 * opaque_margin);
1235 }
Martin Minarik1998b152012-05-10 02:04:35 +02001236
1237 /* frame internal buttons */
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001238 x_r = frame->widget->allocation.width - t->width - t->margin;
1239 x_l = t->width + t->margin;
1240 y = t->width + t->margin;
Martin Minarik1998b152012-05-10 02:04:35 +02001241 wl_list_for_each(button, &frame->buttons_list, link) {
1242 const int button_padding = 4;
1243 w = cairo_image_surface_get_width(button->icon);
1244 h = cairo_image_surface_get_height(button->icon);
1245
1246 if (button->decoration == FRAME_BUTTON_FANCY)
1247 w += 10;
1248
1249 if (button->align == FRAME_BUTTON_LEFT) {
1250 widget_set_allocation(button->widget,
1251 x_l, y , w + 1, h + 1);
1252 x_l += w;
1253 x_l += button_padding;
1254 } else {
1255 x_r -= w;
1256 widget_set_allocation(button->widget,
1257 x_r, y , w + 1, h + 1);
1258 x_r -= button_padding;
1259 }
1260 }
1261}
1262
1263static int
1264frame_button_enter_handler(struct widget *widget,
1265 struct input *input, float x, float y, void *data)
1266{
1267 struct frame_button *frame_button = data;
1268
1269 widget_schedule_redraw(frame_button->widget);
1270 frame_button->state = FRAME_BUTTON_OVER;
1271
1272 return POINTER_LEFT_PTR;
1273}
1274
1275static void
1276frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1277{
1278 struct frame_button *frame_button = data;
1279
1280 widget_schedule_redraw(frame_button->widget);
1281 frame_button->state = FRAME_BUTTON_DEFAULT;
1282}
1283
1284static void
1285frame_button_button_handler(struct widget *widget,
1286 struct input *input, uint32_t time,
1287 uint32_t button, uint32_t state, void *data)
1288{
1289 struct frame_button *frame_button = data;
1290 struct window *window = widget->window;
1291
1292 if (button != BTN_LEFT)
1293 return;
1294
1295 switch (state) {
1296 case 1:
1297 frame_button->state = FRAME_BUTTON_ACTIVE;
1298 widget_schedule_redraw(frame_button->widget);
1299
1300 if (frame_button->type == FRAME_BUTTON_ICON)
1301 window_show_frame_menu(window, input, time);
1302 return;
1303 case 0:
1304 frame_button->state = FRAME_BUTTON_DEFAULT;
1305 widget_schedule_redraw(frame_button->widget);
1306 break;
1307 }
1308
1309 switch (frame_button->type) {
1310 case FRAME_BUTTON_CLOSE:
1311 if (window->close_handler)
1312 window->close_handler(window->parent,
1313 window->user_data);
1314 else
1315 display_exit(window->display);
1316 break;
1317 case FRAME_BUTTON_MINIMIZE:
1318 fprintf(stderr,"Minimize stub\n");
1319 break;
1320 case FRAME_BUTTON_MAXIMIZE:
1321 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1322 break;
1323 default:
1324 /* Unknown operation */
1325 break;
1326 }
1327}
1328
1329static void
1330frame_button_redraw_handler(struct widget *widget, void *data)
1331{
1332 struct frame_button *frame_button = data;
1333 cairo_t *cr;
1334 int width, height, x, y;
1335 struct window *window = widget->window;
1336
1337 x = widget->allocation.x;
1338 y = widget->allocation.y;
1339 width = widget->allocation.width;
1340 height = widget->allocation.height;
1341
1342 if (!width)
1343 return;
1344 if (!height)
1345 return;
1346 if (widget->opaque)
1347 return;
1348
1349 cr = cairo_create(window->cairo_surface);
1350
1351 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1352 cairo_set_line_width(cr, 1);
1353
1354 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1355 cairo_rectangle (cr, x, y, 25, 16);
1356
1357 cairo_stroke_preserve(cr);
1358
1359 switch (frame_button->state) {
1360 case FRAME_BUTTON_DEFAULT:
1361 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1362 break;
1363 case FRAME_BUTTON_OVER:
1364 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1365 break;
1366 case FRAME_BUTTON_ACTIVE:
1367 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1368 break;
1369 }
1370
1371 cairo_fill (cr);
1372
1373 x += 4;
1374 }
1375
1376 cairo_set_source_surface(cr, frame_button->icon, x, y);
1377 cairo_paint(cr);
1378
1379 cairo_destroy(cr);
1380}
1381
1382static struct widget *
1383frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1384 enum frame_button_align align, enum frame_button_decoration style)
1385{
1386 struct frame_button *frame_button;
1387 const char *icon = data;
1388
1389 frame_button = malloc (sizeof *frame_button);
1390 memset(frame_button, 0, sizeof *frame_button);
1391
1392 frame_button->icon = cairo_image_surface_create_from_png(icon);
1393 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1394 frame_button->frame = frame;
1395 frame_button->type = type;
1396 frame_button->align = align;
1397 frame_button->decoration = style;
1398
1399 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1400
1401 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1402 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1403 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1404 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1405 return frame_button->widget;
1406}
1407
1408static void
1409frame_button_destroy(struct frame_button *frame_button)
1410{
1411 widget_destroy(frame_button->widget);
1412 wl_list_remove(&frame_button->link);
1413 cairo_surface_destroy(frame_button->icon);
1414 free(frame_button);
1415
1416 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001417}
1418
1419static void
1420frame_redraw_handler(struct widget *widget, void *data)
1421{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001422 cairo_t *cr;
1423 cairo_text_extents_t extents;
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001424 cairo_font_extents_t font_extents;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001425 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001426 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427 struct window *window = widget->window;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001428 struct theme *t = &window->display->theme;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001429
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001430 if (window->type == TYPE_FULLSCREEN)
1431 return;
1432
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001433 width = widget->allocation.width;
1434 height = widget->allocation.height;
1435
1436 cr = cairo_create(window->cairo_surface);
1437
1438 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1439 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1440 cairo_paint(cr);
1441
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001442 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001443 tile_mask(cr, t->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001444 2, 2, width + 8, height + 8,
1445 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001446
1447 if (window->keyboard_device)
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001448 source = t->active_frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001449 else
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001450 source = t->inactive_frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001451
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001452 tile_source(cr, source,
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001453 t->margin, t->margin,
1454 width - t->margin * 2, height - t->margin * 2,
1455 t->width, t->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001456
1457 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001458 cairo_select_font_face(cr, "sans",
1459 CAIRO_FONT_SLANT_NORMAL,
1460 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001461 cairo_set_font_size(cr, 14);
1462 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001463 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001464 x = (width - extents.width) / 2;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001465 y = t->margin +
1466 (t->titlebar_height -
Kristian Høgsbergd9931882012-05-15 21:52:25 -04001467 font_extents.ascent - font_extents.descent) / 2 +
1468 font_extents.ascent;
1469
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001470 if (window->keyboard_device) {
1471 cairo_move_to(cr, x + 1, y + 1);
1472 cairo_set_source_rgb(cr, 1, 1, 1);
1473 cairo_show_text(cr, window->title);
1474 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001475 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001476 cairo_show_text(cr, window->title);
1477 } else {
1478 cairo_move_to(cr, x, y);
1479 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1480 cairo_show_text(cr, window->title);
1481 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001482
1483 cairo_destroy(cr);
1484}
1485
1486static int
1487frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1488{
1489 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001490 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001491 const int grip_size = 8;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001492 struct theme *t = &widget->window->display->theme;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001493
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001494 if (x < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001495 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001496 else if (t->margin <= x && x < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001497 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001498 else if (x < widget->allocation.width - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001499 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001500 else if (x < widget->allocation.width - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001501 hlocation = WINDOW_RESIZING_RIGHT;
1502 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001503 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001504
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001505 if (y < t->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001506 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001507 else if (t->margin <= y && y < t->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001508 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001509 else if (y < widget->allocation.height - t->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001510 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001511 else if (y < widget->allocation.height - t->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001512 vlocation = WINDOW_RESIZING_BOTTOM;
1513 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001514 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001515
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001516 location = vlocation | hlocation;
1517 if (location & WINDOW_EXTERIOR)
1518 location = WINDOW_EXTERIOR;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04001519 if (location == WINDOW_INTERIOR && y < t->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001520 location = WINDOW_TITLEBAR;
1521 else if (location == WINDOW_INTERIOR)
1522 location = WINDOW_CLIENT_AREA;
1523
1524 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001525}
1526
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001527static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001528frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001529{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001530 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001531
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001532 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001533 switch (location) {
1534 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001535 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001536 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001537 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001538 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001539 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001540 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001541 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001542 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001543 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001544 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001545 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001546 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001547 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001548 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001549 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001550 case WINDOW_EXTERIOR:
1551 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001552 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001553 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001554 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001555}
1556
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001557static void
1558frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001559{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001560 switch (index) {
1561 case 0: /* close */
1562 if (window->close_handler)
1563 window->close_handler(window->parent,
1564 window->user_data);
1565 else
1566 display_exit(window->display);
1567 break;
1568 case 1: /* fullscreen */
1569 /* we don't have a way to get out of fullscreen for now */
1570 window_set_fullscreen(window, 1);
1571 break;
1572 case 2: /* rotate */
1573 case 3: /* scale */
1574 break;
1575 }
1576}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001577
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001578void
1579window_show_frame_menu(struct window *window,
1580 struct input *input, uint32_t time)
1581{
1582 int32_t x, y;
1583
1584 static const char *entries[] = {
1585 "Close", "Fullscreen", "Rotate", "Scale"
1586 };
1587
1588 input_get_position(input, &x, &y);
1589 window_show_menu(window->display, input, time, window,
1590 x - 10, y - 10, frame_menu_func, entries, 4);
1591}
1592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001593static int
1594frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001595 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001596{
1597 return frame_get_pointer_image_for_location(data, input);
1598}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001599
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001600static int
1601frame_motion_handler(struct widget *widget,
1602 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001603 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001604{
1605 return frame_get_pointer_image_for_location(data, input);
1606}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001607
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001608static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001609frame_button_handler(struct widget *widget,
1610 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01001611 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001612
1613{
1614 struct frame *frame = data;
1615 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001616 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001617 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001618
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001619 location = frame_get_pointer_location(frame, input->sx, input->sy);
1620
1621 if (window->display->shell && button == BTN_LEFT && state == 1) {
1622 switch (location) {
1623 case WINDOW_TITLEBAR:
1624 if (!window->shell_surface)
1625 break;
1626 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001627 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001628 wl_shell_surface_move(window->shell_surface,
1629 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001630 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001631 break;
1632 case WINDOW_RESIZING_TOP:
1633 case WINDOW_RESIZING_BOTTOM:
1634 case WINDOW_RESIZING_LEFT:
1635 case WINDOW_RESIZING_RIGHT:
1636 case WINDOW_RESIZING_TOP_LEFT:
1637 case WINDOW_RESIZING_TOP_RIGHT:
1638 case WINDOW_RESIZING_BOTTOM_LEFT:
1639 case WINDOW_RESIZING_BOTTOM_RIGHT:
1640 if (!window->shell_surface)
1641 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001642 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001643
1644 if (!display->dpy) {
1645 /* If we're using shm, allocate a big
1646 pool to create buffers out of while
1647 we resize. We should probably base
1648 this number on the size of the output. */
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001649 window->pool =
1650 shm_pool_create(display, 6 * 1024 * 1024);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001651 }
1652
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001653 wl_shell_surface_resize(window->shell_surface,
1654 input_get_input_device(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001655 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001656 break;
1657 }
1658 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001659 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001660 }
1661}
1662
1663struct widget *
1664frame_create(struct window *window, void *data)
1665{
1666 struct frame *frame;
1667
1668 frame = malloc(sizeof *frame);
1669 memset(frame, 0, sizeof *frame);
1670
1671 frame->widget = window_add_widget(window, frame);
1672 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02001673
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001674 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1675 widget_set_resize_handler(frame->widget, frame_resize_handler);
1676 widget_set_enter_handler(frame->widget, frame_enter_handler);
1677 widget_set_motion_handler(frame->widget, frame_motion_handler);
1678 widget_set_button_handler(frame->widget, frame_button_handler);
1679
Martin Minarik1998b152012-05-10 02:04:35 +02001680 /* Create empty list for frame buttons */
1681 wl_list_init(&frame->buttons_list);
1682
1683 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1684 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1685
1686 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1687 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1688
1689 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1690 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1691
1692 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1693 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1694
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001695 window->frame = frame;
1696
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001697 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001698}
1699
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001700static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001701frame_destroy(struct frame *frame)
1702{
Martin Minarik1998b152012-05-10 02:04:35 +02001703 struct frame_button *button, *tmp;
1704
1705 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1706 frame_button_destroy(button);
1707
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001708 /* frame->child must be destroyed by the application */
1709 widget_destroy(frame->widget);
1710 free(frame);
1711}
1712
1713static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001714input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001715 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001716{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001717 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001718 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001720 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721 return;
1722
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001723 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001724 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001725 widget = old;
1726 if (input->grab)
1727 widget = input->grab;
1728 if (widget->leave_handler)
1729 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001730 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001731 }
1732
1733 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001734 widget = focus;
1735 if (input->grab)
1736 widget = input->grab;
1737 if (widget->enter_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001738 pointer = widget->enter_handler(focus, input, x, y,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001739 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001740 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001741
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001742 input_set_pointer_image(input, input->pointer_enter_serial,
1743 pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001744 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001745}
1746
1747static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001748input_handle_motion(void *data, struct wl_input_device *input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01001749 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001750{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001751 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001752 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001753 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001754 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001755 float sx = wl_fixed_to_double(sx_w);
1756 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001757
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001758 input->sx = sx;
1759 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001761 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001762 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001763 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001764 }
1765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001766 if (input->grab)
1767 widget = input->grab;
1768 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001769 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001770 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001771 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001772 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001773 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001774
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001775 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001776}
1777
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001778void
1779input_grab(struct input *input, struct widget *widget, uint32_t button)
1780{
1781 input->grab = widget;
1782 input->grab_button = button;
1783}
1784
1785void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001786input_ungrab(struct input *input)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001787{
1788 struct widget *widget;
1789
1790 input->grab = NULL;
1791 if (input->pointer_focus) {
1792 widget = widget_find_widget(input->pointer_focus->widget,
1793 input->sx, input->sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001794 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001795 }
1796}
1797
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001798static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001799input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001800 struct wl_input_device *input_device, uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001801 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001802{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001803 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001804 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001805
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 input->display->serial = serial;
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001807 if (input->focus_widget && input->grab == NULL && state)
1808 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001809
Neil Roberts6b28aad2012-01-23 19:11:18 +00001810 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001811 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001812 (*widget->button_handler)(widget,
1813 input, time,
1814 button, state,
1815 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001816
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001817 if (input->grab && input->grab_button == button && !state)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001818 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001819}
1820
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001821static void
Scott Moreau210d0792012-03-22 10:47:01 -06001822input_handle_axis(void *data,
1823 struct wl_input_device *input_device,
1824 uint32_t time, uint32_t axis, int32_t value)
1825{
1826}
1827
1828static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001829input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001830 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001831{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001832 struct input *input = data;
1833 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001834 struct display *d = input->display;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001835 uint32_t code, num_syms;
1836 const xkb_keysym_t *syms;
1837 xkb_keysym_t sym;
1838 xkb_mod_mask_t mask;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001839
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001840 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00001841 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001842 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001843 return;
1844
Kristian Høgsberg70163132012-05-08 15:55:39 -04001845 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1846 xkb_state_update_key(d->xkb.state, code,
1847 state ? XKB_KEY_DOWN : XKB_KEY_UP);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001848
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001849 mask = xkb_state_serialize_mods(d->xkb.state,
Kristian Høgsberg70163132012-05-08 15:55:39 -04001850 XKB_STATE_DEPRESSED |
1851 XKB_STATE_LATCHED);
1852 input->modifiers = 0;
1853 if (mask & input->display->xkb.control_mask)
1854 input->modifiers |= MOD_CONTROL_MASK;
1855 if (mask & input->display->xkb.alt_mask)
1856 input->modifiers |= MOD_ALT_MASK;
1857 if (mask & input->display->xkb.shift_mask)
1858 input->modifiers |= MOD_SHIFT_MASK;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001859
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04001860 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04001861 input->modifiers == MOD_ALT_MASK) {
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001862 if (state)
1863 window_set_maximized(window,
1864 window->type != TYPE_MAXIMIZED);
1865 } else if (window->key_handler) {
Kristian Høgsberg70163132012-05-08 15:55:39 -04001866 if (num_syms == 1)
1867 sym = syms[0];
1868 else
Pekka Paalanen79b56522012-05-14 16:21:06 +03001869 sym = XKB_KEY_NoSymbol;
Kristian Høgsberg70163132012-05-08 15:55:39 -04001870
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001871 (*window->key_handler)(window, input, time, key,
1872 sym, state, window->user_data);
1873 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001874}
1875
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001876static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001877input_remove_pointer_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001878{
1879 struct window *window = input->pointer_focus;
1880
1881 if (!window)
1882 return;
1883
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001884 input_set_focus_widget(input, NULL, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001885
Pekka Paalanene1207c72011-12-16 12:02:09 +02001886 input->pointer_focus = NULL;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03001887 input->current_cursor = POINTER_UNSET;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001888}
1889
1890static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001891input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001892 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001893 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001894 wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001895{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001896 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001897 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001898 struct widget *widget;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04001899 float sx = wl_fixed_to_double(sx_w);
1900 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001901
Martin Minarik1998b152012-05-10 02:04:35 +02001902 if (!surface) {
1903 /* enter event for a window we've just destroyed */
1904 return;
1905 }
1906
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001907 input->display->serial = serial;
1908 input->pointer_enter_serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001909 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001910 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001911
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001912 if (window->pool) {
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -07001913 shm_pool_destroy(window->pool);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001914 window->pool = NULL;
1915 /* Schedule a redraw to free the pool */
1916 window_schedule_redraw(window);
1917 }
1918
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001919 input->sx = sx;
1920 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001921
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001922 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001923 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001924}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001925
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001926static void
1927input_handle_pointer_leave(void *data,
1928 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001929 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001930{
1931 struct input *input = data;
1932
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001933 input->display->serial = serial;
1934 input_remove_pointer_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001935}
1936
1937static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001938input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001939{
1940 struct window *window = input->keyboard_focus;
1941
1942 if (!window)
1943 return;
1944
1945 window->keyboard_device = NULL;
1946 if (window->keyboard_focus_handler)
1947 (*window->keyboard_focus_handler)(window, NULL,
1948 window->user_data);
1949
1950 input->keyboard_focus = NULL;
1951}
1952
1953static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001954input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001955 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001956 uint32_t serial,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001957 struct wl_surface *surface,
1958 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001959{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001960 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001961 struct window *window;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001962
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001963 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001964 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001965
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001966 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001967 window->keyboard_device = input;
1968 if (window->keyboard_focus_handler)
1969 (*window->keyboard_focus_handler)(window,
1970 window->keyboard_device,
1971 window->user_data);
1972}
1973
1974static void
1975input_handle_keyboard_leave(void *data,
1976 struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001977 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001978 struct wl_surface *surface)
1979{
1980 struct input *input = data;
1981
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001982 input->display->serial = serial;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001983 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001984}
1985
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001986static void
1987input_handle_touch_down(void *data,
1988 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001989 uint32_t serial, uint32_t time,
1990 struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001991 int32_t id, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001992{
1993}
1994
1995static void
1996input_handle_touch_up(void *data,
1997 struct wl_input_device *wl_input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001998 uint32_t serial, uint32_t time, int32_t id)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001999{
2000}
2001
2002static void
2003input_handle_touch_motion(void *data,
2004 struct wl_input_device *wl_input_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002005 uint32_t time, int32_t id,
2006 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002007{
2008}
2009
2010static void
2011input_handle_touch_frame(void *data,
2012 struct wl_input_device *wl_input_device)
2013{
2014}
2015
2016static void
2017input_handle_touch_cancel(void *data,
2018 struct wl_input_device *wl_input_device)
2019{
2020}
2021
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002022static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002023 input_handle_motion,
2024 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06002025 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002026 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05002027 input_handle_pointer_enter,
2028 input_handle_pointer_leave,
2029 input_handle_keyboard_enter,
2030 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002031 input_handle_touch_down,
2032 input_handle_touch_up,
2033 input_handle_touch_motion,
2034 input_handle_touch_frame,
2035 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002036};
2037
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002038void
2039input_get_position(struct input *input, int32_t *x, int32_t *y)
2040{
2041 *x = input->sx;
2042 *y = input->sy;
2043}
2044
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002045struct wl_input_device *
2046input_get_input_device(struct input *input)
2047{
2048 return input->input_device;
2049}
2050
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002051uint32_t
2052input_get_modifiers(struct input *input)
2053{
2054 return input->modifiers;
2055}
2056
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002057struct widget *
2058input_get_focus_widget(struct input *input)
2059{
2060 return input->focus_widget;
2061}
2062
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002063struct data_offer {
2064 struct wl_data_offer *offer;
2065 struct input *input;
2066 struct wl_array types;
2067 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002068
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002069 struct task io_task;
2070 int fd;
2071 data_func_t func;
2072 int32_t x, y;
2073 void *user_data;
2074};
2075
2076static void
2077data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2078{
2079 struct data_offer *offer = data;
2080 char **p;
2081
2082 p = wl_array_add(&offer->types, sizeof *p);
2083 *p = strdup(type);
2084}
2085
2086static const struct wl_data_offer_listener data_offer_listener = {
2087 data_offer_offer,
2088};
2089
2090static void
2091data_offer_destroy(struct data_offer *offer)
2092{
2093 char **p;
2094
2095 offer->refcount--;
2096 if (offer->refcount == 0) {
2097 wl_data_offer_destroy(offer->offer);
2098 for (p = offer->types.data; *p; p++)
2099 free(*p);
2100 wl_array_release(&offer->types);
2101 free(offer);
2102 }
2103}
2104
2105static void
2106data_device_data_offer(void *data,
2107 struct wl_data_device *data_device, uint32_t id)
2108{
2109 struct data_offer *offer;
2110
2111 offer = malloc(sizeof *offer);
2112
2113 wl_array_init(&offer->types);
2114 offer->refcount = 1;
2115 offer->input = data;
2116
2117 /* FIXME: Generate typesafe wrappers for this */
2118 offer->offer = (struct wl_data_offer *)
2119 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2120 id, &wl_data_offer_interface);
2121
2122 wl_data_offer_add_listener(offer->offer,
2123 &data_offer_listener, offer);
2124}
2125
2126static void
2127data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002128 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01002129 wl_fixed_t x_w, wl_fixed_t y_w,
2130 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002131{
2132 struct input *input = data;
2133 struct window *window;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002134 float x = wl_fixed_to_double(x_w);
2135 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002136 char **p;
2137
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002138 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002139 input->drag_offer = wl_data_offer_get_user_data(offer);
2140 window = wl_surface_get_user_data(surface);
2141 input->pointer_focus = window;
2142
2143 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2144 *p = NULL;
2145
2146 window = input->pointer_focus;
2147 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002148 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002149 input->drag_offer->types.data,
2150 window->user_data);
2151}
2152
2153static void
2154data_device_leave(void *data, struct wl_data_device *data_device)
2155{
2156 struct input *input = data;
2157
2158 data_offer_destroy(input->drag_offer);
2159 input->drag_offer = NULL;
2160}
2161
2162static void
2163data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01002164 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002165{
2166 struct input *input = data;
2167 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002168 float x = wl_fixed_to_double(x_w);
2169 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002170
2171 input->sx = x;
2172 input->sy = y;
2173
2174 if (window->data_handler)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002175 window->data_handler(window, input, x, y,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002176 input->drag_offer->types.data,
2177 window->user_data);
2178}
2179
2180static void
2181data_device_drop(void *data, struct wl_data_device *data_device)
2182{
2183 struct input *input = data;
2184 struct window *window = input->pointer_focus;
2185
2186 if (window->drop_handler)
2187 window->drop_handler(window, input,
2188 input->sx, input->sy, window->user_data);
2189}
2190
2191static void
2192data_device_selection(void *data,
2193 struct wl_data_device *wl_data_device,
2194 struct wl_data_offer *offer)
2195{
2196 struct input *input = data;
2197 char **p;
2198
2199 if (input->selection_offer)
2200 data_offer_destroy(input->selection_offer);
2201
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05002202 if (offer) {
2203 input->selection_offer = wl_data_offer_get_user_data(offer);
2204 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2205 *p = NULL;
2206 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002207}
2208
2209static const struct wl_data_device_listener data_device_listener = {
2210 data_device_data_offer,
2211 data_device_enter,
2212 data_device_leave,
2213 data_device_motion,
2214 data_device_drop,
2215 data_device_selection
2216};
2217
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218void
2219input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2220{
2221 struct display *display = input->display;
2222 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002223 struct cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002225 if (pointer == input->current_cursor)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 return;
2227
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07002228 if (display->cursors[pointer].n_images == 0)
2229 return;
2230
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002231 image = &display->cursors[pointer].images[0];
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002233 if (!image->surface)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234 return;
2235
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002236 input->current_cursor = pointer;
2237 buffer = display_get_buffer_for_surface(display, image->surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238 wl_input_device_attach(input->input_device, time, buffer,
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03002239 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240}
2241
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002242struct wl_data_device *
2243input_get_data_device(struct input *input)
2244{
2245 return input->data_device;
2246}
2247
2248void
2249input_set_selection(struct input *input,
2250 struct wl_data_source *source, uint32_t time)
2251{
2252 wl_data_device_set_selection(input->data_device, source, time);
2253}
2254
2255void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002256input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002258 wl_data_offer_accept(input->drag_offer->offer,
2259 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002260}
2261
2262static void
2263offer_io_func(struct task *task, uint32_t events)
2264{
2265 struct data_offer *offer =
2266 container_of(task, struct data_offer, io_task);
2267 unsigned int len;
2268 char buffer[4096];
2269
2270 len = read(offer->fd, buffer, sizeof buffer);
2271 offer->func(buffer, len,
2272 offer->x, offer->y, offer->user_data);
2273
2274 if (len == 0) {
2275 close(offer->fd);
2276 data_offer_destroy(offer);
2277 }
2278}
2279
2280static void
2281data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2282 data_func_t func, void *user_data)
2283{
2284 int p[2];
2285
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02002286 if (pipe2(p, O_CLOEXEC) == -1)
2287 return;
2288
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002289 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2290 close(p[1]);
2291
2292 offer->io_task.run = offer_io_func;
2293 offer->fd = p[0];
2294 offer->func = func;
2295 offer->refcount++;
2296 offer->user_data = user_data;
2297
2298 display_watch_fd(offer->input->display,
2299 offer->fd, EPOLLIN, &offer->io_task);
2300}
2301
2302void
2303input_receive_drag_data(struct input *input, const char *mime_type,
2304 data_func_t func, void *data)
2305{
2306 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2307 input->drag_offer->x = input->sx;
2308 input->drag_offer->y = input->sy;
2309}
2310
2311int
2312input_receive_selection_data(struct input *input, const char *mime_type,
2313 data_func_t func, void *data)
2314{
2315 char **p;
2316
2317 if (input->selection_offer == NULL)
2318 return -1;
2319
2320 for (p = input->selection_offer->types.data; *p; p++)
2321 if (strcmp(mime_type, *p) == 0)
2322 break;
2323
2324 if (*p == NULL)
2325 return -1;
2326
2327 data_offer_receive_data(input->selection_offer,
2328 mime_type, func, data);
2329 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002330}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002331
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002332int
2333input_receive_selection_data_to_fd(struct input *input,
2334 const char *mime_type, int fd)
2335{
2336 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2337
2338 return 0;
2339}
2340
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002341void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002342window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002343{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002344 if (!window->shell_surface)
2345 return;
2346
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002347 wl_shell_surface_move(window->shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002348 input->input_device, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002349}
2350
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002351static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002352idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002353{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002354 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002355
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002356 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002357 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002358 widget_set_allocation(widget,
2359 window->pending_allocation.x,
2360 window->pending_allocation.y,
2361 window->pending_allocation.width,
2362 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002363
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002364 if (window->input_region) {
2365 wl_region_destroy(window->input_region);
2366 window->input_region = NULL;
2367 }
2368
2369 if (window->opaque_region) {
2370 wl_region_destroy(window->opaque_region);
2371 window->opaque_region = NULL;
2372 }
2373
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002374 if (widget->resize_handler)
2375 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002376 widget->allocation.width,
2377 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002378 widget->user_data);
2379
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002380 if (window->allocation.width != widget->allocation.width ||
2381 window->allocation.height != widget->allocation.height) {
2382 window->allocation = widget->allocation;
2383 window_schedule_redraw(window);
2384 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002385}
2386
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002387void
2388window_schedule_resize(struct window *window, int width, int height)
2389{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002390 window->pending_allocation.x = 0;
2391 window->pending_allocation.y = 0;
2392 window->pending_allocation.width = width;
2393 window->pending_allocation.height = height;
2394
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002395 window->resize_needed = 1;
2396 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002397}
2398
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002399void
2400widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2401{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002402 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002403}
2404
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002405static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06002406handle_ping(void *data, struct wl_shell_surface *shell_surface,
2407 uint32_t serial)
2408{
2409 wl_shell_surface_pong(shell_surface, serial);
2410}
2411
2412static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002413handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002414 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002415{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002416 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002417
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002418 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002419 return;
2420
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002421 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002422 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002423}
2424
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002425static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002426menu_destroy(struct menu *menu)
2427{
2428 widget_destroy(menu->widget);
2429 window_destroy(menu->window);
2430 free(menu);
2431}
2432
2433static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002434handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2435{
2436 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002437 struct menu *menu = window->widget->user_data;
2438
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002439 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002440 * device. Or just use wl_callback. And this really needs to
2441 * be a window vfunc that the menu can set. And we need the
2442 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002443
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002444 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002445 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002446 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002447}
2448
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002449static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002450 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002451 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002452 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002453};
2454
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002455void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002456window_get_allocation(struct window *window,
2457 struct rectangle *allocation)
2458{
2459 *allocation = window->allocation;
2460}
2461
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002462static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002463widget_redraw(struct widget *widget)
2464{
2465 struct widget *child;
2466
2467 if (widget->redraw_handler)
2468 widget->redraw_handler(widget, widget->user_data);
2469 wl_list_for_each(child, &widget->child_list, link)
2470 widget_redraw(child);
2471}
2472
2473static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002474frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2475{
2476 struct window *window = data;
2477
2478 wl_callback_destroy(callback);
2479 window->redraw_scheduled = 0;
2480 if (window->redraw_needed)
2481 window_schedule_redraw(window);
2482}
2483
2484static const struct wl_callback_listener listener = {
2485 frame_callback
2486};
2487
2488static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002489idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002490{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002491 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002492 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002493
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002494 if (window->resize_needed)
2495 idle_resize(window);
2496
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002497 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002498 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002499 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002500 window->redraw_needed = 0;
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002501 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002502
2503 callback = wl_surface_frame(window->surface);
2504 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002505}
2506
2507void
2508window_schedule_redraw(struct window *window)
2509{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002510 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002511 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002512 window->redraw_task.run = idle_redraw;
2513 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002514 window->redraw_scheduled = 1;
2515 }
2516}
2517
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002518void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002519window_set_custom(struct window *window)
2520{
2521 window->type = TYPE_CUSTOM;
2522}
2523
2524void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002525window_set_fullscreen(struct window *window, int fullscreen)
2526{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002527 if (!window->display->shell)
2528 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002529
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002530 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002531 return;
2532
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002533 if (fullscreen) {
2534 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002535 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002536 wl_shell_surface_set_fullscreen(window->shell_surface,
2537 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2538 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002539 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002540 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002541 wl_shell_surface_set_toplevel(window->shell_surface);
2542 window_schedule_resize(window,
2543 window->saved_allocation.width,
2544 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002545 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002546}
2547
2548void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002549window_set_maximized(struct window *window, int maximized)
2550{
2551 if (!window->display->shell)
2552 return;
2553
2554 if ((window->type == TYPE_MAXIMIZED) == maximized)
2555 return;
2556
2557 if (window->type == TYPE_TOPLEVEL) {
2558 window->saved_allocation = window->allocation;
2559 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2560 window->type = TYPE_MAXIMIZED;
2561 } else {
2562 wl_shell_surface_set_toplevel(window->shell_surface);
2563 window->type = TYPE_TOPLEVEL;
2564 window_schedule_resize(window,
2565 window->saved_allocation.width,
2566 window->saved_allocation.height);
2567 }
2568}
2569
2570void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002571window_set_user_data(struct window *window, void *data)
2572{
2573 window->user_data = data;
2574}
2575
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002576void *
2577window_get_user_data(struct window *window)
2578{
2579 return window->user_data;
2580}
2581
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002582void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002583window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002584 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002585{
2586 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002587}
2588
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002589void
2590window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002591 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002592{
2593 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002594}
2595
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002596void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002597window_set_data_handler(struct window *window, window_data_handler_t handler)
2598{
2599 window->data_handler = handler;
2600}
2601
2602void
2603window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2604{
2605 window->drop_handler = handler;
2606}
2607
2608void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002609window_set_close_handler(struct window *window,
2610 window_close_handler_t handler)
2611{
2612 window->close_handler = handler;
2613}
2614
2615void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002616window_set_title(struct window *window, const char *title)
2617{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002618 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002619 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002620 if (window->shell_surface)
2621 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002622}
2623
2624const char *
2625window_get_title(struct window *window)
2626{
2627 return window->title;
2628}
2629
2630void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002631window_damage(struct window *window, int32_t x, int32_t y,
2632 int32_t width, int32_t height)
2633{
2634 wl_surface_damage(window->surface, x, y, width, height);
2635}
2636
Casey Dahlin9074db52012-04-19 22:50:09 -04002637static void
2638surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01002639 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04002640{
Rob Bradford7507b572012-05-15 17:55:34 +01002641 struct window *window = data;
2642 struct output *output;
2643 struct output *output_found = NULL;
2644 struct window_output *window_output;
2645
2646 wl_list_for_each(output, &window->display->output_list, link) {
2647 if (output->output == wl_output) {
2648 output_found = output;
2649 break;
2650 }
2651 }
2652
2653 if (!output_found)
2654 return;
2655
2656 window_output = malloc (sizeof *window_output);
2657 window_output->output = output_found;
2658
2659 wl_list_insert (&window->window_output_list, &window_output->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002660}
2661
2662static void
2663surface_leave(void *data,
2664 struct wl_surface *wl_surface, struct wl_output *output)
2665{
Rob Bradford7507b572012-05-15 17:55:34 +01002666 struct window *window = data;
2667 struct window_output *window_output;
2668 struct window_output *window_output_found = NULL;
2669
2670 wl_list_for_each(window_output, &window->window_output_list, link) {
2671 if (window_output->output->output == output) {
2672 window_output_found = window_output;
2673 break;
2674 }
2675 }
2676
2677 if (window_output_found) {
2678 wl_list_remove(&window_output_found->link);
2679 free(window_output_found);
2680 }
Casey Dahlin9074db52012-04-19 22:50:09 -04002681}
2682
2683static const struct wl_surface_listener surface_listener = {
2684 surface_enter,
2685 surface_leave
2686};
2687
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002688static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002689window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002690{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002691 struct window *window;
2692
2693 window = malloc(sizeof *window);
2694 if (window == NULL)
2695 return NULL;
2696
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002697 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002698 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002699 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002700 window->surface = wl_compositor_create_surface(display->compositor);
Casey Dahlin9074db52012-04-19 22:50:09 -04002701 wl_surface_add_listener(window->surface, &surface_listener, window);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002702 if (display->shell) {
2703 window->shell_surface =
2704 wl_shell_get_shell_surface(display->shell,
2705 window->surface);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04002706 if (window->title)
2707 wl_shell_surface_set_title(window->shell_surface,
2708 window->title);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002709 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002710 window->allocation.x = 0;
2711 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002712 window->allocation.width = 0;
2713 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002714 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002715 window->transparent = 1;
Kristian Høgsberg407ef642012-04-27 17:17:12 -04002716 window->type = TYPE_NONE;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002717 window->input_region = NULL;
2718 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002719
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002720 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002721#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002722 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002723#else
2724 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2725#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002726 else
2727 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002728
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002729 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002730 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04002731 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002732
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002733 if (window->shell_surface) {
2734 wl_shell_surface_set_user_data(window->shell_surface, window);
2735 wl_shell_surface_add_listener(window->shell_surface,
2736 &shell_surface_listener, window);
2737 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002738
Rob Bradford7507b572012-05-15 17:55:34 +01002739 wl_list_init (&window->window_output_list);
2740
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002741 return window;
2742}
2743
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002744struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002745window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002746{
2747 struct window *window;
2748
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002749 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002750 if (!window)
2751 return NULL;
2752
2753 return window;
2754}
2755
2756struct window *
2757window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002758 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002759{
2760 struct window *window;
2761
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002762 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002763 if (!window)
2764 return NULL;
2765
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002766 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002767 window->x = x;
2768 window->y = y;
2769
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002770 if (display->shell)
2771 wl_shell_surface_set_transient(window->shell_surface,
2772 window->parent->shell_surface,
2773 window->x, window->y, 0);
2774
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002775 return window;
2776}
2777
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002778static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002779menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002780{
2781 int next;
2782
2783 next = (sy - 8) / 20;
2784 if (menu->current != next) {
2785 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002786 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002787 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002788}
2789
2790static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002791menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002792 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002793 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002794{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002795 struct menu *menu = data;
2796
2797 if (widget == menu->widget)
2798 menu_set_item(data, y);
2799
2800 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002801}
2802
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002803static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002804menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002805 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002806{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002807 struct menu *menu = data;
2808
2809 if (widget == menu->widget)
2810 menu_set_item(data, y);
2811
2812 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002813}
2814
2815static void
2816menu_leave_handler(struct widget *widget, struct input *input, void *data)
2817{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002818 struct menu *menu = data;
2819
2820 if (widget == menu->widget)
2821 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002822}
2823
2824static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002825menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002826 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002827 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002828
2829{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002830 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002831
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002832 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002833 /* Either relase after press-drag-release or
2834 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002835 menu->func(menu->window->parent,
2836 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002837 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002838 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002839 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002840}
2841
2842static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002843menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002844{
2845 cairo_t *cr;
2846 const int32_t r = 3, margin = 3;
2847 struct menu *menu = data;
2848 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002849 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002850
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002851 cr = cairo_create(window->cairo_surface);
2852 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2853 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2854 cairo_paint(cr);
2855
2856 width = window->allocation.width;
2857 height = window->allocation.height;
2858 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002859
2860 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002861 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2862 cairo_fill(cr);
2863
2864 for (i = 0; i < menu->count; i++) {
2865 if (i == menu->current) {
2866 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2867 cairo_rectangle(cr, margin, i * 20 + margin,
2868 width - 2 * margin, 20);
2869 cairo_fill(cr);
2870 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2871 cairo_move_to(cr, 10, i * 20 + 16);
2872 cairo_show_text(cr, menu->entries[i]);
2873 } else {
2874 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2875 cairo_move_to(cr, 10, i * 20 + 16);
2876 cairo_show_text(cr, menu->entries[i]);
2877 }
2878 }
2879
2880 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002881}
2882
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002883void
2884window_show_menu(struct display *display,
2885 struct input *input, uint32_t time, struct window *parent,
2886 int32_t x, int32_t y,
2887 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002888{
2889 struct window *window;
2890 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002891 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002892
2893 menu = malloc(sizeof *menu);
2894 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002895 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002896
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002897 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002898 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002899 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002900
2901 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002902 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002903 menu->entries = entries;
2904 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002905 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002906 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002907 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002908 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002909 window->type = TYPE_MENU;
2910 window->x = x;
2911 window->y = y;
2912
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04002913 input_ungrab(input);
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04002914 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2915 display_get_serial(window->display),
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002916 window->parent->shell_surface,
2917 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002918
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002919 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002920 widget_set_enter_handler(menu->widget, menu_enter_handler);
2921 widget_set_leave_handler(menu->widget, menu_leave_handler);
2922 widget_set_motion_handler(menu->widget, menu_motion_handler);
2923 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002924
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002925 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002926 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002927}
2928
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002929void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002930window_set_buffer_type(struct window *window, enum window_buffer_type type)
2931{
2932 window->buffer_type = type;
2933}
2934
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002935
2936static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002937display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002938 struct wl_output *wl_output,
2939 int x, int y,
2940 int physical_width,
2941 int physical_height,
2942 int subpixel,
2943 const char *make,
2944 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002945{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002946 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002947
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002948 output->allocation.x = x;
2949 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002950}
2951
2952static void
2953display_handle_mode(void *data,
2954 struct wl_output *wl_output,
2955 uint32_t flags,
2956 int width,
2957 int height,
2958 int refresh)
2959{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002960 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002961 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002962
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002963 if (flags & WL_OUTPUT_MODE_CURRENT) {
2964 output->allocation.width = width;
2965 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002966 if (display->output_configure_handler)
2967 (*display->output_configure_handler)(
2968 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002969 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002970}
2971
2972static const struct wl_output_listener output_listener = {
2973 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002974 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002975};
2976
2977static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002978display_add_output(struct display *d, uint32_t id)
2979{
2980 struct output *output;
2981
2982 output = malloc(sizeof *output);
2983 if (output == NULL)
2984 return;
2985
2986 memset(output, 0, sizeof *output);
2987 output->display = d;
2988 output->output =
2989 wl_display_bind(d->display, id, &wl_output_interface);
2990 wl_list_insert(d->output_list.prev, &output->link);
2991
2992 wl_output_add_listener(output->output, &output_listener, output);
2993}
2994
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002995static void
2996output_destroy(struct output *output)
2997{
2998 if (output->destroy_handler)
2999 (*output->destroy_handler)(output, output->user_data);
3000
3001 wl_output_destroy(output->output);
3002 wl_list_remove(&output->link);
3003 free(output);
3004}
3005
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003006void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003007display_set_output_configure_handler(struct display *display,
3008 display_output_handler_t handler)
3009{
3010 struct output *output;
3011
3012 display->output_configure_handler = handler;
3013 if (!handler)
3014 return;
3015
3016 wl_list_for_each(output, &display->output_list, link)
3017 (*display->output_configure_handler)(output,
3018 display->user_data);
3019}
3020
3021void
3022output_set_user_data(struct output *output, void *data)
3023{
3024 output->user_data = data;
3025}
3026
3027void *
3028output_get_user_data(struct output *output)
3029{
3030 return output->user_data;
3031}
3032
3033void
3034output_set_destroy_handler(struct output *output,
3035 display_output_handler_t handler)
3036{
3037 output->destroy_handler = handler;
3038 /* FIXME: implement this, once we have way to remove outputs */
3039}
3040
3041void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003042output_get_allocation(struct output *output, struct rectangle *allocation)
3043{
3044 *allocation = output->allocation;
3045}
3046
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003047struct wl_output *
3048output_get_wl_output(struct output *output)
3049{
3050 return output->output;
3051}
3052
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003053static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003054display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003055{
3056 struct input *input;
3057
3058 input = malloc(sizeof *input);
3059 if (input == NULL)
3060 return;
3061
3062 memset(input, 0, sizeof *input);
3063 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003064 input->input_device =
3065 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003066 input->pointer_focus = NULL;
3067 input->keyboard_focus = NULL;
3068 wl_list_insert(d->input_list.prev, &input->link);
3069
3070 wl_input_device_add_listener(input->input_device,
3071 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003072 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003073
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003074 input->data_device =
3075 wl_data_device_manager_get_data_device(d->data_device_manager,
3076 input->input_device);
3077 wl_data_device_add_listener(input->data_device,
3078 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003079}
3080
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003081static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02003082input_destroy(struct input *input)
3083{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05003084 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003085 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003086
3087 if (input->drag_offer)
3088 data_offer_destroy(input->drag_offer);
3089
3090 if (input->selection_offer)
3091 data_offer_destroy(input->selection_offer);
3092
3093 wl_data_device_destroy(input->data_device);
3094 wl_list_remove(&input->link);
3095 wl_input_device_destroy(input->input_device);
3096 free(input);
3097}
3098
3099static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003100display_handle_global(struct wl_display *display, uint32_t id,
3101 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003102{
3103 struct display *d = data;
3104
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003105 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003106 d->compositor =
3107 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003108 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003109 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003110 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04003111 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003112 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003113 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04003114 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04003115 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003116 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3117 d->data_device_manager =
3118 wl_display_bind(display, id,
3119 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003120 }
3121}
3122
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003123static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003124init_xkb(struct display *d)
3125{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003126 d->xkb.names.rules = "evdev";
3127 d->xkb.names.model = "pc105";
3128 d->xkb.names.layout = (char *) option_xkb_layout;
3129 d->xkb.names.variant = (char *) option_xkb_variant;
3130 d->xkb.names.options = (char *) option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003131
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003132 d->xkb.context = xkb_context_new(0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003133 if (!d->xkb.context) {
3134 fprintf(stderr, "Failed to create XKB context\n");
3135 exit(1);
3136 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003137
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04003138 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
Kristian Høgsberg70163132012-05-08 15:55:39 -04003139 if (!d->xkb.keymap) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003140 fprintf(stderr, "Failed to compile keymap\n");
3141 exit(1);
3142 }
Kristian Høgsberg70163132012-05-08 15:55:39 -04003143
3144 d->xkb.state = xkb_state_new(d->xkb.keymap);
3145 if (!d->xkb.state) {
3146 fprintf(stderr, "Failed to create XKB state\n");
3147 exit(1);
3148 }
3149
3150 d->xkb.control_mask =
3151 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3152 d->xkb.alt_mask =
3153 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3154 d->xkb.shift_mask =
3155 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3156
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003157}
3158
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003159static void
3160fini_xkb(struct display *display)
3161{
Kristian Høgsberg70163132012-05-08 15:55:39 -04003162 xkb_state_unref(display->xkb.state);
3163 xkb_map_unref(display->xkb.keymap);
3164 xkb_context_unref(display->xkb.context);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003165}
3166
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003167#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05003168static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003169init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05003170{
3171 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003172 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003173
Rob Clark6396ed32012-03-11 19:48:41 -05003174#ifdef USE_CAIRO_GLESV2
3175# define GL_BIT EGL_OPENGL_ES2_BIT
3176#else
3177# define GL_BIT EGL_OPENGL_BIT
3178#endif
3179
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003180 static const EGLint argb_cfg_attribs[] = {
3181 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003182 EGL_RED_SIZE, 1,
3183 EGL_GREEN_SIZE, 1,
3184 EGL_BLUE_SIZE, 1,
3185 EGL_ALPHA_SIZE, 1,
3186 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05003187 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003188 EGL_NONE
3189 };
Yuval Fledel45568f62010-12-06 09:18:12 -05003190
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003191#ifdef USE_CAIRO_GLESV2
3192 static const EGLint context_attribs[] = {
3193 EGL_CONTEXT_CLIENT_VERSION, 2,
3194 EGL_NONE
3195 };
3196 EGLint api = EGL_OPENGL_ES_API;
3197#else
3198 EGLint *context_attribs = NULL;
3199 EGLint api = EGL_OPENGL_API;
3200#endif
3201
Kristian Høgsberg91342c62011-04-14 14:44:58 -04003202 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05003203 if (!eglInitialize(d->dpy, &major, &minor)) {
3204 fprintf(stderr, "failed to initialize display\n");
3205 return -1;
3206 }
3207
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003208 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003209 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3210 return -1;
3211 }
3212
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003213 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3214 &d->argb_config, 1, &n) || n != 1) {
3215 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01003216 return -1;
3217 }
3218
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003219 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05003220 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003221 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05003222 fprintf(stderr, "failed to create context\n");
3223 return -1;
3224 }
3225
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003226 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01003227 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05003228 return -1;
3229 }
3230
Kristian Høgsberg8def2642011-01-14 17:41:33 -05003231#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02003232 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3233 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3234 fprintf(stderr, "failed to get cairo egl argb device\n");
3235 return -1;
3236 }
Yuval Fledel45568f62010-12-06 09:18:12 -05003237#endif
3238
3239 return 0;
3240}
3241
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003242static void
3243fini_egl(struct display *display)
3244{
3245#ifdef HAVE_CAIRO_EGL
3246 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003247#endif
3248
3249 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3250 EGL_NO_CONTEXT);
3251
3252 eglTerminate(display->dpy);
3253 eglReleaseThread();
3254}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003255#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003256
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003257static int
3258event_mask_update(uint32_t mask, void *data)
3259{
3260 struct display *d = data;
3261
3262 d->mask = mask;
3263
3264 return 0;
3265}
3266
3267static void
3268handle_display_data(struct task *task, uint32_t events)
3269{
3270 struct display *display =
3271 container_of(task, struct display, display_task);
3272
3273 wl_display_iterate(display->display, display->mask);
3274}
3275
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003276struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003277display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003278{
3279 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003280
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003281 argc = parse_options(xkb_options,
3282 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04003283
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003284 d = malloc(sizeof *d);
3285 if (d == NULL)
3286 return NULL;
3287
Tim Wiederhake81bd9792011-01-23 23:25:26 +01003288 memset(d, 0, sizeof *d);
3289
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003290 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003291 if (d->display == NULL) {
3292 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003293 return NULL;
3294 }
3295
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003296 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3297 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3298 d->display_task.run = handle_display_data;
3299 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3300
3301 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003302 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003303 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003304
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003305 /* Set up listener so we'll catch all events. */
3306 wl_display_add_global_listener(d->display,
3307 display_handle_global, d);
3308
3309 /* Process connection events. */
3310 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003311#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05003312 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003313 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003314#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003315
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003316 d->image_target_texture_2d =
3317 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3318 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3319 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3320
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003321 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003322
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003323 display_render_theme(&d->theme);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003324
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003325 wl_list_init(&d->window_list);
3326
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003327 init_xkb(d);
3328
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003329 return d;
3330}
3331
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003332static void
3333display_destroy_outputs(struct display *display)
3334{
3335 struct output *tmp;
3336 struct output *output;
3337
3338 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3339 output_destroy(output);
3340}
3341
Pekka Paalanene1207c72011-12-16 12:02:09 +02003342static void
3343display_destroy_inputs(struct display *display)
3344{
3345 struct input *tmp;
3346 struct input *input;
3347
3348 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3349 input_destroy(input);
3350}
3351
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003352void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003353display_destroy(struct display *display)
3354{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003355 if (!wl_list_empty(&display->window_list))
3356 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3357
3358 if (!wl_list_empty(&display->deferred_list))
3359 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3360
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003361 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003362 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003363
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003364 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003365
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04003366 fini_theme(&display->theme);
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +03003367 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003368
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003369#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003370 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003371#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003372
Pekka Paalanenc2052982011-12-16 11:41:32 +02003373 if (display->shell)
3374 wl_shell_destroy(display->shell);
3375
3376 if (display->shm)
3377 wl_shm_destroy(display->shm);
3378
3379 if (display->data_device_manager)
3380 wl_data_device_manager_destroy(display->data_device_manager);
3381
3382 wl_compositor_destroy(display->compositor);
3383
3384 close(display->epoll_fd);
3385
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003386 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003387 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003388 free(display);
3389}
3390
3391void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003392display_set_user_data(struct display *display, void *data)
3393{
3394 display->user_data = data;
3395}
3396
3397void *
3398display_get_user_data(struct display *display)
3399{
3400 return display->user_data;
3401}
3402
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003403struct wl_display *
3404display_get_display(struct display *display)
3405{
3406 return display->display;
3407}
3408
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003409struct output *
3410display_get_output(struct display *display)
3411{
3412 return container_of(display->output_list.next, struct output, link);
3413}
3414
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003415struct wl_compositor *
3416display_get_compositor(struct display *display)
3417{
3418 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003419}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003420
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003421uint32_t
3422display_get_serial(struct display *display)
3423{
3424 return display->serial;
3425}
3426
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003427EGLDisplay
3428display_get_egl_display(struct display *d)
3429{
3430 return d->dpy;
3431}
3432
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003433struct wl_data_source *
3434display_create_data_source(struct display *display)
3435{
3436 return wl_data_device_manager_create_data_source(display->data_device_manager);
3437}
3438
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003439EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003440display_get_argb_egl_config(struct display *d)
3441{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003442 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003443}
3444
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003445struct wl_shell *
3446display_get_shell(struct display *display)
3447{
3448 return display->shell;
3449}
3450
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003451int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003452display_acquire_window_surface(struct display *display,
3453 struct window *window,
3454 EGLContext ctx)
3455{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003456#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003457 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003458 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003459
3460 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003461 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003462 device = cairo_surface_get_device(window->cairo_surface);
3463 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003464 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003465
Benjamin Franzke0c991632011-09-27 21:57:31 +02003466 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003467 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003468 ctx = display->argb_ctx;
3469 else
3470 assert(0);
3471 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003472
3473 data = cairo_surface_get_user_data(window->cairo_surface,
3474 &surface_data_key);
3475
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003476 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003477 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003478 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3479 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003480
3481 return 0;
3482#else
3483 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003484#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003485}
3486
3487void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003488display_release_window_surface(struct display *display,
3489 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003490{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003491#ifdef HAVE_CAIRO_EGL
3492 cairo_device_t *device;
3493
3494 device = cairo_surface_get_device(window->cairo_surface);
3495 if (!device)
3496 return;
3497
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003498 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003499 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003500 cairo_device_release(device);
3501#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003502}
3503
3504void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003505display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003506{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003507 wl_list_insert(&display->deferred_list, &task->link);
3508}
3509
3510void
3511display_watch_fd(struct display *display,
3512 int fd, uint32_t events, struct task *task)
3513{
3514 struct epoll_event ep;
3515
3516 ep.events = events;
3517 ep.data.ptr = task;
3518 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3519}
3520
3521void
3522display_run(struct display *display)
3523{
3524 struct task *task;
3525 struct epoll_event ep[16];
3526 int i, count;
3527
Pekka Paalanen826d7952011-12-15 10:14:07 +02003528 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003529 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003530 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003531
3532 while (!wl_list_empty(&display->deferred_list)) {
3533 task = container_of(display->deferred_list.next,
3534 struct task, link);
3535 wl_list_remove(&task->link);
3536 task->run(task, 0);
3537 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003538
3539 if (!display->running)
3540 break;
3541
3542 wl_display_flush(display->display);
3543
3544 count = epoll_wait(display->epoll_fd,
3545 ep, ARRAY_LENGTH(ep), -1);
3546 for (i = 0; i < count; i++) {
3547 task = ep[i].data.ptr;
3548 task->run(task, ep[i].events);
3549 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003550 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003551}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003552
3553void
3554display_exit(struct display *display)
3555{
3556 display->running = 0;
3557}