blob: de69003659e3f6f037f070b6ee2700583065c377 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400126 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400127 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400128 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500134 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500136 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500137 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400138 window_data_handler_t data_handler;
139 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500140 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400141
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200142 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500143 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400146 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147};
148
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500149struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500150 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500151 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152 struct wl_list link;
153 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500154 widget_resize_handler_t resize_handler;
155 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500156 widget_enter_handler_t enter_handler;
157 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500158 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500159 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400160 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500161 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400162};
163
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164struct input {
165 struct display *display;
166 struct wl_input_device *input_device;
167 struct window *pointer_focus;
168 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400169 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400170 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200171 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400173
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500174 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500175 struct widget *grab;
176 uint32_t grab_button;
177
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400178 struct wl_data_device *data_device;
179 struct data_offer *drag_offer;
180 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400181};
182
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183struct output {
184 struct display *display;
185 struct wl_output *output;
186 struct rectangle allocation;
187 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200188
189 display_output_handler_t destroy_handler;
190 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500191};
192
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500193struct frame {
194 struct widget *widget;
195 struct widget *child;
196 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400197 int width;
198 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500199};
200
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500201struct menu {
202 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500203 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500204 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205 const char **entries;
206 uint32_t time;
207 int current;
208 int count;
209 menu_func_t func;
210};
211
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400212enum {
213 POINTER_DEFAULT = 100,
214 POINTER_UNSET
215};
216
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500217enum window_location {
218 WINDOW_INTERIOR = 0,
219 WINDOW_RESIZING_TOP = 1,
220 WINDOW_RESIZING_BOTTOM = 2,
221 WINDOW_RESIZING_LEFT = 4,
222 WINDOW_RESIZING_TOP_LEFT = 5,
223 WINDOW_RESIZING_BOTTOM_LEFT = 6,
224 WINDOW_RESIZING_RIGHT = 8,
225 WINDOW_RESIZING_TOP_RIGHT = 9,
226 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
227 WINDOW_RESIZING_MASK = 15,
228 WINDOW_EXTERIOR = 16,
229 WINDOW_TITLEBAR = 17,
230 WINDOW_CLIENT_AREA = 18,
231};
232
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400233const char *option_xkb_layout = "us";
234const char *option_xkb_variant = "";
235const char *option_xkb_options = "";
236
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400237static const struct weston_option xkb_options[] = {
238 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
239 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
240 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400241};
242
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400243static const cairo_user_data_key_t surface_data_key;
244struct surface_data {
245 struct wl_buffer *buffer;
246};
247
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500248#define MULT(_d,c,a,t) \
249 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
250
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500251#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400252
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100253struct egl_window_surface_data {
254 struct display *display;
255 struct wl_surface *surface;
256 struct wl_egl_window *window;
257 EGLSurface surf;
258};
259
260static void
261egl_window_surface_data_destroy(void *p)
262{
263 struct egl_window_surface_data *data = p;
264 struct display *d = data->display;
265
266 eglDestroySurface(d->dpy, data->surf);
267 wl_egl_window_destroy(data->window);
268 data->surface = NULL;
269
270 free(p);
271}
272
273static cairo_surface_t *
274display_create_egl_window_surface(struct display *display,
275 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100277 struct rectangle *rectangle)
278{
279 cairo_surface_t *cairo_surface;
280 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400281 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200282 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100283
284 data = malloc(sizeof *data);
285 if (data == NULL)
286 return NULL;
287
288 data->display = display;
289 data->surface = surface;
290
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500291 config = display->argb_config;
292 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400294 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500299 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300
Benjamin Franzke0c991632011-09-27 21:57:31 +0200301 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 data->surf,
303 rectangle->width,
304 rectangle->height);
305
306 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
307 data, egl_window_surface_data_destroy);
308
309 return cairo_surface;
310}
311
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 EGLImageKHR image;
316 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319};
320
321static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500333 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335}
336
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_get_image_for_egl_image_surface(struct display *display,
339 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342
343 data = cairo_surface_get_user_data (surface, &surface_data_key);
344
345 return data->image;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 EGLDisplay dpy = display->dpy;
355 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
358 data = malloc(sizeof *data);
359 if (data == NULL)
360 return NULL;
361
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800362 data->display = display;
363
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400364 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000367 free(data);
368 return NULL;
369 }
370
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500371 data->device = display->argb_device;
372 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400373
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500374 data->image = display->create_image(dpy, NULL,
375 EGL_NATIVE_PIXMAP_KHR,
376 (EGLClientBuffer) data->pixmap,
377 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500378 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500379 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500380 free(data);
381 return NULL;
382 }
383
384 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400385 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500386
Benjamin Franzke0c991632011-09-27 21:57:31 +0200387 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500390 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 surface = cairo_gl_surface_create_for_texture(data->device,
394 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395 data->texture,
396 rectangle->width,
397 rectangle->height);
398
399 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500400 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
402 return surface;
403}
404
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500405static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500406display_create_egl_image_surface_from_file(struct display *display,
407 const char *filename,
408 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409{
410 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400411 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400414 image = load_image(filename);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200416 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000417 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400418 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000419 return NULL;
420 }
421
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800422 data = cairo_surface_get_user_data(surface, &surface_data_key);
423
Benjamin Franzke0c991632011-09-27 21:57:31 +0200424 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800425 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800426 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400427 GL_RGBA, GL_UNSIGNED_BYTE,
428 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200429 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400431 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433 return surface;
434}
435
436#endif
437
438struct wl_buffer *
439display_get_buffer_for_surface(struct display *display,
440 cairo_surface_t *surface)
441{
442 struct surface_data *data;
443
444 data = cairo_surface_get_user_data (surface, &surface_data_key);
445
446 return data->buffer;
447}
448
449struct shm_surface_data {
450 struct surface_data data;
451 void *map;
452 size_t length;
453};
454
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500455static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400456shm_surface_data_destroy(void *p)
457{
458 struct shm_surface_data *data = p;
459
460 wl_buffer_destroy(data->data.buffer);
461 munmap(data->map, data->length);
462}
463
Kristian Høgsberg16626282012-04-03 11:21:27 -0400464static struct wl_shm_pool *
465make_shm_pool(struct display *display, int size, void **data)
466{
467 char filename[] = "/tmp/wayland-shm-XXXXXX";
468 struct wl_shm_pool *pool;
469 int fd;
470
471 fd = mkstemp(filename);
472 if (fd < 0) {
473 fprintf(stderr, "open %s failed: %m\n", filename);
474 return NULL;
475 }
476 if (ftruncate(fd, size) < 0) {
477 fprintf(stderr, "ftruncate failed: %m\n");
478 close(fd);
479 return NULL;
480 }
481
482 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
483 unlink(filename);
484
485 if (*data == MAP_FAILED) {
486 fprintf(stderr, "mmap failed: %m\n");
487 close(fd);
488 return NULL;
489 }
490
491 pool = wl_shm_create_pool(display->shm, fd, size);
492
493 close(fd);
494
495 return pool;
496}
497
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500498static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501{
502 struct shm_surface_data *data;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400503 struct wl_shm_pool *pool;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400504 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400505 cairo_surface_t *surface;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400506 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507
508 data = malloc(sizeof *data);
509 if (data == NULL)
510 return NULL;
511
512 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
513 rectangle->width);
514 data->length = stride * rectangle->height;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400515 pool = make_shm_pool(display, data->length, &data->map);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516
517 surface = cairo_image_surface_create_for_data (data->map,
518 CAIRO_FORMAT_ARGB32,
519 rectangle->width,
520 rectangle->height,
521 stride);
522
523 cairo_surface_set_user_data (surface, &surface_data_key,
524 data, shm_surface_data_destroy);
525
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400526 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500527 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400528 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500529 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400530
Kristian Høgsberg16626282012-04-03 11:21:27 -0400531 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
532 rectangle->width,
533 rectangle->height,
534 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400535
Kristian Høgsberg16626282012-04-03 11:21:27 -0400536 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537
538 return surface;
539}
540
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500541static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542display_create_shm_surface_from_file(struct display *display,
543 const char *filename,
544 struct rectangle *rect)
545{
546 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400547 pixman_image_t *image;
548 void *dest;
549 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400551 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000554 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400555 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000556 return NULL;
557 }
558
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400559 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
560 dest = cairo_image_surface_get_data(surface);
561 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400563 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564
565 return surface;
566}
567
nobled7b87cb02011-02-01 18:51:47 +0000568static int
569check_size(struct rectangle *rect)
570{
571 if (rect->width && rect->height)
572 return 0;
573
574 fprintf(stderr, "tried to create surface of "
575 "width: %d, height: %d\n", rect->width, rect->height);
576 return -1;
577}
578
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400579cairo_surface_t *
580display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100581 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400582 struct rectangle *rectangle,
583 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584{
nobled7b87cb02011-02-01 18:51:47 +0000585 if (check_size(rectangle) < 0)
586 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500587#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500588 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100589 if (surface)
590 return display_create_egl_window_surface(display,
591 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400592 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100593 rectangle);
594 else
595 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400596 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100597 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500598 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400599#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400600 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601}
602
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500603static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604display_create_surface_from_file(struct display *display,
605 const char *filename,
606 struct rectangle *rectangle)
607{
nobled7b87cb02011-02-01 18:51:47 +0000608 if (check_size(rectangle) < 0)
609 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500610#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500611 if (display->dpy) {
612 return display_create_egl_image_surface_from_file(display,
613 filename,
614 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500615 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500617 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618}
Yuval Fledel45568f62010-12-06 09:18:12 -0500619 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400620 const char *filename;
621 int hotspot_x, hotspot_y;
622} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500623 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
624 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
625 { DATADIR "/weston/bottom_side.png", 16, 20 },
626 { DATADIR "/weston/grabbing.png", 20, 17 },
627 { DATADIR "/weston/left_ptr.png", 10, 5 },
628 { DATADIR "/weston/left_side.png", 10, 20 },
629 { DATADIR "/weston/right_side.png", 30, 19 },
630 { DATADIR "/weston/top_left_corner.png", 8, 8 },
631 { DATADIR "/weston/top_right_corner.png", 26, 8 },
632 { DATADIR "/weston/top_side.png", 18, 8 },
633 { DATADIR "/weston/xterm.png", 15, 15 },
634 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635};
636
637static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400638create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400639{
640 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400641 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400642 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400643
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400644 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400645 display->pointer_surfaces =
646 malloc(count * sizeof *display->pointer_surfaces);
647 rect.width = width;
648 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400649 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400650 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651 display_create_surface_from_file(display,
652 pointer_images[i].filename,
653 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100654 if (!display->pointer_surfaces[i]) {
655 fprintf(stderr, "Error loading pointer image: %s\n",
656 pointer_images[i].filename);
657 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400658 }
659
660}
661
Pekka Paalanen325bb602011-12-19 10:31:45 +0200662static void
663destroy_pointer_surfaces(struct display *display)
664{
665 int i, count;
666
667 count = ARRAY_LENGTH(pointer_images);
668 for (i = 0; i < count; ++i) {
669 if (display->pointer_surfaces[i])
670 cairo_surface_destroy(display->pointer_surfaces[i]);
671 }
672 free(display->pointer_surfaces);
673}
674
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400675cairo_surface_t *
676display_get_pointer_surface(struct display *display, int pointer,
677 int *width, int *height,
678 int *hotspot_x, int *hotspot_y)
679{
680 cairo_surface_t *surface;
681
682 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500683#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400684 *width = cairo_gl_surface_get_width(surface);
685 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000686#else
687 *width = cairo_image_surface_get_width(surface);
688 *height = cairo_image_surface_get_height(surface);
689#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690 *hotspot_x = pointer_images[pointer].hotspot_x;
691 *hotspot_y = pointer_images[pointer].hotspot_y;
692
693 return cairo_surface_reference(surface);
694}
695
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400696static void
697window_attach_surface(struct window *window);
698
699static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400700free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400701{
702 struct window *window = data;
703
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400704 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400705 cairo_surface_destroy(window->pending_surface);
706 window->pending_surface = NULL;
707 if (window->cairo_surface)
708 window_attach_surface(window);
709}
710
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400711static const struct wl_callback_listener free_surface_listener = {
712 free_surface
713};
714
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500715static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200716window_get_resize_dx_dy(struct window *window, int *x, int *y)
717{
718 if (window->resize_edges & WINDOW_RESIZING_LEFT)
719 *x = window->server_allocation.width - window->allocation.width;
720 else
721 *x = 0;
722
723 if (window->resize_edges & WINDOW_RESIZING_TOP)
724 *y = window->server_allocation.height -
725 window->allocation.height;
726 else
727 *y = 0;
728
729 window->resize_edges = 0;
730}
731
732static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500733window_attach_surface(struct window *window)
734{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400735 struct display *display = window->display;
736 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400737 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000738#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100739 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000740#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500741 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742
743 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000744#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100745 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
746 data = cairo_surface_get_user_data(window->cairo_surface,
747 &surface_data_key);
748
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100749 cairo_gl_surface_swapbuffers(window->cairo_surface);
750 wl_egl_window_get_attached_size(data->window,
751 &window->server_allocation.width,
752 &window->server_allocation.height);
753 break;
754 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000755#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100756 case WINDOW_BUFFER_TYPE_SHM:
757 if (window->pending_surface != NULL)
758 return;
759
760 window->pending_surface = window->cairo_surface;
761 window->cairo_surface = NULL;
762
763 buffer =
764 display_get_buffer_for_surface(display,
765 window->pending_surface);
766
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200767 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100768 wl_surface_attach(window->surface, buffer, x, y);
769 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400770 cb = wl_display_sync(display->display);
771 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100772 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000773 default:
774 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100775 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500776
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500777 if (window->input_region) {
778 wl_surface_set_input_region(window->surface,
779 window->input_region);
780 wl_region_destroy(window->input_region);
781 window->input_region = NULL;
782 }
783
784 if (window->opaque_region) {
785 wl_surface_set_opaque_region(window->surface,
786 window->opaque_region);
787 wl_region_destroy(window->opaque_region);
788 window->opaque_region = NULL;
789 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500790
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500791 wl_surface_damage(window->surface, 0, 0,
792 window->allocation.width,
793 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500794}
795
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500796void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400797window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500798{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400799 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100800 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500801}
802
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400803void
804window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400805{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500806 cairo_surface_reference(surface);
807
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400808 if (window->cairo_surface != NULL)
809 cairo_surface_destroy(window->cairo_surface);
810
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500811 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400812}
813
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815static void
816window_resize_cairo_window_surface(struct window *window)
817{
818 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200819 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820
821 data = cairo_surface_get_user_data(window->cairo_surface,
822 &surface_data_key);
823
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200824 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 wl_egl_window_resize(data->window,
826 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200827 window->allocation.height,
828 x,y);
829
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 cairo_gl_surface_set_size(window->cairo_surface,
831 window->allocation.width,
832 window->allocation.height);
833}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400836struct display *
837window_get_display(struct window *window)
838{
839 return window->display;
840}
841
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400842void
843window_create_surface(struct window *window)
844{
845 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400846 uint32_t flags = 0;
847
848 if (!window->transparent)
849 flags = SURFACE_OPAQUE;
850
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500852#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
854 if (window->cairo_surface) {
855 window_resize_cairo_window_surface(window);
856 return;
857 }
858 surface = display_create_surface(window->display,
859 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400860 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500862 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400863 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400865 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866 break;
867#endif
868 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400869 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400870 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400871 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800872 default:
873 surface = NULL;
874 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400876
877 window_set_surface(window, surface);
878 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879}
880
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200881static void frame_destroy(struct frame *frame);
882
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400883void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500884window_destroy(struct window *window)
885{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200886 struct display *display = window->display;
887 struct input *input;
888
889 if (window->redraw_scheduled)
890 wl_list_remove(&window->redraw_task.link);
891
892 wl_list_for_each(input, &display->input_list, link) {
893 if (input->pointer_focus == window)
894 input->pointer_focus = NULL;
895 if (input->keyboard_focus == window)
896 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500897 if (input->focus_widget &&
898 input->focus_widget->window == window)
899 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200900 }
901
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500902 if (window->input_region)
903 wl_region_destroy(window->input_region);
904 if (window->opaque_region)
905 wl_region_destroy(window->opaque_region);
906
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200907 if (window->frame)
908 frame_destroy(window->frame);
909
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200910 if (window->shell_surface)
911 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500912 wl_surface_destroy(window->surface);
913 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200914
915 if (window->cairo_surface != NULL)
916 cairo_surface_destroy(window->cairo_surface);
917 if (window->pending_surface != NULL)
918 cairo_surface_destroy(window->pending_surface);
919
920 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500921 free(window);
922}
923
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500924static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500925widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400926{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500927 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400928
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500929 wl_list_for_each(child, &widget->child_list, link) {
930 target = widget_find_widget(child, x, y);
931 if (target)
932 return target;
933 }
934
935 if (widget->allocation.x <= x &&
936 x < widget->allocation.x + widget->allocation.width &&
937 widget->allocation.y <= y &&
938 y < widget->allocation.y + widget->allocation.height) {
939 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940 }
941
942 return NULL;
943}
944
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500945static struct widget *
946widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400947{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500948 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500950 widget = malloc(sizeof *widget);
951 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500952 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500953 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500954 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500955 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500956 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500957
958 return widget;
959}
960
961struct widget *
962window_add_widget(struct window *window, void *data)
963{
964 window->widget = widget_create(window, data);
965 wl_list_init(&window->widget->link);
966
967 return window->widget;
968}
969
970struct widget *
971widget_add_widget(struct widget *parent, void *data)
972{
973 struct widget *widget;
974
975 widget = widget_create(parent->window, data);
976 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500978 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400979}
980
981void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500982widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400983{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200984 struct display *display = widget->window->display;
985 struct input *input;
986
987 wl_list_for_each(input, &display->input_list, link) {
988 if (input->focus_widget == widget)
989 input->focus_widget = NULL;
990 }
991
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500992 wl_list_remove(&widget->link);
993 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400994}
995
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000}
1001
1002void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001003widget_set_size(struct widget *widget, int32_t width, int32_t height)
1004{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001005 widget->allocation.width = width;
1006 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001007}
1008
1009void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001010widget_set_allocation(struct widget *widget,
1011 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001012{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001013 widget->allocation.x = x;
1014 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001015 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001016}
1017
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001018void
1019widget_set_transparent(struct widget *widget, int transparent)
1020{
1021 widget->opaque = !transparent;
1022}
1023
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001024void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001025widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028}
1029
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001030void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001031widget_set_resize_handler(struct widget *widget,
1032 widget_resize_handler_t handler)
1033{
1034 widget->resize_handler = handler;
1035}
1036
1037void
1038widget_set_redraw_handler(struct widget *widget,
1039 widget_redraw_handler_t handler)
1040{
1041 widget->redraw_handler = handler;
1042}
1043
1044void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001045widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001046{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001047 widget->enter_handler = handler;
1048}
1049
1050void
1051widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1052{
1053 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001054}
1055
1056void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001057widget_set_motion_handler(struct widget *widget,
1058 widget_motion_handler_t handler)
1059{
1060 widget->motion_handler = handler;
1061}
1062
1063void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001064widget_set_button_handler(struct widget *widget,
1065 widget_button_handler_t handler)
1066{
1067 widget->button_handler = handler;
1068}
1069
1070void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001071widget_schedule_redraw(struct widget *widget)
1072{
1073 window_schedule_redraw(widget->window);
1074}
1075
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001076cairo_surface_t *
1077window_get_surface(struct window *window)
1078{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001079 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001080}
1081
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001082struct wl_surface *
1083window_get_wl_surface(struct window *window)
1084{
1085 return window->surface;
1086}
1087
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001088struct wl_shell_surface *
1089window_get_wl_shell_surface(struct window *window)
1090{
1091 return window->shell_surface;
1092}
1093
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001094static void
1095frame_resize_handler(struct widget *widget,
1096 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001097{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001098 struct frame *frame = data;
1099 struct widget *child = frame->child;
1100 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001101 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001102 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001103 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001104
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001105 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001106 decoration_width = (frame->width + frame->margin) * 2;
1107 decoration_height = frame->width +
1108 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001109
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001110 allocation.x = frame->width + frame->margin;
1111 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001112 allocation.width = width - decoration_width;
1113 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001114
1115 widget->window->input_region =
1116 wl_compositor_create_region(display->compositor);
1117 wl_region_add(widget->window->input_region,
1118 frame->margin, frame->margin,
1119 width - 2 * frame->margin,
1120 height - 2 * frame->margin);
1121
1122 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001123 } else {
1124 decoration_width = 0;
1125 decoration_height = 0;
1126
1127 allocation.x = 0;
1128 allocation.y = 0;
1129 allocation.width = width;
1130 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001131 opaque_margin = 0;
1132 }
1133
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001134 widget_set_allocation(child, allocation.x, allocation.y,
1135 allocation.width, allocation.height);
1136
1137 if (child->resize_handler)
1138 child->resize_handler(child,
1139 allocation.width,
1140 allocation.height,
1141 child->user_data);
1142
1143 widget_set_allocation(widget, 0, 0,
1144 child->allocation.width + decoration_width,
1145 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001146
1147 if (child->opaque) {
1148 widget->window->opaque_region =
1149 wl_compositor_create_region(display->compositor);
1150 wl_region_add(widget->window->opaque_region,
1151 opaque_margin, opaque_margin,
1152 widget->allocation.width - 2 * opaque_margin,
1153 widget->allocation.height - 2 * opaque_margin);
1154 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001155}
1156
1157static void
1158frame_redraw_handler(struct widget *widget, void *data)
1159{
1160 struct frame *frame = data;
1161 cairo_t *cr;
1162 cairo_text_extents_t extents;
1163 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001164 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165 struct window *window = widget->window;
1166
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001167 if (window->type == TYPE_FULLSCREEN)
1168 return;
1169
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001170 width = widget->allocation.width;
1171 height = widget->allocation.height;
1172
1173 cr = cairo_create(window->cairo_surface);
1174
1175 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1176 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1177 cairo_paint(cr);
1178
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001179 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001180 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001181 2, 2, width + 8, height + 8,
1182 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001183
1184 if (window->keyboard_device)
1185 source = window->display->active_frame;
1186 else
1187 source = window->display->inactive_frame;
1188
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001189 tile_source(cr, source,
1190 frame->margin, frame->margin,
1191 width - frame->margin * 2, height - frame->margin * 2,
1192 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001193
1194 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001195 cairo_select_font_face(cr, "sans",
1196 CAIRO_FONT_SLANT_NORMAL,
1197 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001198 cairo_set_font_size(cr, 14);
1199 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001200 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001201 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001202 if (window->keyboard_device) {
1203 cairo_move_to(cr, x + 1, y + 1);
1204 cairo_set_source_rgb(cr, 1, 1, 1);
1205 cairo_show_text(cr, window->title);
1206 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001207 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001208 cairo_show_text(cr, window->title);
1209 } else {
1210 cairo_move_to(cr, x, y);
1211 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1212 cairo_show_text(cr, window->title);
1213 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001214
1215 cairo_destroy(cr);
1216}
1217
1218static int
1219frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1220{
1221 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001222 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 const int grip_size = 8;
1224
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001225 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001226 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001227 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001228 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001230 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232 hlocation = WINDOW_RESIZING_RIGHT;
1233 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001235
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001236 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001237 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001239 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001241 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243 vlocation = WINDOW_RESIZING_BOTTOM;
1244 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 location = vlocation | hlocation;
1248 if (location & WINDOW_EXTERIOR)
1249 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001251 location = WINDOW_TITLEBAR;
1252 else if (location == WINDOW_INTERIOR)
1253 location = WINDOW_CLIENT_AREA;
1254
1255 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256}
1257
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001258static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001259frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264 switch (location) {
1265 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001268 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001270 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001272 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001274 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001276 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001277 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001278 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001280 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001281 case WINDOW_EXTERIOR:
1282 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001284 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286}
1287
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001288static void
1289frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001291 switch (index) {
1292 case 0: /* close */
1293 if (window->close_handler)
1294 window->close_handler(window->parent,
1295 window->user_data);
1296 else
1297 display_exit(window->display);
1298 break;
1299 case 1: /* fullscreen */
1300 /* we don't have a way to get out of fullscreen for now */
1301 window_set_fullscreen(window, 1);
1302 break;
1303 case 2: /* rotate */
1304 case 3: /* scale */
1305 break;
1306 }
1307}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001308
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001309void
1310window_show_frame_menu(struct window *window,
1311 struct input *input, uint32_t time)
1312{
1313 int32_t x, y;
1314
1315 static const char *entries[] = {
1316 "Close", "Fullscreen", "Rotate", "Scale"
1317 };
1318
1319 input_get_position(input, &x, &y);
1320 window_show_menu(window->display, input, time, window,
1321 x - 10, y - 10, frame_menu_func, entries, 4);
1322}
1323
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001324static int
1325frame_enter_handler(struct widget *widget,
1326 struct input *input, uint32_t time,
1327 int32_t x, int32_t y, void *data)
1328{
1329 return frame_get_pointer_image_for_location(data, input);
1330}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001331
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001332static int
1333frame_motion_handler(struct widget *widget,
1334 struct input *input, uint32_t time,
1335 int32_t x, int32_t y, void *data)
1336{
1337 return frame_get_pointer_image_for_location(data, input);
1338}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001340static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001341frame_button_handler(struct widget *widget,
1342 struct input *input, uint32_t time,
1343 int button, int state, void *data)
1344
1345{
1346 struct frame *frame = data;
1347 struct window *window = widget->window;
1348 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001349 location = frame_get_pointer_location(frame, input->sx, input->sy);
1350
1351 if (window->display->shell && button == BTN_LEFT && state == 1) {
1352 switch (location) {
1353 case WINDOW_TITLEBAR:
1354 if (!window->shell_surface)
1355 break;
1356 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001357 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001358 wl_shell_surface_move(window->shell_surface,
1359 input_get_input_device(input),
1360 time);
1361 break;
1362 case WINDOW_RESIZING_TOP:
1363 case WINDOW_RESIZING_BOTTOM:
1364 case WINDOW_RESIZING_LEFT:
1365 case WINDOW_RESIZING_RIGHT:
1366 case WINDOW_RESIZING_TOP_LEFT:
1367 case WINDOW_RESIZING_TOP_RIGHT:
1368 case WINDOW_RESIZING_BOTTOM_LEFT:
1369 case WINDOW_RESIZING_BOTTOM_RIGHT:
1370 if (!window->shell_surface)
1371 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001372 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373 wl_shell_surface_resize(window->shell_surface,
1374 input_get_input_device(input),
1375 time, location);
1376 break;
1377 }
1378 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001379 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001380 }
1381}
1382
1383struct widget *
1384frame_create(struct window *window, void *data)
1385{
1386 struct frame *frame;
1387
1388 frame = malloc(sizeof *frame);
1389 memset(frame, 0, sizeof *frame);
1390
1391 frame->widget = window_add_widget(window, frame);
1392 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001393 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001394 frame->width = 4;
1395 frame->titlebar_height = 30
1396;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001397 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1398 widget_set_resize_handler(frame->widget, frame_resize_handler);
1399 widget_set_enter_handler(frame->widget, frame_enter_handler);
1400 widget_set_motion_handler(frame->widget, frame_motion_handler);
1401 widget_set_button_handler(frame->widget, frame_button_handler);
1402
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001403 window->frame = frame;
1404
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001405 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001406}
1407
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001408static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001409frame_destroy(struct frame *frame)
1410{
1411 /* frame->child must be destroyed by the application */
1412 widget_destroy(frame->widget);
1413 free(frame);
1414}
1415
1416static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001417input_set_focus_widget(struct input *input, struct widget *focus,
1418 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001419{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001420 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001421 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001422
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001423 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424 return;
1425
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001426 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001427 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001428 widget = old;
1429 if (input->grab)
1430 widget = input->grab;
1431 if (widget->leave_handler)
1432 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001433 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001434 }
1435
1436 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001437 widget = focus;
1438 if (input->grab)
1439 widget = input->grab;
1440 if (widget->enter_handler)
1441 pointer = widget->enter_handler(focus, input, time,
1442 x, y,
1443 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001444 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001445
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001446 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001447 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448}
1449
1450static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001451input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001452 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001453{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001454 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001455 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001456 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001457 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001458
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001459 input->sx = sx;
1460 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001461
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001462 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001463 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001464 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001465 }
1466
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001467 if (input->grab)
1468 widget = input->grab;
1469 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001470 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001471 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001473 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001474 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001475
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001476 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001477}
1478
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001479void
1480input_grab(struct input *input, struct widget *widget, uint32_t button)
1481{
1482 input->grab = widget;
1483 input->grab_button = button;
1484}
1485
1486void
1487input_ungrab(struct input *input, uint32_t time)
1488{
1489 struct widget *widget;
1490
1491 input->grab = NULL;
1492 if (input->pointer_focus) {
1493 widget = widget_find_widget(input->pointer_focus->widget,
1494 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001495 input_set_focus_widget(input, widget,
1496 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001497 }
1498}
1499
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001500static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001501input_handle_button(void *data,
1502 struct wl_input_device *input_device,
1503 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001504{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001505 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001506 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001507
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001508 if (input->focus_widget && input->grab == NULL && state)
1509 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001510
Neil Roberts6b28aad2012-01-23 19:11:18 +00001511 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001512 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001513 (*widget->button_handler)(widget,
1514 input, time,
1515 button, state,
1516 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001517
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001518 if (input->grab && input->grab_button == button && !state)
1519 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001520}
1521
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001522static void
Scott Moreau210d0792012-03-22 10:47:01 -06001523input_handle_axis(void *data,
1524 struct wl_input_device *input_device,
1525 uint32_t time, uint32_t axis, int32_t value)
1526{
1527}
1528
1529static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001530input_handle_key(void *data, struct wl_input_device *input_device,
1531 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001532{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001533 struct input *input = data;
1534 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001535 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001536 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001537
Daniel Stone0d5a5092012-02-16 12:48:00 +00001538 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001539 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001540 return;
1541
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001542 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001543 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001544 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1545 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001546
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001547 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1548
1549 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001550 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001551 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001552 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001553
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001554 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1555 if (state)
1556 window_set_maximized(window,
1557 window->type != TYPE_MAXIMIZED);
1558 } else if (window->key_handler) {
1559 (*window->key_handler)(window, input, time, key,
1560 sym, state, window->user_data);
1561 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001562}
1563
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001564static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001565input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001566{
1567 struct window *window = input->pointer_focus;
1568
1569 if (!window)
1570 return;
1571
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001572 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001573
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574 input->pointer_focus = NULL;
1575 input->current_pointer_image = POINTER_UNSET;
1576}
1577
1578static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001579input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001580 struct wl_input_device *input_device,
1581 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001582 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001583{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001584 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001585 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001586 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001587
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001588 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001589 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001590
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001591 input->sx = sx;
1592 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001594 widget = widget_find_widget(window->widget, sx, sy);
1595 input_set_focus_widget(input, widget, time, sx, sy);
1596}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001597
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001598static void
1599input_handle_pointer_leave(void *data,
1600 struct wl_input_device *input_device,
1601 uint32_t time, struct wl_surface *surface)
1602{
1603 struct input *input = data;
1604
1605 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001606}
1607
1608static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001609input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001610{
1611 struct window *window = input->keyboard_focus;
1612
1613 if (!window)
1614 return;
1615
1616 window->keyboard_device = NULL;
1617 if (window->keyboard_focus_handler)
1618 (*window->keyboard_focus_handler)(window, NULL,
1619 window->user_data);
1620
1621 input->keyboard_focus = NULL;
1622}
1623
1624static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001625input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001626 struct wl_input_device *input_device,
1627 uint32_t time,
1628 struct wl_surface *surface,
1629 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001630{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001631 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001632 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001633 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001634 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001635
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001636 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001637
1638 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001639 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001640 for (k = keys->data; k < end; k++)
1641 input->modifiers |= d->xkb->map->modmap[*k];
1642
1643 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001644 window->keyboard_device = input;
1645 if (window->keyboard_focus_handler)
1646 (*window->keyboard_focus_handler)(window,
1647 window->keyboard_device,
1648 window->user_data);
1649}
1650
1651static void
1652input_handle_keyboard_leave(void *data,
1653 struct wl_input_device *input_device,
1654 uint32_t time,
1655 struct wl_surface *surface)
1656{
1657 struct input *input = data;
1658
1659 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001660}
1661
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001662static void
1663input_handle_touch_down(void *data,
1664 struct wl_input_device *wl_input_device,
1665 uint32_t time, struct wl_surface *surface,
1666 int32_t id, int32_t x, int32_t y)
1667{
1668}
1669
1670static void
1671input_handle_touch_up(void *data,
1672 struct wl_input_device *wl_input_device,
1673 uint32_t time, int32_t id)
1674{
1675}
1676
1677static void
1678input_handle_touch_motion(void *data,
1679 struct wl_input_device *wl_input_device,
1680 uint32_t time, int32_t id, int32_t x, int32_t y)
1681{
1682}
1683
1684static void
1685input_handle_touch_frame(void *data,
1686 struct wl_input_device *wl_input_device)
1687{
1688}
1689
1690static void
1691input_handle_touch_cancel(void *data,
1692 struct wl_input_device *wl_input_device)
1693{
1694}
1695
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001696static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001697 input_handle_motion,
1698 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001699 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001700 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001701 input_handle_pointer_enter,
1702 input_handle_pointer_leave,
1703 input_handle_keyboard_enter,
1704 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001705 input_handle_touch_down,
1706 input_handle_touch_up,
1707 input_handle_touch_motion,
1708 input_handle_touch_frame,
1709 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001710};
1711
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001712void
1713input_get_position(struct input *input, int32_t *x, int32_t *y)
1714{
1715 *x = input->sx;
1716 *y = input->sy;
1717}
1718
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001719struct wl_input_device *
1720input_get_input_device(struct input *input)
1721{
1722 return input->input_device;
1723}
1724
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001725uint32_t
1726input_get_modifiers(struct input *input)
1727{
1728 return input->modifiers;
1729}
1730
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001731struct widget *
1732input_get_focus_widget(struct input *input)
1733{
1734 return input->focus_widget;
1735}
1736
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001737struct data_offer {
1738 struct wl_data_offer *offer;
1739 struct input *input;
1740 struct wl_array types;
1741 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001742
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001743 struct task io_task;
1744 int fd;
1745 data_func_t func;
1746 int32_t x, y;
1747 void *user_data;
1748};
1749
1750static void
1751data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1752{
1753 struct data_offer *offer = data;
1754 char **p;
1755
1756 p = wl_array_add(&offer->types, sizeof *p);
1757 *p = strdup(type);
1758}
1759
1760static const struct wl_data_offer_listener data_offer_listener = {
1761 data_offer_offer,
1762};
1763
1764static void
1765data_offer_destroy(struct data_offer *offer)
1766{
1767 char **p;
1768
1769 offer->refcount--;
1770 if (offer->refcount == 0) {
1771 wl_data_offer_destroy(offer->offer);
1772 for (p = offer->types.data; *p; p++)
1773 free(*p);
1774 wl_array_release(&offer->types);
1775 free(offer);
1776 }
1777}
1778
1779static void
1780data_device_data_offer(void *data,
1781 struct wl_data_device *data_device, uint32_t id)
1782{
1783 struct data_offer *offer;
1784
1785 offer = malloc(sizeof *offer);
1786
1787 wl_array_init(&offer->types);
1788 offer->refcount = 1;
1789 offer->input = data;
1790
1791 /* FIXME: Generate typesafe wrappers for this */
1792 offer->offer = (struct wl_data_offer *)
1793 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1794 id, &wl_data_offer_interface);
1795
1796 wl_data_offer_add_listener(offer->offer,
1797 &data_offer_listener, offer);
1798}
1799
1800static void
1801data_device_enter(void *data, struct wl_data_device *data_device,
1802 uint32_t time, struct wl_surface *surface,
1803 int32_t x, int32_t y, struct wl_data_offer *offer)
1804{
1805 struct input *input = data;
1806 struct window *window;
1807 char **p;
1808
1809 input->drag_offer = wl_data_offer_get_user_data(offer);
1810 window = wl_surface_get_user_data(surface);
1811 input->pointer_focus = window;
1812
1813 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1814 *p = NULL;
1815
1816 window = input->pointer_focus;
1817 if (window->data_handler)
1818 window->data_handler(window, input, time, x, y,
1819 input->drag_offer->types.data,
1820 window->user_data);
1821}
1822
1823static void
1824data_device_leave(void *data, struct wl_data_device *data_device)
1825{
1826 struct input *input = data;
1827
1828 data_offer_destroy(input->drag_offer);
1829 input->drag_offer = NULL;
1830}
1831
1832static void
1833data_device_motion(void *data, struct wl_data_device *data_device,
1834 uint32_t time, int32_t x, int32_t y)
1835{
1836 struct input *input = data;
1837 struct window *window = input->pointer_focus;
1838
1839 input->sx = x;
1840 input->sy = y;
1841
1842 if (window->data_handler)
1843 window->data_handler(window, input, time, x, y,
1844 input->drag_offer->types.data,
1845 window->user_data);
1846}
1847
1848static void
1849data_device_drop(void *data, struct wl_data_device *data_device)
1850{
1851 struct input *input = data;
1852 struct window *window = input->pointer_focus;
1853
1854 if (window->drop_handler)
1855 window->drop_handler(window, input,
1856 input->sx, input->sy, window->user_data);
1857}
1858
1859static void
1860data_device_selection(void *data,
1861 struct wl_data_device *wl_data_device,
1862 struct wl_data_offer *offer)
1863{
1864 struct input *input = data;
1865 char **p;
1866
1867 if (input->selection_offer)
1868 data_offer_destroy(input->selection_offer);
1869
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001870 if (offer) {
1871 input->selection_offer = wl_data_offer_get_user_data(offer);
1872 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1873 *p = NULL;
1874 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001875}
1876
1877static const struct wl_data_device_listener data_device_listener = {
1878 data_device_data_offer,
1879 data_device_enter,
1880 data_device_leave,
1881 data_device_motion,
1882 data_device_drop,
1883 data_device_selection
1884};
1885
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001886void
1887input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1888{
1889 struct display *display = input->display;
1890 struct wl_buffer *buffer;
1891 cairo_surface_t *surface;
1892
1893 if (pointer == input->current_pointer_image)
1894 return;
1895
1896 input->current_pointer_image = pointer;
1897 surface = display->pointer_surfaces[pointer];
1898
1899 if (!surface)
1900 return;
1901
1902 buffer = display_get_buffer_for_surface(display, surface);
1903 wl_input_device_attach(input->input_device, time, buffer,
1904 pointer_images[pointer].hotspot_x,
1905 pointer_images[pointer].hotspot_y);
1906}
1907
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001908struct wl_data_device *
1909input_get_data_device(struct input *input)
1910{
1911 return input->data_device;
1912}
1913
1914void
1915input_set_selection(struct input *input,
1916 struct wl_data_source *source, uint32_t time)
1917{
1918 wl_data_device_set_selection(input->data_device, source, time);
1919}
1920
1921void
1922input_accept(struct input *input, uint32_t time, const char *type)
1923{
1924 wl_data_offer_accept(input->drag_offer->offer, time, type);
1925}
1926
1927static void
1928offer_io_func(struct task *task, uint32_t events)
1929{
1930 struct data_offer *offer =
1931 container_of(task, struct data_offer, io_task);
1932 unsigned int len;
1933 char buffer[4096];
1934
1935 len = read(offer->fd, buffer, sizeof buffer);
1936 offer->func(buffer, len,
1937 offer->x, offer->y, offer->user_data);
1938
1939 if (len == 0) {
1940 close(offer->fd);
1941 data_offer_destroy(offer);
1942 }
1943}
1944
1945static void
1946data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1947 data_func_t func, void *user_data)
1948{
1949 int p[2];
1950
1951 pipe2(p, O_CLOEXEC);
1952 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1953 close(p[1]);
1954
1955 offer->io_task.run = offer_io_func;
1956 offer->fd = p[0];
1957 offer->func = func;
1958 offer->refcount++;
1959 offer->user_data = user_data;
1960
1961 display_watch_fd(offer->input->display,
1962 offer->fd, EPOLLIN, &offer->io_task);
1963}
1964
1965void
1966input_receive_drag_data(struct input *input, const char *mime_type,
1967 data_func_t func, void *data)
1968{
1969 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1970 input->drag_offer->x = input->sx;
1971 input->drag_offer->y = input->sy;
1972}
1973
1974int
1975input_receive_selection_data(struct input *input, const char *mime_type,
1976 data_func_t func, void *data)
1977{
1978 char **p;
1979
1980 if (input->selection_offer == NULL)
1981 return -1;
1982
1983 for (p = input->selection_offer->types.data; *p; p++)
1984 if (strcmp(mime_type, *p) == 0)
1985 break;
1986
1987 if (*p == NULL)
1988 return -1;
1989
1990 data_offer_receive_data(input->selection_offer,
1991 mime_type, func, data);
1992 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001993}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001994
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001995int
1996input_receive_selection_data_to_fd(struct input *input,
1997 const char *mime_type, int fd)
1998{
1999 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2000
2001 return 0;
2002}
2003
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002004void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002005window_move(struct window *window, struct input *input, uint32_t time)
2006{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002007 if (!window->shell_surface)
2008 return;
2009
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002010 wl_shell_surface_move(window->shell_surface,
2011 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002012}
2013
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002014static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002015idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002016{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002017 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002018
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002019 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002020 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002021 widget_set_allocation(widget,
2022 window->pending_allocation.x,
2023 window->pending_allocation.y,
2024 window->pending_allocation.width,
2025 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002026
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002027 if (window->input_region) {
2028 wl_region_destroy(window->input_region);
2029 window->input_region = NULL;
2030 }
2031
2032 if (window->opaque_region) {
2033 wl_region_destroy(window->opaque_region);
2034 window->opaque_region = NULL;
2035 }
2036
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002037 if (widget->resize_handler)
2038 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002039 widget->allocation.width,
2040 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002041 widget->user_data);
2042
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002043 if (window->allocation.width != widget->allocation.width ||
2044 window->allocation.height != widget->allocation.height) {
2045 window->allocation = widget->allocation;
2046 window_schedule_redraw(window);
2047 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002048}
2049
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002050void
2051window_schedule_resize(struct window *window, int width, int height)
2052{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002053 window->pending_allocation.x = 0;
2054 window->pending_allocation.y = 0;
2055 window->pending_allocation.width = width;
2056 window->pending_allocation.height = height;
2057
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002058 window->resize_needed = 1;
2059 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002060}
2061
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002062void
2063widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2064{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002065 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002066}
2067
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002068static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002070 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002072{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002074
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002075 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002076 return;
2077
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002078 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002079 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002080}
2081
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002083menu_destroy(struct menu *menu)
2084{
2085 widget_destroy(menu->widget);
2086 window_destroy(menu->window);
2087 free(menu);
2088}
2089
2090static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002091handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2092{
2093 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 struct menu *menu = window->widget->user_data;
2095
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002096 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002097 * device. Or just use wl_callback. And this really needs to
2098 * be a window vfunc that the menu can set. And we need the
2099 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002100
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002101 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002102 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002103 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002104}
2105
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002107 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002108 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002109};
2110
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002111void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002112window_get_allocation(struct window *window,
2113 struct rectangle *allocation)
2114{
2115 *allocation = window->allocation;
2116}
2117
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002118static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002119widget_redraw(struct widget *widget)
2120{
2121 struct widget *child;
2122
2123 if (widget->redraw_handler)
2124 widget->redraw_handler(widget, widget->user_data);
2125 wl_list_for_each(child, &widget->child_list, link)
2126 widget_redraw(child);
2127}
2128
2129static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002130frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2131{
2132 struct window *window = data;
2133
2134 wl_callback_destroy(callback);
2135 window->redraw_scheduled = 0;
2136 if (window->redraw_needed)
2137 window_schedule_redraw(window);
2138}
2139
2140static const struct wl_callback_listener listener = {
2141 frame_callback
2142};
2143
2144static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002145idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002146{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002147 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002148 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002149
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002150 if (window->resize_needed)
2151 idle_resize(window);
2152
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002153 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002154 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002155 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002156 window->redraw_needed = 0;
2157
2158 callback = wl_surface_frame(window->surface);
2159 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002160}
2161
2162void
2163window_schedule_redraw(struct window *window)
2164{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002165 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002166 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002167 window->redraw_task.run = idle_redraw;
2168 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002169 window->redraw_scheduled = 1;
2170 }
2171}
2172
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002173void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002174window_set_custom(struct window *window)
2175{
2176 window->type = TYPE_CUSTOM;
2177}
2178
2179void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002180window_set_fullscreen(struct window *window, int fullscreen)
2181{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002182 if (!window->display->shell)
2183 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002184
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002185 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002186 return;
2187
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002188 if (fullscreen) {
2189 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002190 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002191 wl_shell_surface_set_fullscreen(window->shell_surface,
2192 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2193 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002194 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002195 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002196 wl_shell_surface_set_toplevel(window->shell_surface);
2197 window_schedule_resize(window,
2198 window->saved_allocation.width,
2199 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002200 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002201}
2202
2203void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002204window_set_maximized(struct window *window, int maximized)
2205{
2206 if (!window->display->shell)
2207 return;
2208
2209 if ((window->type == TYPE_MAXIMIZED) == maximized)
2210 return;
2211
2212 if (window->type == TYPE_TOPLEVEL) {
2213 window->saved_allocation = window->allocation;
2214 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2215 window->type = TYPE_MAXIMIZED;
2216 } else {
2217 wl_shell_surface_set_toplevel(window->shell_surface);
2218 window->type = TYPE_TOPLEVEL;
2219 window_schedule_resize(window,
2220 window->saved_allocation.width,
2221 window->saved_allocation.height);
2222 }
2223}
2224
2225void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002226window_set_user_data(struct window *window, void *data)
2227{
2228 window->user_data = data;
2229}
2230
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002231void *
2232window_get_user_data(struct window *window)
2233{
2234 return window->user_data;
2235}
2236
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002237void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002238window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002239 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002240{
2241 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002242}
2243
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002244void
2245window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002246 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002247{
2248 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002249}
2250
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002251void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002252window_set_data_handler(struct window *window, window_data_handler_t handler)
2253{
2254 window->data_handler = handler;
2255}
2256
2257void
2258window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2259{
2260 window->drop_handler = handler;
2261}
2262
2263void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002264window_set_close_handler(struct window *window,
2265 window_close_handler_t handler)
2266{
2267 window->close_handler = handler;
2268}
2269
2270void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002271window_set_title(struct window *window, const char *title)
2272{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002273 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002274 window->title = strdup(title);
2275}
2276
2277const char *
2278window_get_title(struct window *window)
2279{
2280 return window->title;
2281}
2282
2283void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002284window_damage(struct window *window, int32_t x, int32_t y,
2285 int32_t width, int32_t height)
2286{
2287 wl_surface_damage(window->surface, x, y, width, height);
2288}
2289
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002290static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002291window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002292{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002293 struct window *window;
2294
2295 window = malloc(sizeof *window);
2296 if (window == NULL)
2297 return NULL;
2298
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002299 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002300 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002301 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002302 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002303 if (display->shell) {
2304 window->shell_surface =
2305 wl_shell_get_shell_surface(display->shell,
2306 window->surface);
2307 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002308 window->allocation.x = 0;
2309 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002310 window->allocation.width = 0;
2311 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002312 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002313 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002314 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002315 window->input_region = NULL;
2316 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002317
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002318 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002319#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002320 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2321 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002322#else
2323 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2324#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002325 else
2326 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002327
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002328 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002329 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002330
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002331 if (window->shell_surface) {
2332 wl_shell_surface_set_user_data(window->shell_surface, window);
2333 wl_shell_surface_add_listener(window->shell_surface,
2334 &shell_surface_listener, window);
2335 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002336
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002337 return window;
2338}
2339
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002340struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002341window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002342{
2343 struct window *window;
2344
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002345 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002346 if (!window)
2347 return NULL;
2348
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002349 window->type = TYPE_TOPLEVEL;
2350 if (display->shell)
2351 wl_shell_surface_set_toplevel(window->shell_surface);
2352
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002353 return window;
2354}
2355
2356struct window *
2357window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002358 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002359{
2360 struct window *window;
2361
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002362 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002363 if (!window)
2364 return NULL;
2365
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002366 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002367 window->x = x;
2368 window->y = y;
2369
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002370 if (display->shell)
2371 wl_shell_surface_set_transient(window->shell_surface,
2372 window->parent->shell_surface,
2373 window->x, window->y, 0);
2374
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002375 return window;
2376}
2377
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002378static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002379menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002380{
2381 int next;
2382
2383 next = (sy - 8) / 20;
2384 if (menu->current != next) {
2385 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002386 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002388}
2389
2390static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002391menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002392 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002393 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002394{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002395 struct menu *menu = data;
2396
2397 if (widget == menu->widget)
2398 menu_set_item(data, y);
2399
2400 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002401}
2402
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002403static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002404menu_enter_handler(struct widget *widget,
2405 struct input *input, uint32_t time,
2406 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002408 struct menu *menu = data;
2409
2410 if (widget == menu->widget)
2411 menu_set_item(data, y);
2412
2413 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002414}
2415
2416static void
2417menu_leave_handler(struct widget *widget, struct input *input, void *data)
2418{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002419 struct menu *menu = data;
2420
2421 if (widget == menu->widget)
2422 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423}
2424
2425static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002426menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002427 struct input *input, uint32_t time,
2428 int button, int state, void *data)
2429
2430{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002431 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002432
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002433 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002434 /* Either relase after press-drag-release or
2435 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002436 menu->func(menu->window->parent,
2437 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002438 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002439 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002440 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002441}
2442
2443static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002444menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002445{
2446 cairo_t *cr;
2447 const int32_t r = 3, margin = 3;
2448 struct menu *menu = data;
2449 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002450 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002451
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002452 cr = cairo_create(window->cairo_surface);
2453 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2454 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2455 cairo_paint(cr);
2456
2457 width = window->allocation.width;
2458 height = window->allocation.height;
2459 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002460
2461 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2463 cairo_fill(cr);
2464
2465 for (i = 0; i < menu->count; i++) {
2466 if (i == menu->current) {
2467 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2468 cairo_rectangle(cr, margin, i * 20 + margin,
2469 width - 2 * margin, 20);
2470 cairo_fill(cr);
2471 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2472 cairo_move_to(cr, 10, i * 20 + 16);
2473 cairo_show_text(cr, menu->entries[i]);
2474 } else {
2475 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2476 cairo_move_to(cr, 10, i * 20 + 16);
2477 cairo_show_text(cr, menu->entries[i]);
2478 }
2479 }
2480
2481 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002482}
2483
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002484void
2485window_show_menu(struct display *display,
2486 struct input *input, uint32_t time, struct window *parent,
2487 int32_t x, int32_t y,
2488 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002489{
2490 struct window *window;
2491 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002492 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002493
2494 menu = malloc(sizeof *menu);
2495 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002496 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002497
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002498 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002499 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002500 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002501
2502 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002503 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002504 menu->entries = entries;
2505 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002506 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002507 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002508 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002509 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002510 window->type = TYPE_MENU;
2511 window->x = x;
2512 window->y = y;
2513
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002514 wl_shell_surface_set_popup(window->shell_surface,
2515 input->input_device, time,
2516 window->parent->shell_surface,
2517 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002518
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002519 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002520 widget_set_enter_handler(menu->widget, menu_enter_handler);
2521 widget_set_leave_handler(menu->widget, menu_leave_handler);
2522 widget_set_motion_handler(menu->widget, menu_motion_handler);
2523 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002524
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002526 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002527}
2528
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002529void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002530window_set_buffer_type(struct window *window, enum window_buffer_type type)
2531{
2532 window->buffer_type = type;
2533}
2534
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002535
2536static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002537display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002538 struct wl_output *wl_output,
2539 int x, int y,
2540 int physical_width,
2541 int physical_height,
2542 int subpixel,
2543 const char *make,
2544 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002545{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002546 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002547
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002548 output->allocation.x = x;
2549 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002550}
2551
2552static void
2553display_handle_mode(void *data,
2554 struct wl_output *wl_output,
2555 uint32_t flags,
2556 int width,
2557 int height,
2558 int refresh)
2559{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002560 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002561 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002562
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002563 if (flags & WL_OUTPUT_MODE_CURRENT) {
2564 output->allocation.width = width;
2565 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002566 if (display->output_configure_handler)
2567 (*display->output_configure_handler)(
2568 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002569 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002570}
2571
2572static const struct wl_output_listener output_listener = {
2573 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002574 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002575};
2576
2577static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002578display_add_output(struct display *d, uint32_t id)
2579{
2580 struct output *output;
2581
2582 output = malloc(sizeof *output);
2583 if (output == NULL)
2584 return;
2585
2586 memset(output, 0, sizeof *output);
2587 output->display = d;
2588 output->output =
2589 wl_display_bind(d->display, id, &wl_output_interface);
2590 wl_list_insert(d->output_list.prev, &output->link);
2591
2592 wl_output_add_listener(output->output, &output_listener, output);
2593}
2594
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002595static void
2596output_destroy(struct output *output)
2597{
2598 if (output->destroy_handler)
2599 (*output->destroy_handler)(output, output->user_data);
2600
2601 wl_output_destroy(output->output);
2602 wl_list_remove(&output->link);
2603 free(output);
2604}
2605
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002606void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002607display_set_output_configure_handler(struct display *display,
2608 display_output_handler_t handler)
2609{
2610 struct output *output;
2611
2612 display->output_configure_handler = handler;
2613 if (!handler)
2614 return;
2615
2616 wl_list_for_each(output, &display->output_list, link)
2617 (*display->output_configure_handler)(output,
2618 display->user_data);
2619}
2620
2621void
2622output_set_user_data(struct output *output, void *data)
2623{
2624 output->user_data = data;
2625}
2626
2627void *
2628output_get_user_data(struct output *output)
2629{
2630 return output->user_data;
2631}
2632
2633void
2634output_set_destroy_handler(struct output *output,
2635 display_output_handler_t handler)
2636{
2637 output->destroy_handler = handler;
2638 /* FIXME: implement this, once we have way to remove outputs */
2639}
2640
2641void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002642output_get_allocation(struct output *output, struct rectangle *allocation)
2643{
2644 *allocation = output->allocation;
2645}
2646
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002647struct wl_output *
2648output_get_wl_output(struct output *output)
2649{
2650 return output->output;
2651}
2652
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002653static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002654display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002655{
2656 struct input *input;
2657
2658 input = malloc(sizeof *input);
2659 if (input == NULL)
2660 return;
2661
2662 memset(input, 0, sizeof *input);
2663 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002664 input->input_device =
2665 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002666 input->pointer_focus = NULL;
2667 input->keyboard_focus = NULL;
2668 wl_list_insert(d->input_list.prev, &input->link);
2669
2670 wl_input_device_add_listener(input->input_device,
2671 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002672 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002673
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002674 input->data_device =
2675 wl_data_device_manager_get_data_device(d->data_device_manager,
2676 input->input_device);
2677 wl_data_device_add_listener(input->data_device,
2678 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002679}
2680
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002681static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002682input_destroy(struct input *input)
2683{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002684 input_remove_keyboard_focus(input);
2685 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002686
2687 if (input->drag_offer)
2688 data_offer_destroy(input->drag_offer);
2689
2690 if (input->selection_offer)
2691 data_offer_destroy(input->selection_offer);
2692
2693 wl_data_device_destroy(input->data_device);
2694 wl_list_remove(&input->link);
2695 wl_input_device_destroy(input->input_device);
2696 free(input);
2697}
2698
2699static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002700display_handle_global(struct wl_display *display, uint32_t id,
2701 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002702{
2703 struct display *d = data;
2704
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002705 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002706 d->compositor =
2707 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002708 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002709 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002710 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002711 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002712 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002713 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002714 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002715 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002716 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2717 d->data_device_manager =
2718 wl_display_bind(display, id,
2719 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002720 }
2721}
2722
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002723static void
2724display_render_frame(struct display *d)
2725{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002726 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002727 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002728
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002729 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002730 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2731 cr = cairo_create(d->shadow);
2732 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2733 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002734 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002735 cairo_fill(cr);
2736 cairo_destroy(cr);
2737 blur_surface(d->shadow, 64);
2738
2739 d->active_frame =
2740 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2741 cr = cairo_create(d->active_frame);
2742 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002743
2744 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2745 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2746 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2747 cairo_set_source(cr, pattern);
2748 cairo_pattern_destroy(pattern);
2749
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002750 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002751 cairo_fill(cr);
2752 cairo_destroy(cr);
2753
2754 d->inactive_frame =
2755 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2756 cr = cairo_create(d->inactive_frame);
2757 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002758 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002759 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002760 cairo_fill(cr);
2761 cairo_destroy(cr);
2762}
2763
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002764static void
2765init_xkb(struct display *d)
2766{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002767 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002768
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002769 names.rules = "evdev";
2770 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002771 names.layout = option_xkb_layout;
2772 names.variant = option_xkb_variant;
2773 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002774
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002775 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002776 if (!d->xkb) {
2777 fprintf(stderr, "Failed to compile keymap\n");
2778 exit(1);
2779 }
2780}
2781
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002782static void
2783fini_xkb(struct display *display)
2784{
2785 xkb_free_keymap(display->xkb);
2786}
2787
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002788#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002789static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002790init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002791{
2792 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002793 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002794
Rob Clark6396ed32012-03-11 19:48:41 -05002795#ifdef USE_CAIRO_GLESV2
2796# define GL_BIT EGL_OPENGL_ES2_BIT
2797#else
2798# define GL_BIT EGL_OPENGL_BIT
2799#endif
2800
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002801 static const EGLint argb_cfg_attribs[] = {
2802 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002803 EGL_RED_SIZE, 1,
2804 EGL_GREEN_SIZE, 1,
2805 EGL_BLUE_SIZE, 1,
2806 EGL_ALPHA_SIZE, 1,
2807 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002808 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002809 EGL_NONE
2810 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002811
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002812#ifdef USE_CAIRO_GLESV2
2813 static const EGLint context_attribs[] = {
2814 EGL_CONTEXT_CLIENT_VERSION, 2,
2815 EGL_NONE
2816 };
2817 EGLint api = EGL_OPENGL_ES_API;
2818#else
2819 EGLint *context_attribs = NULL;
2820 EGLint api = EGL_OPENGL_API;
2821#endif
2822
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002823 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002824 if (!eglInitialize(d->dpy, &major, &minor)) {
2825 fprintf(stderr, "failed to initialize display\n");
2826 return -1;
2827 }
2828
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002829 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002830 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2831 return -1;
2832 }
2833
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002834 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2835 &d->argb_config, 1, &n) || n != 1) {
2836 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002837 return -1;
2838 }
2839
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002840 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002841 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002842 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002843 fprintf(stderr, "failed to create context\n");
2844 return -1;
2845 }
2846
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002847 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002848 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002849 return -1;
2850 }
2851
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002852#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002853 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2854 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2855 fprintf(stderr, "failed to get cairo egl argb device\n");
2856 return -1;
2857 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002858#endif
2859
2860 return 0;
2861}
2862
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002863static void
2864fini_egl(struct display *display)
2865{
2866#ifdef HAVE_CAIRO_EGL
2867 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002868#endif
2869
2870 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2871 EGL_NO_CONTEXT);
2872
2873 eglTerminate(display->dpy);
2874 eglReleaseThread();
2875}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002876#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002877
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002878static int
2879event_mask_update(uint32_t mask, void *data)
2880{
2881 struct display *d = data;
2882
2883 d->mask = mask;
2884
2885 return 0;
2886}
2887
2888static void
2889handle_display_data(struct task *task, uint32_t events)
2890{
2891 struct display *display =
2892 container_of(task, struct display, display_task);
2893
2894 wl_display_iterate(display->display, display->mask);
2895}
2896
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002897struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002898display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002899{
2900 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002901
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002902 argc = parse_options(xkb_options,
2903 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002904
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002905 d = malloc(sizeof *d);
2906 if (d == NULL)
2907 return NULL;
2908
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002909 memset(d, 0, sizeof *d);
2910
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002911 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002912 if (d->display == NULL) {
2913 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002914 return NULL;
2915 }
2916
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002917 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2918 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2919 d->display_task.run = handle_display_data;
2920 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2921
2922 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002923 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002924 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002925
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002926 /* Set up listener so we'll catch all events. */
2927 wl_display_add_global_listener(d->display,
2928 display_handle_global, d);
2929
2930 /* Process connection events. */
2931 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002932#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002933 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002934 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002935#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002936
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002937 d->image_target_texture_2d =
2938 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2939 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2940 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2941
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002942 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002943
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002944 display_render_frame(d);
2945
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002946 wl_list_init(&d->window_list);
2947
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002948 init_xkb(d);
2949
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002950 return d;
2951}
2952
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002953static void
2954display_destroy_outputs(struct display *display)
2955{
2956 struct output *tmp;
2957 struct output *output;
2958
2959 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2960 output_destroy(output);
2961}
2962
Pekka Paalanene1207c72011-12-16 12:02:09 +02002963static void
2964display_destroy_inputs(struct display *display)
2965{
2966 struct input *tmp;
2967 struct input *input;
2968
2969 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2970 input_destroy(input);
2971}
2972
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002973void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002974display_destroy(struct display *display)
2975{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002976 if (!wl_list_empty(&display->window_list))
2977 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2978
2979 if (!wl_list_empty(&display->deferred_list))
2980 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2981
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002982 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002983 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002984
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002985 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002986
2987 cairo_surface_destroy(display->active_frame);
2988 cairo_surface_destroy(display->inactive_frame);
2989 cairo_surface_destroy(display->shadow);
2990 destroy_pointer_surfaces(display);
2991
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002992#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002993 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002994#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002995
Pekka Paalanenc2052982011-12-16 11:41:32 +02002996 if (display->shell)
2997 wl_shell_destroy(display->shell);
2998
2999 if (display->shm)
3000 wl_shm_destroy(display->shm);
3001
3002 if (display->data_device_manager)
3003 wl_data_device_manager_destroy(display->data_device_manager);
3004
3005 wl_compositor_destroy(display->compositor);
3006
3007 close(display->epoll_fd);
3008
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003009 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003010 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003011 free(display);
3012}
3013
3014void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003015display_set_user_data(struct display *display, void *data)
3016{
3017 display->user_data = data;
3018}
3019
3020void *
3021display_get_user_data(struct display *display)
3022{
3023 return display->user_data;
3024}
3025
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003026struct wl_display *
3027display_get_display(struct display *display)
3028{
3029 return display->display;
3030}
3031
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003032struct output *
3033display_get_output(struct display *display)
3034{
3035 return container_of(display->output_list.next, struct output, link);
3036}
3037
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003038struct wl_compositor *
3039display_get_compositor(struct display *display)
3040{
3041 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003042}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003043
3044EGLDisplay
3045display_get_egl_display(struct display *d)
3046{
3047 return d->dpy;
3048}
3049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003050struct wl_data_source *
3051display_create_data_source(struct display *display)
3052{
3053 return wl_data_device_manager_create_data_source(display->data_device_manager);
3054}
3055
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003056EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003057display_get_argb_egl_config(struct display *d)
3058{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003059 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003060}
3061
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003062struct wl_shell *
3063display_get_shell(struct display *display)
3064{
3065 return display->shell;
3066}
3067
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003068int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003069display_acquire_window_surface(struct display *display,
3070 struct window *window,
3071 EGLContext ctx)
3072{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003073#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003074 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003075 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076
3077 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003078 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003079 device = cairo_surface_get_device(window->cairo_surface);
3080 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003081 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003082
Benjamin Franzke0c991632011-09-27 21:57:31 +02003083 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003084 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003085 ctx = display->argb_ctx;
3086 else
3087 assert(0);
3088 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089
3090 data = cairo_surface_get_user_data(window->cairo_surface,
3091 &surface_data_key);
3092
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003093 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003094 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003095 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3096 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003097
3098 return 0;
3099#else
3100 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003101#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003102}
3103
3104void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003105display_release_window_surface(struct display *display,
3106 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003107{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003108#ifdef HAVE_CAIRO_EGL
3109 cairo_device_t *device;
3110
3111 device = cairo_surface_get_device(window->cairo_surface);
3112 if (!device)
3113 return;
3114
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003115 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003116 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003117 cairo_device_release(device);
3118#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003119}
3120
3121void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003122display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003123{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003124 wl_list_insert(&display->deferred_list, &task->link);
3125}
3126
3127void
3128display_watch_fd(struct display *display,
3129 int fd, uint32_t events, struct task *task)
3130{
3131 struct epoll_event ep;
3132
3133 ep.events = events;
3134 ep.data.ptr = task;
3135 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3136}
3137
3138void
3139display_run(struct display *display)
3140{
3141 struct task *task;
3142 struct epoll_event ep[16];
3143 int i, count;
3144
Pekka Paalanen826d7952011-12-15 10:14:07 +02003145 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003146 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003147 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003148
3149 while (!wl_list_empty(&display->deferred_list)) {
3150 task = container_of(display->deferred_list.next,
3151 struct task, link);
3152 wl_list_remove(&task->link);
3153 task->run(task, 0);
3154 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003155
3156 if (!display->running)
3157 break;
3158
3159 wl_display_flush(display->display);
3160
3161 count = epoll_wait(display->epoll_fd,
3162 ep, ARRAY_LENGTH(ep), -1);
3163 for (i = 0; i < count; i++) {
3164 task = ep[i].data.ptr;
3165 task->run(task, ep[i].events);
3166 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003167 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003168}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003169
3170void
3171display_exit(struct display *display)
3172{
3173 display->running = 0;
3174}