blob: 09114b5cd656c308e2606c9ae9393556f6086bdd [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øgsberg7d804062010-09-07 21:50:06 -0400169 uint32_t 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øgsberg06bc2642010-12-01 09:50:16 -0500464static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400466 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467{
468 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400469 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400470 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800471 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472 char filename[] = "/tmp/wayland-shm-XXXXXX";
473
474 data = malloc(sizeof *data);
475 if (data == NULL)
476 return NULL;
477
478 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
479 rectangle->width);
480 data->length = stride * rectangle->height;
481 fd = mkstemp(filename);
482 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000483 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400484 return NULL;
485 }
486 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000487 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488 close(fd);
489 return NULL;
490 }
491
492 data->map = mmap(NULL, data->length,
493 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
494 unlink(filename);
495
496 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000497 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498 close(fd);
499 return NULL;
500 }
501
502 surface = cairo_image_surface_create_for_data (data->map,
503 CAIRO_FORMAT_ARGB32,
504 rectangle->width,
505 rectangle->height,
506 stride);
507
508 cairo_surface_set_user_data (surface, &surface_data_key,
509 data, shm_surface_data_destroy);
510
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400511 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500512 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400513 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500514 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400515
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516 data->data.buffer = wl_shm_create_buffer(display->shm,
517 fd,
518 rectangle->width,
519 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400520 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521
522 close(fd);
523
524 return surface;
525}
526
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500527static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528display_create_shm_surface_from_file(struct display *display,
529 const char *filename,
530 struct rectangle *rect)
531{
532 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400533 pixman_image_t *image;
534 void *dest;
535 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400536
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400537 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400539 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000540 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400541 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000542 return NULL;
543 }
544
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400545 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
546 dest = cairo_image_surface_get_data(surface);
547 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400549 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
551 return surface;
552}
553
nobled7b87cb02011-02-01 18:51:47 +0000554static int
555check_size(struct rectangle *rect)
556{
557 if (rect->width && rect->height)
558 return 0;
559
560 fprintf(stderr, "tried to create surface of "
561 "width: %d, height: %d\n", rect->width, rect->height);
562 return -1;
563}
564
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565cairo_surface_t *
566display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100567 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400568 struct rectangle *rectangle,
569 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570{
nobled7b87cb02011-02-01 18:51:47 +0000571 if (check_size(rectangle) < 0)
572 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500573#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500574 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100575 if (surface)
576 return display_create_egl_window_surface(display,
577 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400578 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100579 rectangle);
580 else
581 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400582 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100583 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500584 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400586 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587}
588
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500589static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400590display_create_surface_from_file(struct display *display,
591 const char *filename,
592 struct rectangle *rectangle)
593{
nobled7b87cb02011-02-01 18:51:47 +0000594 if (check_size(rectangle) < 0)
595 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500596#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500597 if (display->dpy) {
598 return display_create_egl_image_surface_from_file(display,
599 filename,
600 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500601 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400602#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500603 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400604}
Yuval Fledel45568f62010-12-06 09:18:12 -0500605 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400606 const char *filename;
607 int hotspot_x, hotspot_y;
608} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500609 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
610 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
611 { DATADIR "/weston/bottom_side.png", 16, 20 },
612 { DATADIR "/weston/grabbing.png", 20, 17 },
613 { DATADIR "/weston/left_ptr.png", 10, 5 },
614 { DATADIR "/weston/left_side.png", 10, 20 },
615 { DATADIR "/weston/right_side.png", 30, 19 },
616 { DATADIR "/weston/top_left_corner.png", 8, 8 },
617 { DATADIR "/weston/top_right_corner.png", 26, 8 },
618 { DATADIR "/weston/top_side.png", 18, 8 },
619 { DATADIR "/weston/xterm.png", 15, 15 },
620 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400621};
622
623static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400624create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400625{
626 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400627 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400628 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400629
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400630 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400631 display->pointer_surfaces =
632 malloc(count * sizeof *display->pointer_surfaces);
633 rect.width = width;
634 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400635 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400636 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637 display_create_surface_from_file(display,
638 pointer_images[i].filename,
639 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100640 if (!display->pointer_surfaces[i]) {
641 fprintf(stderr, "Error loading pointer image: %s\n",
642 pointer_images[i].filename);
643 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400644 }
645
646}
647
Pekka Paalanen325bb602011-12-19 10:31:45 +0200648static void
649destroy_pointer_surfaces(struct display *display)
650{
651 int i, count;
652
653 count = ARRAY_LENGTH(pointer_images);
654 for (i = 0; i < count; ++i) {
655 if (display->pointer_surfaces[i])
656 cairo_surface_destroy(display->pointer_surfaces[i]);
657 }
658 free(display->pointer_surfaces);
659}
660
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400661cairo_surface_t *
662display_get_pointer_surface(struct display *display, int pointer,
663 int *width, int *height,
664 int *hotspot_x, int *hotspot_y)
665{
666 cairo_surface_t *surface;
667
668 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500669#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400670 *width = cairo_gl_surface_get_width(surface);
671 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000672#else
673 *width = cairo_image_surface_get_width(surface);
674 *height = cairo_image_surface_get_height(surface);
675#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400676 *hotspot_x = pointer_images[pointer].hotspot_x;
677 *hotspot_y = pointer_images[pointer].hotspot_y;
678
679 return cairo_surface_reference(surface);
680}
681
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400682static void
683window_attach_surface(struct window *window);
684
685static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400686free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400687{
688 struct window *window = data;
689
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400690 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400691 cairo_surface_destroy(window->pending_surface);
692 window->pending_surface = NULL;
693 if (window->cairo_surface)
694 window_attach_surface(window);
695}
696
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400697static const struct wl_callback_listener free_surface_listener = {
698 free_surface
699};
700
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500701static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200702window_get_resize_dx_dy(struct window *window, int *x, int *y)
703{
704 if (window->resize_edges & WINDOW_RESIZING_LEFT)
705 *x = window->server_allocation.width - window->allocation.width;
706 else
707 *x = 0;
708
709 if (window->resize_edges & WINDOW_RESIZING_TOP)
710 *y = window->server_allocation.height -
711 window->allocation.height;
712 else
713 *y = 0;
714
715 window->resize_edges = 0;
716}
717
718static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500719window_attach_surface(struct window *window)
720{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400721 struct display *display = window->display;
722 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400723 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000724#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100725 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000726#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500727 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100728
729 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000730#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100731 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
732 data = cairo_surface_get_user_data(window->cairo_surface,
733 &surface_data_key);
734
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100735 cairo_gl_surface_swapbuffers(window->cairo_surface);
736 wl_egl_window_get_attached_size(data->window,
737 &window->server_allocation.width,
738 &window->server_allocation.height);
739 break;
740 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000741#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100742 case WINDOW_BUFFER_TYPE_SHM:
743 if (window->pending_surface != NULL)
744 return;
745
746 window->pending_surface = window->cairo_surface;
747 window->cairo_surface = NULL;
748
749 buffer =
750 display_get_buffer_for_surface(display,
751 window->pending_surface);
752
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200753 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100754 wl_surface_attach(window->surface, buffer, x, y);
755 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400756 cb = wl_display_sync(display->display);
757 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100758 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000759 default:
760 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100761 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500762
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500763 if (window->input_region) {
764 wl_surface_set_input_region(window->surface,
765 window->input_region);
766 wl_region_destroy(window->input_region);
767 window->input_region = NULL;
768 }
769
770 if (window->opaque_region) {
771 wl_surface_set_opaque_region(window->surface,
772 window->opaque_region);
773 wl_region_destroy(window->opaque_region);
774 window->opaque_region = NULL;
775 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500776
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500777 wl_surface_damage(window->surface, 0, 0,
778 window->allocation.width,
779 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500780}
781
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500782void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400783window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500784{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100785 if (window->cairo_surface) {
786 switch (window->buffer_type) {
787 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
788 case WINDOW_BUFFER_TYPE_SHM:
789 display_surface_damage(window->display,
790 window->cairo_surface,
791 0, 0,
792 window->allocation.width,
793 window->allocation.height);
794 break;
795 default:
796 break;
797 }
798 window_attach_surface(window);
799 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500800}
801
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400802void
803window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500805 cairo_surface_reference(surface);
806
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400807 if (window->cairo_surface != NULL)
808 cairo_surface_destroy(window->cairo_surface);
809
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500810 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400811}
812
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814static void
815window_resize_cairo_window_surface(struct window *window)
816{
817 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200818 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819
820 data = cairo_surface_get_user_data(window->cairo_surface,
821 &surface_data_key);
822
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200823 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 wl_egl_window_resize(data->window,
825 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200826 window->allocation.height,
827 x,y);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_set_size(window->cairo_surface,
830 window->allocation.width,
831 window->allocation.height);
832}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400835struct display *
836window_get_display(struct window *window)
837{
838 return window->display;
839}
840
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400841void
842window_create_surface(struct window *window)
843{
844 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400845 uint32_t flags = 0;
846
847 if (!window->transparent)
848 flags = SURFACE_OPAQUE;
849
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500851#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
853 if (window->cairo_surface) {
854 window_resize_cairo_window_surface(window);
855 return;
856 }
857 surface = display_create_surface(window->display,
858 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400859 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500861 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100863 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400864 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865 break;
866#endif
867 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400868 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400869 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800871 default:
872 surface = NULL;
873 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400875
876 window_set_surface(window, surface);
877 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878}
879
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200880static void frame_destroy(struct frame *frame);
881
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400882void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500883window_destroy(struct window *window)
884{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200885 struct display *display = window->display;
886 struct input *input;
887
888 if (window->redraw_scheduled)
889 wl_list_remove(&window->redraw_task.link);
890
891 wl_list_for_each(input, &display->input_list, link) {
892 if (input->pointer_focus == window)
893 input->pointer_focus = NULL;
894 if (input->keyboard_focus == window)
895 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500896 if (input->focus_widget &&
897 input->focus_widget->window == window)
898 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200899 }
900
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500901 if (window->input_region)
902 wl_region_destroy(window->input_region);
903 if (window->opaque_region)
904 wl_region_destroy(window->opaque_region);
905
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200906 if (window->frame)
907 frame_destroy(window->frame);
908
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200909 if (window->shell_surface)
910 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500911 wl_surface_destroy(window->surface);
912 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200913
914 if (window->cairo_surface != NULL)
915 cairo_surface_destroy(window->cairo_surface);
916 if (window->pending_surface != NULL)
917 cairo_surface_destroy(window->pending_surface);
918
919 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500920 free(window);
921}
922
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500923static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500924widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400925{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500926 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400927
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500928 wl_list_for_each(child, &widget->child_list, link) {
929 target = widget_find_widget(child, x, y);
930 if (target)
931 return target;
932 }
933
934 if (widget->allocation.x <= x &&
935 x < widget->allocation.x + widget->allocation.width &&
936 widget->allocation.y <= y &&
937 y < widget->allocation.y + widget->allocation.height) {
938 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400939 }
940
941 return NULL;
942}
943
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500944static struct widget *
945widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400946{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500947 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400948
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500949 widget = malloc(sizeof *widget);
950 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500951 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500952 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500953 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500954 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500955 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500956
957 return widget;
958}
959
960struct widget *
961window_add_widget(struct window *window, void *data)
962{
963 window->widget = widget_create(window, data);
964 wl_list_init(&window->widget->link);
965
966 return window->widget;
967}
968
969struct widget *
970widget_add_widget(struct widget *parent, void *data)
971{
972 struct widget *widget;
973
974 widget = widget_create(parent->window, data);
975 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400976
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500977 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400978}
979
980void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500981widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400982{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200983 struct display *display = widget->window->display;
984 struct input *input;
985
986 wl_list_for_each(input, &display->input_list, link) {
987 if (input->focus_widget == widget)
988 input->focus_widget = NULL;
989 }
990
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500991 wl_list_remove(&widget->link);
992 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993}
994
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400995void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500996widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500998 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999}
1000
1001void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001002widget_set_size(struct widget *widget, int32_t width, int32_t height)
1003{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001004 widget->allocation.width = width;
1005 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001006}
1007
1008void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009widget_set_allocation(struct widget *widget,
1010 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001011{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 widget->allocation.x = x;
1013 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001014 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015}
1016
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001017void
1018widget_set_transparent(struct widget *widget, int transparent)
1019{
1020 widget->opaque = !transparent;
1021}
1022
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027}
1028
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001029void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001030widget_set_resize_handler(struct widget *widget,
1031 widget_resize_handler_t handler)
1032{
1033 widget->resize_handler = handler;
1034}
1035
1036void
1037widget_set_redraw_handler(struct widget *widget,
1038 widget_redraw_handler_t handler)
1039{
1040 widget->redraw_handler = handler;
1041}
1042
1043void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001044widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001045{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001046 widget->enter_handler = handler;
1047}
1048
1049void
1050widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1051{
1052 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001053}
1054
1055void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001056widget_set_motion_handler(struct widget *widget,
1057 widget_motion_handler_t handler)
1058{
1059 widget->motion_handler = handler;
1060}
1061
1062void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001063widget_set_button_handler(struct widget *widget,
1064 widget_button_handler_t handler)
1065{
1066 widget->button_handler = handler;
1067}
1068
1069void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001070widget_schedule_redraw(struct widget *widget)
1071{
1072 window_schedule_redraw(widget->window);
1073}
1074
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001075cairo_surface_t *
1076window_get_surface(struct window *window)
1077{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001078 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001079}
1080
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001081struct wl_surface *
1082window_get_wl_surface(struct window *window)
1083{
1084 return window->surface;
1085}
1086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087struct wl_shell_surface *
1088window_get_wl_shell_surface(struct window *window)
1089{
1090 return window->shell_surface;
1091}
1092
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001093static void
1094frame_resize_handler(struct widget *widget,
1095 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001096{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001097 struct frame *frame = data;
1098 struct widget *child = frame->child;
1099 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001100 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001101 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001102 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001103
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001104 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001105 decoration_width = (frame->width + frame->margin) * 2;
1106 decoration_height = frame->width +
1107 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001108
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001109 allocation.x = frame->width + frame->margin;
1110 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001111 allocation.width = width - decoration_width;
1112 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001113
1114 widget->window->input_region =
1115 wl_compositor_create_region(display->compositor);
1116 wl_region_add(widget->window->input_region,
1117 frame->margin, frame->margin,
1118 width - 2 * frame->margin,
1119 height - 2 * frame->margin);
1120
1121 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001122 } else {
1123 decoration_width = 0;
1124 decoration_height = 0;
1125
1126 allocation.x = 0;
1127 allocation.y = 0;
1128 allocation.width = width;
1129 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001130 opaque_margin = 0;
1131 }
1132
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001133 widget_set_allocation(child, allocation.x, allocation.y,
1134 allocation.width, allocation.height);
1135
1136 if (child->resize_handler)
1137 child->resize_handler(child,
1138 allocation.width,
1139 allocation.height,
1140 child->user_data);
1141
1142 widget_set_allocation(widget, 0, 0,
1143 child->allocation.width + decoration_width,
1144 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001145
1146 if (child->opaque) {
1147 widget->window->opaque_region =
1148 wl_compositor_create_region(display->compositor);
1149 wl_region_add(widget->window->opaque_region,
1150 opaque_margin, opaque_margin,
1151 widget->allocation.width - 2 * opaque_margin,
1152 widget->allocation.height - 2 * opaque_margin);
1153 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001154}
1155
1156static void
1157frame_redraw_handler(struct widget *widget, void *data)
1158{
1159 struct frame *frame = data;
1160 cairo_t *cr;
1161 cairo_text_extents_t extents;
1162 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001163 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001164 struct window *window = widget->window;
1165
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001166 if (window->type == TYPE_FULLSCREEN)
1167 return;
1168
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 width = widget->allocation.width;
1170 height = widget->allocation.height;
1171
1172 cr = cairo_create(window->cairo_surface);
1173
1174 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1175 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1176 cairo_paint(cr);
1177
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001178 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001179 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001180 2, 2, width + 8, height + 8,
1181 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001182
1183 if (window->keyboard_device)
1184 source = window->display->active_frame;
1185 else
1186 source = window->display->inactive_frame;
1187
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001188 tile_source(cr, source,
1189 frame->margin, frame->margin,
1190 width - frame->margin * 2, height - frame->margin * 2,
1191 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001192
1193 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001194 cairo_select_font_face(cr, "sans",
1195 CAIRO_FONT_SLANT_NORMAL,
1196 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001197 cairo_set_font_size(cr, 14);
1198 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001199 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001200 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001201 if (window->keyboard_device) {
1202 cairo_move_to(cr, x + 1, y + 1);
1203 cairo_set_source_rgb(cr, 1, 1, 1);
1204 cairo_show_text(cr, window->title);
1205 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001206 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001207 cairo_show_text(cr, window->title);
1208 } else {
1209 cairo_move_to(cr, x, y);
1210 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1211 cairo_show_text(cr, window->title);
1212 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001213
1214 cairo_destroy(cr);
1215}
1216
1217static int
1218frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1219{
1220 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001221 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001222 const int grip_size = 8;
1223
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001224 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001225 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001226 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001229 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231 hlocation = WINDOW_RESIZING_RIGHT;
1232 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001233 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242 vlocation = WINDOW_RESIZING_BOTTOM;
1243 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001244 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 location = vlocation | hlocation;
1247 if (location & WINDOW_EXTERIOR)
1248 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 location = WINDOW_TITLEBAR;
1251 else if (location == WINDOW_INTERIOR)
1252 location = WINDOW_CLIENT_AREA;
1253
1254 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255}
1256
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001257static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001260 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001262 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 switch (location) {
1264 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001265 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001267 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001269 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001271 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001273 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001275 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001277 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001279 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001280 case WINDOW_EXTERIOR:
1281 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001283 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001285}
1286
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287static void
1288frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001289{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290 switch (index) {
1291 case 0: /* close */
1292 if (window->close_handler)
1293 window->close_handler(window->parent,
1294 window->user_data);
1295 else
1296 display_exit(window->display);
1297 break;
1298 case 1: /* fullscreen */
1299 /* we don't have a way to get out of fullscreen for now */
1300 window_set_fullscreen(window, 1);
1301 break;
1302 case 2: /* rotate */
1303 case 3: /* scale */
1304 break;
1305 }
1306}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001308void
1309window_show_frame_menu(struct window *window,
1310 struct input *input, uint32_t time)
1311{
1312 int32_t x, y;
1313
1314 static const char *entries[] = {
1315 "Close", "Fullscreen", "Rotate", "Scale"
1316 };
1317
1318 input_get_position(input, &x, &y);
1319 window_show_menu(window->display, input, time, window,
1320 x - 10, y - 10, frame_menu_func, entries, 4);
1321}
1322
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001323static int
1324frame_enter_handler(struct widget *widget,
1325 struct input *input, uint32_t time,
1326 int32_t x, int32_t y, void *data)
1327{
1328 return frame_get_pointer_image_for_location(data, input);
1329}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001330
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001331static int
1332frame_motion_handler(struct widget *widget,
1333 struct input *input, uint32_t time,
1334 int32_t x, int32_t y, void *data)
1335{
1336 return frame_get_pointer_image_for_location(data, input);
1337}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001338
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001339static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001340frame_button_handler(struct widget *widget,
1341 struct input *input, uint32_t time,
1342 int button, int state, void *data)
1343
1344{
1345 struct frame *frame = data;
1346 struct window *window = widget->window;
1347 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348 location = frame_get_pointer_location(frame, input->sx, input->sy);
1349
1350 if (window->display->shell && button == BTN_LEFT && state == 1) {
1351 switch (location) {
1352 case WINDOW_TITLEBAR:
1353 if (!window->shell_surface)
1354 break;
1355 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001356 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001357 wl_shell_surface_move(window->shell_surface,
1358 input_get_input_device(input),
1359 time);
1360 break;
1361 case WINDOW_RESIZING_TOP:
1362 case WINDOW_RESIZING_BOTTOM:
1363 case WINDOW_RESIZING_LEFT:
1364 case WINDOW_RESIZING_RIGHT:
1365 case WINDOW_RESIZING_TOP_LEFT:
1366 case WINDOW_RESIZING_TOP_RIGHT:
1367 case WINDOW_RESIZING_BOTTOM_LEFT:
1368 case WINDOW_RESIZING_BOTTOM_RIGHT:
1369 if (!window->shell_surface)
1370 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001371 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001372 wl_shell_surface_resize(window->shell_surface,
1373 input_get_input_device(input),
1374 time, location);
1375 break;
1376 }
1377 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001378 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001379 }
1380}
1381
1382struct widget *
1383frame_create(struct window *window, void *data)
1384{
1385 struct frame *frame;
1386
1387 frame = malloc(sizeof *frame);
1388 memset(frame, 0, sizeof *frame);
1389
1390 frame->widget = window_add_widget(window, frame);
1391 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001392 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001393 frame->width = 4;
1394 frame->titlebar_height = 30
1395;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001396 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1397 widget_set_resize_handler(frame->widget, frame_resize_handler);
1398 widget_set_enter_handler(frame->widget, frame_enter_handler);
1399 widget_set_motion_handler(frame->widget, frame_motion_handler);
1400 widget_set_button_handler(frame->widget, frame_button_handler);
1401
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001402 window->frame = frame;
1403
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001404 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001405}
1406
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001407static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001408frame_destroy(struct frame *frame)
1409{
1410 /* frame->child must be destroyed by the application */
1411 widget_destroy(frame->widget);
1412 free(frame);
1413}
1414
1415static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001416input_set_focus_widget(struct input *input, struct widget *focus,
1417 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001418{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001419 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001420 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001421
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001422 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001423 return;
1424
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001425 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001426 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001427 widget = old;
1428 if (input->grab)
1429 widget = input->grab;
1430 if (widget->leave_handler)
1431 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001432 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001433 }
1434
1435 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001436 widget = focus;
1437 if (input->grab)
1438 widget = input->grab;
1439 if (widget->enter_handler)
1440 pointer = widget->enter_handler(focus, input, time,
1441 x, y,
1442 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001443 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001444
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001445 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001446 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001447}
1448
1449static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001450input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001451 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001452{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001453 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001454 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001455 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001456 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001457
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001458 input->sx = sx;
1459 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001460
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001461 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001462 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001463 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001464 }
1465
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001466 if (input->grab)
1467 widget = input->grab;
1468 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001469 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001470 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001471 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001472 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001473 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001474
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001475 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001476}
1477
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001478void
1479input_grab(struct input *input, struct widget *widget, uint32_t button)
1480{
1481 input->grab = widget;
1482 input->grab_button = button;
1483}
1484
1485void
1486input_ungrab(struct input *input, uint32_t time)
1487{
1488 struct widget *widget;
1489
1490 input->grab = NULL;
1491 if (input->pointer_focus) {
1492 widget = widget_find_widget(input->pointer_focus->widget,
1493 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001494 input_set_focus_widget(input, widget,
1495 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001496 }
1497}
1498
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001499static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001500input_handle_button(void *data,
1501 struct wl_input_device *input_device,
1502 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001503{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001504 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001505 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001506
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001507 if (input->focus_widget && input->grab == NULL && state)
1508 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001509
Neil Roberts6b28aad2012-01-23 19:11:18 +00001510 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001511 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001512 (*widget->button_handler)(widget,
1513 input, time,
1514 button, state,
1515 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001516
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001517 if (input->grab && input->grab_button == button && !state)
1518 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001519}
1520
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001521static void
Scott Moreau210d0792012-03-22 10:47:01 -06001522input_handle_axis(void *data,
1523 struct wl_input_device *input_device,
1524 uint32_t time, uint32_t axis, int32_t value)
1525{
1526}
1527
1528static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001529input_handle_key(void *data, struct wl_input_device *input_device,
1530 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001531{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001532 struct input *input = data;
1533 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001534 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001535 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001536
Daniel Stone0d5a5092012-02-16 12:48:00 +00001537 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001538 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001539 return;
1540
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001541 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001542 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001543 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1544 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001545
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001546 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1547
1548 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001549 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001550 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001552
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001553 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1554 if (state)
1555 window_set_maximized(window,
1556 window->type != TYPE_MAXIMIZED);
1557 } else if (window->key_handler) {
1558 (*window->key_handler)(window, input, time, key,
1559 sym, state, window->user_data);
1560 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001561}
1562
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001563static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001564input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001565{
1566 struct window *window = input->pointer_focus;
1567
1568 if (!window)
1569 return;
1570
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001571 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001572
Pekka Paalanene1207c72011-12-16 12:02:09 +02001573 input->pointer_focus = NULL;
1574 input->current_pointer_image = POINTER_UNSET;
1575}
1576
1577static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001578input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001579 struct wl_input_device *input_device,
1580 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001581 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001582{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001583 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001584 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001585 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001586
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001587 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001588 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001589
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001590 input->sx = sx;
1591 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001592
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001593 widget = widget_find_widget(window->widget, sx, sy);
1594 input_set_focus_widget(input, widget, time, sx, sy);
1595}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001596
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001597static void
1598input_handle_pointer_leave(void *data,
1599 struct wl_input_device *input_device,
1600 uint32_t time, struct wl_surface *surface)
1601{
1602 struct input *input = data;
1603
1604 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001605}
1606
1607static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001608input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001609{
1610 struct window *window = input->keyboard_focus;
1611
1612 if (!window)
1613 return;
1614
1615 window->keyboard_device = NULL;
1616 if (window->keyboard_focus_handler)
1617 (*window->keyboard_focus_handler)(window, NULL,
1618 window->user_data);
1619
1620 input->keyboard_focus = NULL;
1621}
1622
1623static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001624input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001625 struct wl_input_device *input_device,
1626 uint32_t time,
1627 struct wl_surface *surface,
1628 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001629{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001630 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001631 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001632 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001633 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001634
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001635 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001636
1637 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001638 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001639 for (k = keys->data; k < end; k++)
1640 input->modifiers |= d->xkb->map->modmap[*k];
1641
1642 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001643 window->keyboard_device = input;
1644 if (window->keyboard_focus_handler)
1645 (*window->keyboard_focus_handler)(window,
1646 window->keyboard_device,
1647 window->user_data);
1648}
1649
1650static void
1651input_handle_keyboard_leave(void *data,
1652 struct wl_input_device *input_device,
1653 uint32_t time,
1654 struct wl_surface *surface)
1655{
1656 struct input *input = data;
1657
1658 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001659}
1660
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001661static void
1662input_handle_touch_down(void *data,
1663 struct wl_input_device *wl_input_device,
1664 uint32_t time, struct wl_surface *surface,
1665 int32_t id, int32_t x, int32_t y)
1666{
1667}
1668
1669static void
1670input_handle_touch_up(void *data,
1671 struct wl_input_device *wl_input_device,
1672 uint32_t time, int32_t id)
1673{
1674}
1675
1676static void
1677input_handle_touch_motion(void *data,
1678 struct wl_input_device *wl_input_device,
1679 uint32_t time, int32_t id, int32_t x, int32_t y)
1680{
1681}
1682
1683static void
1684input_handle_touch_frame(void *data,
1685 struct wl_input_device *wl_input_device)
1686{
1687}
1688
1689static void
1690input_handle_touch_cancel(void *data,
1691 struct wl_input_device *wl_input_device)
1692{
1693}
1694
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001695static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001696 input_handle_motion,
1697 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001698 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001699 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001700 input_handle_pointer_enter,
1701 input_handle_pointer_leave,
1702 input_handle_keyboard_enter,
1703 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001704 input_handle_touch_down,
1705 input_handle_touch_up,
1706 input_handle_touch_motion,
1707 input_handle_touch_frame,
1708 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001709};
1710
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001711void
1712input_get_position(struct input *input, int32_t *x, int32_t *y)
1713{
1714 *x = input->sx;
1715 *y = input->sy;
1716}
1717
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001718struct wl_input_device *
1719input_get_input_device(struct input *input)
1720{
1721 return input->input_device;
1722}
1723
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001724uint32_t
1725input_get_modifiers(struct input *input)
1726{
1727 return input->modifiers;
1728}
1729
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001730struct widget *
1731input_get_focus_widget(struct input *input)
1732{
1733 return input->focus_widget;
1734}
1735
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001736struct data_offer {
1737 struct wl_data_offer *offer;
1738 struct input *input;
1739 struct wl_array types;
1740 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001741
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001742 struct task io_task;
1743 int fd;
1744 data_func_t func;
1745 int32_t x, y;
1746 void *user_data;
1747};
1748
1749static void
1750data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1751{
1752 struct data_offer *offer = data;
1753 char **p;
1754
1755 p = wl_array_add(&offer->types, sizeof *p);
1756 *p = strdup(type);
1757}
1758
1759static const struct wl_data_offer_listener data_offer_listener = {
1760 data_offer_offer,
1761};
1762
1763static void
1764data_offer_destroy(struct data_offer *offer)
1765{
1766 char **p;
1767
1768 offer->refcount--;
1769 if (offer->refcount == 0) {
1770 wl_data_offer_destroy(offer->offer);
1771 for (p = offer->types.data; *p; p++)
1772 free(*p);
1773 wl_array_release(&offer->types);
1774 free(offer);
1775 }
1776}
1777
1778static void
1779data_device_data_offer(void *data,
1780 struct wl_data_device *data_device, uint32_t id)
1781{
1782 struct data_offer *offer;
1783
1784 offer = malloc(sizeof *offer);
1785
1786 wl_array_init(&offer->types);
1787 offer->refcount = 1;
1788 offer->input = data;
1789
1790 /* FIXME: Generate typesafe wrappers for this */
1791 offer->offer = (struct wl_data_offer *)
1792 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1793 id, &wl_data_offer_interface);
1794
1795 wl_data_offer_add_listener(offer->offer,
1796 &data_offer_listener, offer);
1797}
1798
1799static void
1800data_device_enter(void *data, struct wl_data_device *data_device,
1801 uint32_t time, struct wl_surface *surface,
1802 int32_t x, int32_t y, struct wl_data_offer *offer)
1803{
1804 struct input *input = data;
1805 struct window *window;
1806 char **p;
1807
1808 input->drag_offer = wl_data_offer_get_user_data(offer);
1809 window = wl_surface_get_user_data(surface);
1810 input->pointer_focus = window;
1811
1812 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1813 *p = NULL;
1814
1815 window = input->pointer_focus;
1816 if (window->data_handler)
1817 window->data_handler(window, input, time, x, y,
1818 input->drag_offer->types.data,
1819 window->user_data);
1820}
1821
1822static void
1823data_device_leave(void *data, struct wl_data_device *data_device)
1824{
1825 struct input *input = data;
1826
1827 data_offer_destroy(input->drag_offer);
1828 input->drag_offer = NULL;
1829}
1830
1831static void
1832data_device_motion(void *data, struct wl_data_device *data_device,
1833 uint32_t time, int32_t x, int32_t y)
1834{
1835 struct input *input = data;
1836 struct window *window = input->pointer_focus;
1837
1838 input->sx = x;
1839 input->sy = y;
1840
1841 if (window->data_handler)
1842 window->data_handler(window, input, time, x, y,
1843 input->drag_offer->types.data,
1844 window->user_data);
1845}
1846
1847static void
1848data_device_drop(void *data, struct wl_data_device *data_device)
1849{
1850 struct input *input = data;
1851 struct window *window = input->pointer_focus;
1852
1853 if (window->drop_handler)
1854 window->drop_handler(window, input,
1855 input->sx, input->sy, window->user_data);
1856}
1857
1858static void
1859data_device_selection(void *data,
1860 struct wl_data_device *wl_data_device,
1861 struct wl_data_offer *offer)
1862{
1863 struct input *input = data;
1864 char **p;
1865
1866 if (input->selection_offer)
1867 data_offer_destroy(input->selection_offer);
1868
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001869 if (offer) {
1870 input->selection_offer = wl_data_offer_get_user_data(offer);
1871 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1872 *p = NULL;
1873 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001874}
1875
1876static const struct wl_data_device_listener data_device_listener = {
1877 data_device_data_offer,
1878 data_device_enter,
1879 data_device_leave,
1880 data_device_motion,
1881 data_device_drop,
1882 data_device_selection
1883};
1884
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001885void
1886input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1887{
1888 struct display *display = input->display;
1889 struct wl_buffer *buffer;
1890 cairo_surface_t *surface;
1891
1892 if (pointer == input->current_pointer_image)
1893 return;
1894
1895 input->current_pointer_image = pointer;
1896 surface = display->pointer_surfaces[pointer];
1897
1898 if (!surface)
1899 return;
1900
1901 buffer = display_get_buffer_for_surface(display, surface);
1902 wl_input_device_attach(input->input_device, time, buffer,
1903 pointer_images[pointer].hotspot_x,
1904 pointer_images[pointer].hotspot_y);
1905}
1906
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001907struct wl_data_device *
1908input_get_data_device(struct input *input)
1909{
1910 return input->data_device;
1911}
1912
1913void
1914input_set_selection(struct input *input,
1915 struct wl_data_source *source, uint32_t time)
1916{
1917 wl_data_device_set_selection(input->data_device, source, time);
1918}
1919
1920void
1921input_accept(struct input *input, uint32_t time, const char *type)
1922{
1923 wl_data_offer_accept(input->drag_offer->offer, time, type);
1924}
1925
1926static void
1927offer_io_func(struct task *task, uint32_t events)
1928{
1929 struct data_offer *offer =
1930 container_of(task, struct data_offer, io_task);
1931 unsigned int len;
1932 char buffer[4096];
1933
1934 len = read(offer->fd, buffer, sizeof buffer);
1935 offer->func(buffer, len,
1936 offer->x, offer->y, offer->user_data);
1937
1938 if (len == 0) {
1939 close(offer->fd);
1940 data_offer_destroy(offer);
1941 }
1942}
1943
1944static void
1945data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1946 data_func_t func, void *user_data)
1947{
1948 int p[2];
1949
1950 pipe2(p, O_CLOEXEC);
1951 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1952 close(p[1]);
1953
1954 offer->io_task.run = offer_io_func;
1955 offer->fd = p[0];
1956 offer->func = func;
1957 offer->refcount++;
1958 offer->user_data = user_data;
1959
1960 display_watch_fd(offer->input->display,
1961 offer->fd, EPOLLIN, &offer->io_task);
1962}
1963
1964void
1965input_receive_drag_data(struct input *input, const char *mime_type,
1966 data_func_t func, void *data)
1967{
1968 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1969 input->drag_offer->x = input->sx;
1970 input->drag_offer->y = input->sy;
1971}
1972
1973int
1974input_receive_selection_data(struct input *input, const char *mime_type,
1975 data_func_t func, void *data)
1976{
1977 char **p;
1978
1979 if (input->selection_offer == NULL)
1980 return -1;
1981
1982 for (p = input->selection_offer->types.data; *p; p++)
1983 if (strcmp(mime_type, *p) == 0)
1984 break;
1985
1986 if (*p == NULL)
1987 return -1;
1988
1989 data_offer_receive_data(input->selection_offer,
1990 mime_type, func, data);
1991 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001992}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001993
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001994int
1995input_receive_selection_data_to_fd(struct input *input,
1996 const char *mime_type, int fd)
1997{
1998 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1999
2000 return 0;
2001}
2002
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002003void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002004window_move(struct window *window, struct input *input, uint32_t time)
2005{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002006 if (!window->shell_surface)
2007 return;
2008
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002009 wl_shell_surface_move(window->shell_surface,
2010 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002011}
2012
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002013static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002014idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002015{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002016 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002017
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002018 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002019 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002020 widget_set_allocation(widget,
2021 window->pending_allocation.x,
2022 window->pending_allocation.y,
2023 window->pending_allocation.width,
2024 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002025
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002026 if (window->input_region) {
2027 wl_region_destroy(window->input_region);
2028 window->input_region = NULL;
2029 }
2030
2031 if (window->opaque_region) {
2032 wl_region_destroy(window->opaque_region);
2033 window->opaque_region = NULL;
2034 }
2035
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002036 if (widget->resize_handler)
2037 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002038 widget->allocation.width,
2039 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002040 widget->user_data);
2041
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002042 if (window->allocation.width != widget->allocation.width ||
2043 window->allocation.height != widget->allocation.height) {
2044 window->allocation = widget->allocation;
2045 window_schedule_redraw(window);
2046 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002047}
2048
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002049void
2050window_schedule_resize(struct window *window, int width, int height)
2051{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002052 window->pending_allocation.x = 0;
2053 window->pending_allocation.y = 0;
2054 window->pending_allocation.width = width;
2055 window->pending_allocation.height = height;
2056
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002057 window->resize_needed = 1;
2058 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002059}
2060
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002061void
2062widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2063{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002064 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002065}
2066
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002067static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002068handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002069 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002070 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002071{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002072 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002073
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002074 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002075 return;
2076
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002077 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002078 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002079}
2080
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002082menu_destroy(struct menu *menu)
2083{
2084 widget_destroy(menu->widget);
2085 window_destroy(menu->window);
2086 free(menu);
2087}
2088
2089static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002090handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2091{
2092 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002093 struct menu *menu = window->widget->user_data;
2094
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002095 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002096 * device. Or just use wl_callback. And this really needs to
2097 * be a window vfunc that the menu can set. And we need the
2098 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002099
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002100 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002101 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002102 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002103}
2104
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002105static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002106 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002107 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002108};
2109
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002110void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002111window_get_allocation(struct window *window,
2112 struct rectangle *allocation)
2113{
2114 *allocation = window->allocation;
2115}
2116
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002117static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002118widget_redraw(struct widget *widget)
2119{
2120 struct widget *child;
2121
2122 if (widget->redraw_handler)
2123 widget->redraw_handler(widget, widget->user_data);
2124 wl_list_for_each(child, &widget->child_list, link)
2125 widget_redraw(child);
2126}
2127
2128static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002129frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2130{
2131 struct window *window = data;
2132
2133 wl_callback_destroy(callback);
2134 window->redraw_scheduled = 0;
2135 if (window->redraw_needed)
2136 window_schedule_redraw(window);
2137}
2138
2139static const struct wl_callback_listener listener = {
2140 frame_callback
2141};
2142
2143static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002144idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002145{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002146 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002147 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002148
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002149 if (window->resize_needed)
2150 idle_resize(window);
2151
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002152 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002153 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002154 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002155 window->redraw_needed = 0;
2156
2157 callback = wl_surface_frame(window->surface);
2158 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002159}
2160
2161void
2162window_schedule_redraw(struct window *window)
2163{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002164 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002165 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002166 window->redraw_task.run = idle_redraw;
2167 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002168 window->redraw_scheduled = 1;
2169 }
2170}
2171
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002172void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002173window_set_custom(struct window *window)
2174{
2175 window->type = TYPE_CUSTOM;
2176}
2177
2178void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002179window_set_fullscreen(struct window *window, int fullscreen)
2180{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002181 if (!window->display->shell)
2182 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002183
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002184 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002185 return;
2186
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002187 if (fullscreen) {
2188 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002189 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002190 wl_shell_surface_set_fullscreen(window->shell_surface,
2191 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2192 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002193 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002194 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002195 wl_shell_surface_set_toplevel(window->shell_surface);
2196 window_schedule_resize(window,
2197 window->saved_allocation.width,
2198 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002199 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002200}
2201
2202void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002203window_set_maximized(struct window *window, int maximized)
2204{
2205 if (!window->display->shell)
2206 return;
2207
2208 if ((window->type == TYPE_MAXIMIZED) == maximized)
2209 return;
2210
2211 if (window->type == TYPE_TOPLEVEL) {
2212 window->saved_allocation = window->allocation;
2213 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2214 window->type = TYPE_MAXIMIZED;
2215 } else {
2216 wl_shell_surface_set_toplevel(window->shell_surface);
2217 window->type = TYPE_TOPLEVEL;
2218 window_schedule_resize(window,
2219 window->saved_allocation.width,
2220 window->saved_allocation.height);
2221 }
2222}
2223
2224void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002225window_set_user_data(struct window *window, void *data)
2226{
2227 window->user_data = data;
2228}
2229
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002230void *
2231window_get_user_data(struct window *window)
2232{
2233 return window->user_data;
2234}
2235
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002236void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002237window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002238 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002239{
2240 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002241}
2242
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002243void
2244window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002245 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002246{
2247 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002248}
2249
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002250void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002251window_set_data_handler(struct window *window, window_data_handler_t handler)
2252{
2253 window->data_handler = handler;
2254}
2255
2256void
2257window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2258{
2259 window->drop_handler = handler;
2260}
2261
2262void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002263window_set_close_handler(struct window *window,
2264 window_close_handler_t handler)
2265{
2266 window->close_handler = handler;
2267}
2268
2269void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002270window_set_title(struct window *window, const char *title)
2271{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002272 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002273 window->title = strdup(title);
2274}
2275
2276const char *
2277window_get_title(struct window *window)
2278{
2279 return window->title;
2280}
2281
2282void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002283display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2284 int32_t x, int32_t y, int32_t width, int32_t height)
2285{
2286 struct wl_buffer *buffer;
2287
2288 buffer = display_get_buffer_for_surface(display, cairo_surface);
2289
2290 wl_buffer_damage(buffer, x, y, width, height);
2291}
2292
2293void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002294window_damage(struct window *window, int32_t x, int32_t y,
2295 int32_t width, int32_t height)
2296{
2297 wl_surface_damage(window->surface, x, y, width, height);
2298}
2299
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002300static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002301window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002302{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002303 struct window *window;
2304
2305 window = malloc(sizeof *window);
2306 if (window == NULL)
2307 return NULL;
2308
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002309 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002310 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002311 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002312 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002313 if (display->shell) {
2314 window->shell_surface =
2315 wl_shell_get_shell_surface(display->shell,
2316 window->surface);
2317 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002318 window->allocation.x = 0;
2319 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002320 window->allocation.width = 0;
2321 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002322 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002323 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002324 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002325 window->input_region = NULL;
2326 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002327
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002328 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002329#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002330 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2331 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002332#else
2333 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2334#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002335 else
2336 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002337
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002338 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002339 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002340
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002341 if (window->shell_surface) {
2342 wl_shell_surface_set_user_data(window->shell_surface, window);
2343 wl_shell_surface_add_listener(window->shell_surface,
2344 &shell_surface_listener, window);
2345 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002346
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002347 return window;
2348}
2349
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002350struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002351window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002352{
2353 struct window *window;
2354
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002355 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002356 if (!window)
2357 return NULL;
2358
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002359 window->type = TYPE_TOPLEVEL;
2360 if (display->shell)
2361 wl_shell_surface_set_toplevel(window->shell_surface);
2362
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002363 return window;
2364}
2365
2366struct window *
2367window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002368 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002369{
2370 struct window *window;
2371
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002372 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002373 if (!window)
2374 return NULL;
2375
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002376 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002377 window->x = x;
2378 window->y = y;
2379
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002380 if (display->shell)
2381 wl_shell_surface_set_transient(window->shell_surface,
2382 window->parent->shell_surface,
2383 window->x, window->y, 0);
2384
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002385 return window;
2386}
2387
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002388static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002389menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002390{
2391 int next;
2392
2393 next = (sy - 8) / 20;
2394 if (menu->current != next) {
2395 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002396 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002398}
2399
2400static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002401menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002402 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002403 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002404{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002405 struct menu *menu = data;
2406
2407 if (widget == menu->widget)
2408 menu_set_item(data, y);
2409
2410 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411}
2412
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002413static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002414menu_enter_handler(struct widget *widget,
2415 struct input *input, uint32_t time,
2416 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002417{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002418 struct menu *menu = data;
2419
2420 if (widget == menu->widget)
2421 menu_set_item(data, y);
2422
2423 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002424}
2425
2426static void
2427menu_leave_handler(struct widget *widget, struct input *input, void *data)
2428{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002429 struct menu *menu = data;
2430
2431 if (widget == menu->widget)
2432 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433}
2434
2435static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002436menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002437 struct input *input, uint32_t time,
2438 int button, int state, void *data)
2439
2440{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002441 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002442
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002443 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002444 /* Either relase after press-drag-release or
2445 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002446 menu->func(menu->window->parent,
2447 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002448 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002449 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002450 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002451}
2452
2453static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002454menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002455{
2456 cairo_t *cr;
2457 const int32_t r = 3, margin = 3;
2458 struct menu *menu = data;
2459 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002460 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002461
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462 cr = cairo_create(window->cairo_surface);
2463 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2464 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2465 cairo_paint(cr);
2466
2467 width = window->allocation.width;
2468 height = window->allocation.height;
2469 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002470
2471 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002472 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2473 cairo_fill(cr);
2474
2475 for (i = 0; i < menu->count; i++) {
2476 if (i == menu->current) {
2477 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2478 cairo_rectangle(cr, margin, i * 20 + margin,
2479 width - 2 * margin, 20);
2480 cairo_fill(cr);
2481 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2482 cairo_move_to(cr, 10, i * 20 + 16);
2483 cairo_show_text(cr, menu->entries[i]);
2484 } else {
2485 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2486 cairo_move_to(cr, 10, i * 20 + 16);
2487 cairo_show_text(cr, menu->entries[i]);
2488 }
2489 }
2490
2491 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002492}
2493
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002494void
2495window_show_menu(struct display *display,
2496 struct input *input, uint32_t time, struct window *parent,
2497 int32_t x, int32_t y,
2498 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002499{
2500 struct window *window;
2501 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002502 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002503
2504 menu = malloc(sizeof *menu);
2505 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002506 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002507
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002508 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002509 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002510 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002511
2512 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002513 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002514 menu->entries = entries;
2515 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002516 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002517 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002518 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002519 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002520 window->type = TYPE_MENU;
2521 window->x = x;
2522 window->y = y;
2523
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002524 wl_shell_surface_set_popup(window->shell_surface,
2525 input->input_device, time,
2526 window->parent->shell_surface,
2527 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002528
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002529 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002530 widget_set_enter_handler(menu->widget, menu_enter_handler);
2531 widget_set_leave_handler(menu->widget, menu_leave_handler);
2532 widget_set_motion_handler(menu->widget, menu_motion_handler);
2533 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002534
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002535 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002536 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002537}
2538
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002539void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002540window_set_buffer_type(struct window *window, enum window_buffer_type type)
2541{
2542 window->buffer_type = type;
2543}
2544
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002545
2546static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002547display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002548 struct wl_output *wl_output,
2549 int x, int y,
2550 int physical_width,
2551 int physical_height,
2552 int subpixel,
2553 const char *make,
2554 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002555{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002556 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002557
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002558 output->allocation.x = x;
2559 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002560}
2561
2562static void
2563display_handle_mode(void *data,
2564 struct wl_output *wl_output,
2565 uint32_t flags,
2566 int width,
2567 int height,
2568 int refresh)
2569{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002570 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002571 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002572
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002573 if (flags & WL_OUTPUT_MODE_CURRENT) {
2574 output->allocation.width = width;
2575 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002576 if (display->output_configure_handler)
2577 (*display->output_configure_handler)(
2578 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002580}
2581
2582static const struct wl_output_listener output_listener = {
2583 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002584 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002585};
2586
2587static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002588display_add_output(struct display *d, uint32_t id)
2589{
2590 struct output *output;
2591
2592 output = malloc(sizeof *output);
2593 if (output == NULL)
2594 return;
2595
2596 memset(output, 0, sizeof *output);
2597 output->display = d;
2598 output->output =
2599 wl_display_bind(d->display, id, &wl_output_interface);
2600 wl_list_insert(d->output_list.prev, &output->link);
2601
2602 wl_output_add_listener(output->output, &output_listener, output);
2603}
2604
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002605static void
2606output_destroy(struct output *output)
2607{
2608 if (output->destroy_handler)
2609 (*output->destroy_handler)(output, output->user_data);
2610
2611 wl_output_destroy(output->output);
2612 wl_list_remove(&output->link);
2613 free(output);
2614}
2615
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002616void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002617display_set_output_configure_handler(struct display *display,
2618 display_output_handler_t handler)
2619{
2620 struct output *output;
2621
2622 display->output_configure_handler = handler;
2623 if (!handler)
2624 return;
2625
2626 wl_list_for_each(output, &display->output_list, link)
2627 (*display->output_configure_handler)(output,
2628 display->user_data);
2629}
2630
2631void
2632output_set_user_data(struct output *output, void *data)
2633{
2634 output->user_data = data;
2635}
2636
2637void *
2638output_get_user_data(struct output *output)
2639{
2640 return output->user_data;
2641}
2642
2643void
2644output_set_destroy_handler(struct output *output,
2645 display_output_handler_t handler)
2646{
2647 output->destroy_handler = handler;
2648 /* FIXME: implement this, once we have way to remove outputs */
2649}
2650
2651void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002652output_get_allocation(struct output *output, struct rectangle *allocation)
2653{
2654 *allocation = output->allocation;
2655}
2656
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002657struct wl_output *
2658output_get_wl_output(struct output *output)
2659{
2660 return output->output;
2661}
2662
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002663static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002664display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002665{
2666 struct input *input;
2667
2668 input = malloc(sizeof *input);
2669 if (input == NULL)
2670 return;
2671
2672 memset(input, 0, sizeof *input);
2673 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002674 input->input_device =
2675 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002676 input->pointer_focus = NULL;
2677 input->keyboard_focus = NULL;
2678 wl_list_insert(d->input_list.prev, &input->link);
2679
2680 wl_input_device_add_listener(input->input_device,
2681 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002682 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002683
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002684 input->data_device =
2685 wl_data_device_manager_get_data_device(d->data_device_manager,
2686 input->input_device);
2687 wl_data_device_add_listener(input->data_device,
2688 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002689}
2690
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002691static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002692input_destroy(struct input *input)
2693{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002694 input_remove_keyboard_focus(input);
2695 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002696
2697 if (input->drag_offer)
2698 data_offer_destroy(input->drag_offer);
2699
2700 if (input->selection_offer)
2701 data_offer_destroy(input->selection_offer);
2702
2703 wl_data_device_destroy(input->data_device);
2704 wl_list_remove(&input->link);
2705 wl_input_device_destroy(input->input_device);
2706 free(input);
2707}
2708
2709static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002710display_handle_global(struct wl_display *display, uint32_t id,
2711 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002712{
2713 struct display *d = data;
2714
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002715 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002716 d->compositor =
2717 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002718 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002719 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002720 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002721 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002722 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002723 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002724 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002725 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002726 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2727 d->data_device_manager =
2728 wl_display_bind(display, id,
2729 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002730 }
2731}
2732
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002733static void
2734display_render_frame(struct display *d)
2735{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002736 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002737 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002738
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002739 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002740 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2741 cr = cairo_create(d->shadow);
2742 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2743 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002744 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002745 cairo_fill(cr);
2746 cairo_destroy(cr);
2747 blur_surface(d->shadow, 64);
2748
2749 d->active_frame =
2750 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2751 cr = cairo_create(d->active_frame);
2752 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002753
2754 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2755 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2756 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2757 cairo_set_source(cr, pattern);
2758 cairo_pattern_destroy(pattern);
2759
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002760 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002761 cairo_fill(cr);
2762 cairo_destroy(cr);
2763
2764 d->inactive_frame =
2765 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2766 cr = cairo_create(d->inactive_frame);
2767 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002768 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002769 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002770 cairo_fill(cr);
2771 cairo_destroy(cr);
2772}
2773
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002774static void
2775init_xkb(struct display *d)
2776{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002777 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002778
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002779 names.rules = "evdev";
2780 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002781 names.layout = option_xkb_layout;
2782 names.variant = option_xkb_variant;
2783 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002784
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002785 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002786 if (!d->xkb) {
2787 fprintf(stderr, "Failed to compile keymap\n");
2788 exit(1);
2789 }
2790}
2791
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002792static void
2793fini_xkb(struct display *display)
2794{
2795 xkb_free_keymap(display->xkb);
2796}
2797
Yuval Fledel45568f62010-12-06 09:18:12 -05002798static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002799init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002800{
2801 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002802 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002803
Rob Clark6396ed32012-03-11 19:48:41 -05002804#ifdef USE_CAIRO_GLESV2
2805# define GL_BIT EGL_OPENGL_ES2_BIT
2806#else
2807# define GL_BIT EGL_OPENGL_BIT
2808#endif
2809
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002810 static const EGLint argb_cfg_attribs[] = {
2811 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002812 EGL_RED_SIZE, 1,
2813 EGL_GREEN_SIZE, 1,
2814 EGL_BLUE_SIZE, 1,
2815 EGL_ALPHA_SIZE, 1,
2816 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002817 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002818 EGL_NONE
2819 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002820
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002821#ifdef USE_CAIRO_GLESV2
2822 static const EGLint context_attribs[] = {
2823 EGL_CONTEXT_CLIENT_VERSION, 2,
2824 EGL_NONE
2825 };
2826 EGLint api = EGL_OPENGL_ES_API;
2827#else
2828 EGLint *context_attribs = NULL;
2829 EGLint api = EGL_OPENGL_API;
2830#endif
2831
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002832 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002833 if (!eglInitialize(d->dpy, &major, &minor)) {
2834 fprintf(stderr, "failed to initialize display\n");
2835 return -1;
2836 }
2837
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002838 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002839 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2840 return -1;
2841 }
2842
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002843 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2844 &d->argb_config, 1, &n) || n != 1) {
2845 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002846 return -1;
2847 }
2848
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002849 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002850 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002851 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002852 fprintf(stderr, "failed to create context\n");
2853 return -1;
2854 }
2855
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002856 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002857 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002858 return -1;
2859 }
2860
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002861#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002862 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2863 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2864 fprintf(stderr, "failed to get cairo egl argb device\n");
2865 return -1;
2866 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002867#endif
2868
2869 return 0;
2870}
2871
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002872static void
2873fini_egl(struct display *display)
2874{
2875#ifdef HAVE_CAIRO_EGL
2876 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002877#endif
2878
2879 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2880 EGL_NO_CONTEXT);
2881
2882 eglTerminate(display->dpy);
2883 eglReleaseThread();
2884}
2885
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002886static int
2887event_mask_update(uint32_t mask, void *data)
2888{
2889 struct display *d = data;
2890
2891 d->mask = mask;
2892
2893 return 0;
2894}
2895
2896static void
2897handle_display_data(struct task *task, uint32_t events)
2898{
2899 struct display *display =
2900 container_of(task, struct display, display_task);
2901
2902 wl_display_iterate(display->display, display->mask);
2903}
2904
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002905struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002906display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002907{
2908 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002909
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002910 argc = parse_options(xkb_options,
2911 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002912
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002913 d = malloc(sizeof *d);
2914 if (d == NULL)
2915 return NULL;
2916
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002917 memset(d, 0, sizeof *d);
2918
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002919 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002920 if (d->display == NULL) {
2921 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002922 return NULL;
2923 }
2924
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002925 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2926 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2927 d->display_task.run = handle_display_data;
2928 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2929
2930 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002931 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002932 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002933
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002934 /* Set up listener so we'll catch all events. */
2935 wl_display_add_global_listener(d->display,
2936 display_handle_global, d);
2937
2938 /* Process connection events. */
2939 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002940 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002941 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002942
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002943 d->image_target_texture_2d =
2944 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2945 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2946 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2947
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002948 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002949
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002950 display_render_frame(d);
2951
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002952 wl_list_init(&d->window_list);
2953
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002954 init_xkb(d);
2955
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002956 return d;
2957}
2958
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002959static void
2960display_destroy_outputs(struct display *display)
2961{
2962 struct output *tmp;
2963 struct output *output;
2964
2965 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2966 output_destroy(output);
2967}
2968
Pekka Paalanene1207c72011-12-16 12:02:09 +02002969static void
2970display_destroy_inputs(struct display *display)
2971{
2972 struct input *tmp;
2973 struct input *input;
2974
2975 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2976 input_destroy(input);
2977}
2978
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002979void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002980display_destroy(struct display *display)
2981{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002982 if (!wl_list_empty(&display->window_list))
2983 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2984
2985 if (!wl_list_empty(&display->deferred_list))
2986 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2987
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002988 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002989 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002990
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002991 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002992
2993 cairo_surface_destroy(display->active_frame);
2994 cairo_surface_destroy(display->inactive_frame);
2995 cairo_surface_destroy(display->shadow);
2996 destroy_pointer_surfaces(display);
2997
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002998 fini_egl(display);
2999
Pekka Paalanenc2052982011-12-16 11:41:32 +02003000 if (display->shell)
3001 wl_shell_destroy(display->shell);
3002
3003 if (display->shm)
3004 wl_shm_destroy(display->shm);
3005
3006 if (display->data_device_manager)
3007 wl_data_device_manager_destroy(display->data_device_manager);
3008
3009 wl_compositor_destroy(display->compositor);
3010
3011 close(display->epoll_fd);
3012
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003013 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003014 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003015 free(display);
3016}
3017
3018void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003019display_set_user_data(struct display *display, void *data)
3020{
3021 display->user_data = data;
3022}
3023
3024void *
3025display_get_user_data(struct display *display)
3026{
3027 return display->user_data;
3028}
3029
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003030struct wl_display *
3031display_get_display(struct display *display)
3032{
3033 return display->display;
3034}
3035
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003036struct output *
3037display_get_output(struct display *display)
3038{
3039 return container_of(display->output_list.next, struct output, link);
3040}
3041
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003042struct wl_compositor *
3043display_get_compositor(struct display *display)
3044{
3045 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003046}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003047
3048EGLDisplay
3049display_get_egl_display(struct display *d)
3050{
3051 return d->dpy;
3052}
3053
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003054struct wl_data_source *
3055display_create_data_source(struct display *display)
3056{
3057 return wl_data_device_manager_create_data_source(display->data_device_manager);
3058}
3059
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003060EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003061display_get_argb_egl_config(struct display *d)
3062{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003063 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003064}
3065
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003066struct wl_shell *
3067display_get_shell(struct display *display)
3068{
3069 return display->shell;
3070}
3071
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003072int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003073display_acquire_window_surface(struct display *display,
3074 struct window *window,
3075 EGLContext ctx)
3076{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003077#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003078 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003079 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003080
3081 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003082 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003083 device = cairo_surface_get_device(window->cairo_surface);
3084 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003085 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003086
Benjamin Franzke0c991632011-09-27 21:57:31 +02003087 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003088 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003089 ctx = display->argb_ctx;
3090 else
3091 assert(0);
3092 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003093
3094 data = cairo_surface_get_user_data(window->cairo_surface,
3095 &surface_data_key);
3096
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003097 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003098 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003099 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3100 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003101
3102 return 0;
3103#else
3104 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003105#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003106}
3107
3108void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003109display_release_window_surface(struct display *display,
3110 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003111{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003112#ifdef HAVE_CAIRO_EGL
3113 cairo_device_t *device;
3114
3115 device = cairo_surface_get_device(window->cairo_surface);
3116 if (!device)
3117 return;
3118
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003119 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003120 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003121 cairo_device_release(device);
3122#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003123}
3124
3125void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003126display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003127{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003128 wl_list_insert(&display->deferred_list, &task->link);
3129}
3130
3131void
3132display_watch_fd(struct display *display,
3133 int fd, uint32_t events, struct task *task)
3134{
3135 struct epoll_event ep;
3136
3137 ep.events = events;
3138 ep.data.ptr = task;
3139 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3140}
3141
3142void
3143display_run(struct display *display)
3144{
3145 struct task *task;
3146 struct epoll_event ep[16];
3147 int i, count;
3148
Pekka Paalanen826d7952011-12-15 10:14:07 +02003149 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003150 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003151 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003152
3153 while (!wl_list_empty(&display->deferred_list)) {
3154 task = container_of(display->deferred_list.next,
3155 struct task, link);
3156 wl_list_remove(&task->link);
3157 task->run(task, 0);
3158 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003159
3160 if (!display->running)
3161 break;
3162
3163 wl_display_flush(display->display);
3164
3165 count = epoll_wait(display->epoll_fd,
3166 ep, ARRAY_LENGTH(ep), -1);
3167 for (i = 0; i < count; i++) {
3168 task = ep[i].data.ptr;
3169 task->run(task, ep[i].events);
3170 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003171 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003172}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003173
3174void
3175display_exit(struct display *display)
3176{
3177 display->running = 0;
3178}