blob: 4c859aad0ebb5fe3604756a56fa7936a03ca330a [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øgsbergb67e94b2012-01-10 12:23:19 -0500128 int resize_scheduled;
129 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400131 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400132 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400133 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500134
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500135 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500136
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500137 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500138 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400139 window_data_handler_t data_handler;
140 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400142
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200143 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500144 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500145
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500146 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400147 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500148};
149
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500150struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500151 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500152 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400153 struct wl_list link;
154 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500155 widget_resize_handler_t resize_handler;
156 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500157 widget_enter_handler_t enter_handler;
158 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500159 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500160 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500162 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400163};
164
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165struct input {
166 struct display *display;
167 struct wl_input_device *input_device;
168 struct window *pointer_focus;
169 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400170 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200172 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400174
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500175 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500176 struct widget *grab;
177 uint32_t grab_button;
178
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400179 struct wl_data_device *data_device;
180 struct data_offer *drag_offer;
181 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400182};
183
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500184struct output {
185 struct display *display;
186 struct wl_output *output;
187 struct rectangle allocation;
188 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200189
190 display_output_handler_t destroy_handler;
191 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500192};
193
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500194struct frame {
195 struct widget *widget;
196 struct widget *child;
197 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400198 int width;
199 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500200};
201
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500202struct menu {
203 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500204 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500205 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500206 const char **entries;
207 uint32_t time;
208 int current;
209 int count;
210 menu_func_t func;
211};
212
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400213enum {
214 POINTER_DEFAULT = 100,
215 POINTER_UNSET
216};
217
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500218enum window_location {
219 WINDOW_INTERIOR = 0,
220 WINDOW_RESIZING_TOP = 1,
221 WINDOW_RESIZING_BOTTOM = 2,
222 WINDOW_RESIZING_LEFT = 4,
223 WINDOW_RESIZING_TOP_LEFT = 5,
224 WINDOW_RESIZING_BOTTOM_LEFT = 6,
225 WINDOW_RESIZING_RIGHT = 8,
226 WINDOW_RESIZING_TOP_RIGHT = 9,
227 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
228 WINDOW_RESIZING_MASK = 15,
229 WINDOW_EXTERIOR = 16,
230 WINDOW_TITLEBAR = 17,
231 WINDOW_CLIENT_AREA = 18,
232};
233
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400234const char *option_xkb_layout = "us";
235const char *option_xkb_variant = "";
236const char *option_xkb_options = "";
237
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400238static const struct weston_option xkb_options[] = {
239 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
240 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
241 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400242};
243
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244static const cairo_user_data_key_t surface_data_key;
245struct surface_data {
246 struct wl_buffer *buffer;
247};
248
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500249#define MULT(_d,c,a,t) \
250 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
251
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500252#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400253
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100254struct egl_window_surface_data {
255 struct display *display;
256 struct wl_surface *surface;
257 struct wl_egl_window *window;
258 EGLSurface surf;
259};
260
261static void
262egl_window_surface_data_destroy(void *p)
263{
264 struct egl_window_surface_data *data = p;
265 struct display *d = data->display;
266
267 eglDestroySurface(d->dpy, data->surf);
268 wl_egl_window_destroy(data->window);
269 data->surface = NULL;
270
271 free(p);
272}
273
274static cairo_surface_t *
275display_create_egl_window_surface(struct display *display,
276 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278 struct rectangle *rectangle)
279{
280 cairo_surface_t *cairo_surface;
281 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400282 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200283 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100284
285 data = malloc(sizeof *data);
286 if (data == NULL)
287 return NULL;
288
289 data->display = display;
290 data->surface = surface;
291
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500292 config = display->argb_config;
293 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400295 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500300 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Benjamin Franzke0c991632011-09-27 21:57:31 +0200302 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100303 data->surf,
304 rectangle->width,
305 rectangle->height);
306
307 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
308 data, egl_window_surface_data_destroy);
309
310 return cairo_surface;
311}
312
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 EGLImageKHR image;
317 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800318 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320};
321
322static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500325 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200330 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800331
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500332 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500334 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336}
337
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339display_get_image_for_egl_image_surface(struct display *display,
340 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500343
344 data = cairo_surface_get_user_data (surface, &surface_data_key);
345
346 return data->image;
347}
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400351 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 EGLDisplay dpy = display->dpy;
356 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200357 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
359 data = malloc(sizeof *data);
360 if (data == NULL)
361 return NULL;
362
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800363 data->display = display;
364
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400365 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500367 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000368 free(data);
369 return NULL;
370 }
371
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500372 data->device = display->argb_device;
373 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400374
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500375 data->image = display->create_image(dpy, NULL,
376 EGL_NATIVE_PIXMAP_KHR,
377 (EGLClientBuffer) data->pixmap,
378 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500379 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500380 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 free(data);
382 return NULL;
383 }
384
385 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400386 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500387
Benjamin Franzke0c991632011-09-27 21:57:31 +0200388 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400389 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500391 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 surface = cairo_gl_surface_create_for_texture(data->device,
395 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396 data->texture,
397 rectangle->width,
398 rectangle->height);
399
400 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500401 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
403 return surface;
404}
405
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500406static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500407display_create_egl_image_surface_from_file(struct display *display,
408 const char *filename,
409 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410{
411 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400412 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500413 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400414
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400415 image = load_image(filename);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400416
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200417 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000418 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400419 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000420 return NULL;
421 }
422
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800423 data = cairo_surface_get_user_data(surface, &surface_data_key);
424
Benjamin Franzke0c991632011-09-27 21:57:31 +0200425 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800426 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800427 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400428 GL_RGBA, GL_UNSIGNED_BYTE,
429 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200430 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400432 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400433
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400434 return surface;
435}
436
437#endif
438
439struct wl_buffer *
440display_get_buffer_for_surface(struct display *display,
441 cairo_surface_t *surface)
442{
443 struct surface_data *data;
444
445 data = cairo_surface_get_user_data (surface, &surface_data_key);
446
447 return data->buffer;
448}
449
450struct shm_surface_data {
451 struct surface_data data;
452 void *map;
453 size_t length;
454};
455
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500456static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457shm_surface_data_destroy(void *p)
458{
459 struct shm_surface_data *data = p;
460
461 wl_buffer_destroy(data->data.buffer);
462 munmap(data->map, data->length);
463}
464
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500465static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400467 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468{
469 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400470 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400471 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800472 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 char filename[] = "/tmp/wayland-shm-XXXXXX";
474
475 data = malloc(sizeof *data);
476 if (data == NULL)
477 return NULL;
478
479 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
480 rectangle->width);
481 data->length = stride * rectangle->height;
482 fd = mkstemp(filename);
483 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000484 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400485 return NULL;
486 }
487 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000488 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489 close(fd);
490 return NULL;
491 }
492
493 data->map = mmap(NULL, data->length,
494 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
495 unlink(filename);
496
497 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000498 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499 close(fd);
500 return NULL;
501 }
502
503 surface = cairo_image_surface_create_for_data (data->map,
504 CAIRO_FORMAT_ARGB32,
505 rectangle->width,
506 rectangle->height,
507 stride);
508
509 cairo_surface_set_user_data (surface, &surface_data_key,
510 data, shm_surface_data_destroy);
511
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400512 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500513 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400514 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500515 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400516
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517 data->data.buffer = wl_shm_create_buffer(display->shm,
518 fd,
519 rectangle->width,
520 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400521 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522
523 close(fd);
524
525 return surface;
526}
527
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500528static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400529display_create_shm_surface_from_file(struct display *display,
530 const char *filename,
531 struct rectangle *rect)
532{
533 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400534 pixman_image_t *image;
535 void *dest;
536 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400538 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400539
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400540 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000541 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400542 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000543 return NULL;
544 }
545
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400546 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
547 dest = cairo_image_surface_get_data(surface);
548 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400550 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551
552 return surface;
553}
554
nobled7b87cb02011-02-01 18:51:47 +0000555static int
556check_size(struct rectangle *rect)
557{
558 if (rect->width && rect->height)
559 return 0;
560
561 fprintf(stderr, "tried to create surface of "
562 "width: %d, height: %d\n", rect->width, rect->height);
563 return -1;
564}
565
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400566cairo_surface_t *
567display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100568 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400569 struct rectangle *rectangle,
570 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400571{
nobled7b87cb02011-02-01 18:51:47 +0000572 if (check_size(rectangle) < 0)
573 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500574#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500575 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100576 if (surface)
577 return display_create_egl_window_surface(display,
578 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400579 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100580 rectangle);
581 else
582 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400583 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100584 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500585 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400586#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400587 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588}
589
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500590static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591display_create_surface_from_file(struct display *display,
592 const char *filename,
593 struct rectangle *rectangle)
594{
nobled7b87cb02011-02-01 18:51:47 +0000595 if (check_size(rectangle) < 0)
596 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500597#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500598 if (display->dpy) {
599 return display_create_egl_image_surface_from_file(display,
600 filename,
601 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500602 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500604 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400605}
Yuval Fledel45568f62010-12-06 09:18:12 -0500606 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400607 const char *filename;
608 int hotspot_x, hotspot_y;
609} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500610 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
611 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
612 { DATADIR "/weston/bottom_side.png", 16, 20 },
613 { DATADIR "/weston/grabbing.png", 20, 17 },
614 { DATADIR "/weston/left_ptr.png", 10, 5 },
615 { DATADIR "/weston/left_side.png", 10, 20 },
616 { DATADIR "/weston/right_side.png", 30, 19 },
617 { DATADIR "/weston/top_left_corner.png", 8, 8 },
618 { DATADIR "/weston/top_right_corner.png", 26, 8 },
619 { DATADIR "/weston/top_side.png", 18, 8 },
620 { DATADIR "/weston/xterm.png", 15, 15 },
621 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400622};
623
624static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400625create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400626{
627 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400628 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400629 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400630
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400632 display->pointer_surfaces =
633 malloc(count * sizeof *display->pointer_surfaces);
634 rect.width = width;
635 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400636 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400637 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638 display_create_surface_from_file(display,
639 pointer_images[i].filename,
640 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100641 if (!display->pointer_surfaces[i]) {
642 fprintf(stderr, "Error loading pointer image: %s\n",
643 pointer_images[i].filename);
644 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400645 }
646
647}
648
Pekka Paalanen325bb602011-12-19 10:31:45 +0200649static void
650destroy_pointer_surfaces(struct display *display)
651{
652 int i, count;
653
654 count = ARRAY_LENGTH(pointer_images);
655 for (i = 0; i < count; ++i) {
656 if (display->pointer_surfaces[i])
657 cairo_surface_destroy(display->pointer_surfaces[i]);
658 }
659 free(display->pointer_surfaces);
660}
661
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400662cairo_surface_t *
663display_get_pointer_surface(struct display *display, int pointer,
664 int *width, int *height,
665 int *hotspot_x, int *hotspot_y)
666{
667 cairo_surface_t *surface;
668
669 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500670#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400671 *width = cairo_gl_surface_get_width(surface);
672 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000673#else
674 *width = cairo_image_surface_get_width(surface);
675 *height = cairo_image_surface_get_height(surface);
676#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400677 *hotspot_x = pointer_images[pointer].hotspot_x;
678 *hotspot_y = pointer_images[pointer].hotspot_y;
679
680 return cairo_surface_reference(surface);
681}
682
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400683static void
684window_attach_surface(struct window *window);
685
686static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400687free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400688{
689 struct window *window = data;
690
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400691 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400692 cairo_surface_destroy(window->pending_surface);
693 window->pending_surface = NULL;
694 if (window->cairo_surface)
695 window_attach_surface(window);
696}
697
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400698static const struct wl_callback_listener free_surface_listener = {
699 free_surface
700};
701
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500702static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200703window_get_resize_dx_dy(struct window *window, int *x, int *y)
704{
705 if (window->resize_edges & WINDOW_RESIZING_LEFT)
706 *x = window->server_allocation.width - window->allocation.width;
707 else
708 *x = 0;
709
710 if (window->resize_edges & WINDOW_RESIZING_TOP)
711 *y = window->server_allocation.height -
712 window->allocation.height;
713 else
714 *y = 0;
715
716 window->resize_edges = 0;
717}
718
719static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500720window_attach_surface(struct window *window)
721{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400722 struct display *display = window->display;
723 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400724 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000725#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100726 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000727#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500728 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100729
730 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000731#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100732 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
733 data = cairo_surface_get_user_data(window->cairo_surface,
734 &surface_data_key);
735
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100736 cairo_gl_surface_swapbuffers(window->cairo_surface);
737 wl_egl_window_get_attached_size(data->window,
738 &window->server_allocation.width,
739 &window->server_allocation.height);
740 break;
741 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000742#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100743 case WINDOW_BUFFER_TYPE_SHM:
744 if (window->pending_surface != NULL)
745 return;
746
747 window->pending_surface = window->cairo_surface;
748 window->cairo_surface = NULL;
749
750 buffer =
751 display_get_buffer_for_surface(display,
752 window->pending_surface);
753
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200754 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755 wl_surface_attach(window->surface, buffer, x, y);
756 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757 cb = wl_display_sync(display->display);
758 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100759 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000760 default:
761 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100762 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500763
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500764 if (window->input_region) {
765 wl_surface_set_input_region(window->surface,
766 window->input_region);
767 wl_region_destroy(window->input_region);
768 window->input_region = NULL;
769 }
770
771 if (window->opaque_region) {
772 wl_surface_set_opaque_region(window->surface,
773 window->opaque_region);
774 wl_region_destroy(window->opaque_region);
775 window->opaque_region = NULL;
776 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500777
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500778 wl_surface_damage(window->surface, 0, 0,
779 window->allocation.width,
780 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500781}
782
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500783void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400784window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500785{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100786 if (window->cairo_surface) {
787 switch (window->buffer_type) {
788 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
789 case WINDOW_BUFFER_TYPE_SHM:
790 display_surface_damage(window->display,
791 window->cairo_surface,
792 0, 0,
793 window->allocation.width,
794 window->allocation.height);
795 break;
796 default:
797 break;
798 }
799 window_attach_surface(window);
800 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500801}
802
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400803void
804window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400805{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500806 cairo_surface_reference(surface);
807
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400808 if (window->cairo_surface != NULL)
809 cairo_surface_destroy(window->cairo_surface);
810
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500811 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400812}
813
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815static void
816window_resize_cairo_window_surface(struct window *window)
817{
818 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200819 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820
821 data = cairo_surface_get_user_data(window->cairo_surface,
822 &surface_data_key);
823
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200824 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 wl_egl_window_resize(data->window,
826 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200827 window->allocation.height,
828 x,y);
829
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 cairo_gl_surface_set_size(window->cairo_surface,
831 window->allocation.width,
832 window->allocation.height);
833}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400836struct display *
837window_get_display(struct window *window)
838{
839 return window->display;
840}
841
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400842void
843window_create_surface(struct window *window)
844{
845 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400846 uint32_t flags = 0;
847
848 if (!window->transparent)
849 flags = SURFACE_OPAQUE;
850
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400851 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500852#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
854 if (window->cairo_surface) {
855 window_resize_cairo_window_surface(window);
856 return;
857 }
858 surface = display_create_surface(window->display,
859 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400860 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500862 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400863 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400865 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866 break;
867#endif
868 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400869 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400870 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400871 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800872 default:
873 surface = NULL;
874 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400876
877 window_set_surface(window, surface);
878 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879}
880
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200881static void frame_destroy(struct frame *frame);
882
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400883void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500884window_destroy(struct window *window)
885{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200886 struct display *display = window->display;
887 struct input *input;
888
889 if (window->redraw_scheduled)
890 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500891 if (window->resize_scheduled)
892 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200893
894 wl_list_for_each(input, &display->input_list, link) {
895 if (input->pointer_focus == window)
896 input->pointer_focus = NULL;
897 if (input->keyboard_focus == window)
898 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500899 if (input->focus_widget &&
900 input->focus_widget->window == window)
901 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200902 }
903
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500904 if (window->input_region)
905 wl_region_destroy(window->input_region);
906 if (window->opaque_region)
907 wl_region_destroy(window->opaque_region);
908
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200909 if (window->frame)
910 frame_destroy(window->frame);
911
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200912 if (window->shell_surface)
913 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500914 wl_surface_destroy(window->surface);
915 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200916
917 if (window->cairo_surface != NULL)
918 cairo_surface_destroy(window->cairo_surface);
919 if (window->pending_surface != NULL)
920 cairo_surface_destroy(window->pending_surface);
921
922 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500923 free(window);
924}
925
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500926static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500927widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400928{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500929 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400930
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500931 wl_list_for_each(child, &widget->child_list, link) {
932 target = widget_find_widget(child, x, y);
933 if (target)
934 return target;
935 }
936
937 if (widget->allocation.x <= x &&
938 x < widget->allocation.x + widget->allocation.width &&
939 widget->allocation.y <= y &&
940 y < widget->allocation.y + widget->allocation.height) {
941 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942 }
943
944 return NULL;
945}
946
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500947static struct widget *
948widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400949{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500950 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400951
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500952 widget = malloc(sizeof *widget);
953 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500954 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500955 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500956 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500957 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500958 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500959
960 return widget;
961}
962
963struct widget *
964window_add_widget(struct window *window, void *data)
965{
966 window->widget = widget_create(window, data);
967 wl_list_init(&window->widget->link);
968
969 return window->widget;
970}
971
972struct widget *
973widget_add_widget(struct widget *parent, void *data)
974{
975 struct widget *widget;
976
977 widget = widget_create(parent->window, data);
978 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400979
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500980 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400981}
982
983void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500984widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200986 struct display *display = widget->window->display;
987 struct input *input;
988
989 wl_list_for_each(input, &display->input_list, link) {
990 if (input->focus_widget == widget)
991 input->focus_widget = NULL;
992 }
993
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500994 wl_list_remove(&widget->link);
995 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996}
997
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001000{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001001 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002}
1003
1004void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001005widget_set_size(struct widget *widget, int32_t width, int32_t height)
1006{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001007 widget->allocation.width = width;
1008 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001009}
1010
1011void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012widget_set_allocation(struct widget *widget,
1013 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001015 widget->allocation.x = x;
1016 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001017 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001018}
1019
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001020void
1021widget_set_transparent(struct widget *widget, int transparent)
1022{
1023 widget->opaque = !transparent;
1024}
1025
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001030}
1031
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001032void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001033widget_set_resize_handler(struct widget *widget,
1034 widget_resize_handler_t handler)
1035{
1036 widget->resize_handler = handler;
1037}
1038
1039void
1040widget_set_redraw_handler(struct widget *widget,
1041 widget_redraw_handler_t handler)
1042{
1043 widget->redraw_handler = handler;
1044}
1045
1046void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001047widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001048{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001049 widget->enter_handler = handler;
1050}
1051
1052void
1053widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1054{
1055 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001056}
1057
1058void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001059widget_set_motion_handler(struct widget *widget,
1060 widget_motion_handler_t handler)
1061{
1062 widget->motion_handler = handler;
1063}
1064
1065void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001066widget_set_button_handler(struct widget *widget,
1067 widget_button_handler_t handler)
1068{
1069 widget->button_handler = handler;
1070}
1071
1072void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001073widget_schedule_redraw(struct widget *widget)
1074{
1075 window_schedule_redraw(widget->window);
1076}
1077
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001078cairo_surface_t *
1079window_get_surface(struct window *window)
1080{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001081 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001082}
1083
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001084struct wl_surface *
1085window_get_wl_surface(struct window *window)
1086{
1087 return window->surface;
1088}
1089
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001090struct wl_shell_surface *
1091window_get_wl_shell_surface(struct window *window)
1092{
1093 return window->shell_surface;
1094}
1095
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001096static void
1097frame_resize_handler(struct widget *widget,
1098 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001099{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001100 struct frame *frame = data;
1101 struct widget *child = frame->child;
1102 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001103 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001104 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001105 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001106
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001107 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001108 decoration_width = (frame->width + frame->margin) * 2;
1109 decoration_height = frame->width +
1110 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001111
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001112 allocation.x = frame->width + frame->margin;
1113 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001114 allocation.width = width - decoration_width;
1115 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001116
1117 widget->window->input_region =
1118 wl_compositor_create_region(display->compositor);
1119 wl_region_add(widget->window->input_region,
1120 frame->margin, frame->margin,
1121 width - 2 * frame->margin,
1122 height - 2 * frame->margin);
1123
1124 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001125 } else {
1126 decoration_width = 0;
1127 decoration_height = 0;
1128
1129 allocation.x = 0;
1130 allocation.y = 0;
1131 allocation.width = width;
1132 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001133 opaque_margin = 0;
1134 }
1135
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001136 widget_set_allocation(child, allocation.x, allocation.y,
1137 allocation.width, allocation.height);
1138
1139 if (child->resize_handler)
1140 child->resize_handler(child,
1141 allocation.width,
1142 allocation.height,
1143 child->user_data);
1144
1145 widget_set_allocation(widget, 0, 0,
1146 child->allocation.width + decoration_width,
1147 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001148
1149 if (child->opaque) {
1150 widget->window->opaque_region =
1151 wl_compositor_create_region(display->compositor);
1152 wl_region_add(widget->window->opaque_region,
1153 opaque_margin, opaque_margin,
1154 widget->allocation.width - 2 * opaque_margin,
1155 widget->allocation.height - 2 * opaque_margin);
1156 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001157}
1158
1159static void
1160frame_redraw_handler(struct widget *widget, void *data)
1161{
1162 struct frame *frame = data;
1163 cairo_t *cr;
1164 cairo_text_extents_t extents;
1165 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001166 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167 struct window *window = widget->window;
1168
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001169 if (window->type == TYPE_FULLSCREEN)
1170 return;
1171
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172 width = widget->allocation.width;
1173 height = widget->allocation.height;
1174
1175 cr = cairo_create(window->cairo_surface);
1176
1177 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1178 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1179 cairo_paint(cr);
1180
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001181 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001182 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001183 2, 2, width + 8, height + 8,
1184 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001185
1186 if (window->keyboard_device)
1187 source = window->display->active_frame;
1188 else
1189 source = window->display->inactive_frame;
1190
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001191 tile_source(cr, source,
1192 frame->margin, frame->margin,
1193 width - frame->margin * 2, height - frame->margin * 2,
1194 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001195
1196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001197 cairo_select_font_face(cr, "sans",
1198 CAIRO_FONT_SLANT_NORMAL,
1199 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001200 cairo_set_font_size(cr, 14);
1201 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001202 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001203 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001204 if (window->keyboard_device) {
1205 cairo_move_to(cr, x + 1, y + 1);
1206 cairo_set_source_rgb(cr, 1, 1, 1);
1207 cairo_show_text(cr, window->title);
1208 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001209 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001210 cairo_show_text(cr, window->title);
1211 } else {
1212 cairo_move_to(cr, x, y);
1213 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1214 cairo_show_text(cr, window->title);
1215 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001216
1217 cairo_destroy(cr);
1218}
1219
1220static int
1221frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1222{
1223 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001224 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 const int grip_size = 8;
1226
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001227 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001228 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001229 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001230 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001231 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001232 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234 hlocation = WINDOW_RESIZING_RIGHT;
1235 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001237
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001238 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001240 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001241 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001242 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001243 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245 vlocation = WINDOW_RESIZING_BOTTOM;
1246 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001249 location = vlocation | hlocation;
1250 if (location & WINDOW_EXTERIOR)
1251 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 location = WINDOW_TITLEBAR;
1254 else if (location == WINDOW_INTERIOR)
1255 location = WINDOW_CLIENT_AREA;
1256
1257 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258}
1259
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001260static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001261frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 switch (location) {
1267 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001268 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001270 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001272 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001274 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001276 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001277 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001278 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001280 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001281 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001283 case WINDOW_EXTERIOR:
1284 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001288}
1289
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290static void
1291frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001293 switch (index) {
1294 case 0: /* close */
1295 if (window->close_handler)
1296 window->close_handler(window->parent,
1297 window->user_data);
1298 else
1299 display_exit(window->display);
1300 break;
1301 case 1: /* fullscreen */
1302 /* we don't have a way to get out of fullscreen for now */
1303 window_set_fullscreen(window, 1);
1304 break;
1305 case 2: /* rotate */
1306 case 3: /* scale */
1307 break;
1308 }
1309}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001311void
1312window_show_frame_menu(struct window *window,
1313 struct input *input, uint32_t time)
1314{
1315 int32_t x, y;
1316
1317 static const char *entries[] = {
1318 "Close", "Fullscreen", "Rotate", "Scale"
1319 };
1320
1321 input_get_position(input, &x, &y);
1322 window_show_menu(window->display, input, time, window,
1323 x - 10, y - 10, frame_menu_func, entries, 4);
1324}
1325
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001326static int
1327frame_enter_handler(struct widget *widget,
1328 struct input *input, uint32_t time,
1329 int32_t x, int32_t y, void *data)
1330{
1331 return frame_get_pointer_image_for_location(data, input);
1332}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001333
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001334static int
1335frame_motion_handler(struct widget *widget,
1336 struct input *input, uint32_t time,
1337 int32_t x, int32_t y, void *data)
1338{
1339 return frame_get_pointer_image_for_location(data, input);
1340}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001341
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001342static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001343frame_button_handler(struct widget *widget,
1344 struct input *input, uint32_t time,
1345 int button, int state, void *data)
1346
1347{
1348 struct frame *frame = data;
1349 struct window *window = widget->window;
1350 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001351 location = frame_get_pointer_location(frame, input->sx, input->sy);
1352
1353 if (window->display->shell && button == BTN_LEFT && state == 1) {
1354 switch (location) {
1355 case WINDOW_TITLEBAR:
1356 if (!window->shell_surface)
1357 break;
1358 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001359 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001360 wl_shell_surface_move(window->shell_surface,
1361 input_get_input_device(input),
1362 time);
1363 break;
1364 case WINDOW_RESIZING_TOP:
1365 case WINDOW_RESIZING_BOTTOM:
1366 case WINDOW_RESIZING_LEFT:
1367 case WINDOW_RESIZING_RIGHT:
1368 case WINDOW_RESIZING_TOP_LEFT:
1369 case WINDOW_RESIZING_TOP_RIGHT:
1370 case WINDOW_RESIZING_BOTTOM_LEFT:
1371 case WINDOW_RESIZING_BOTTOM_RIGHT:
1372 if (!window->shell_surface)
1373 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001374 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375 wl_shell_surface_resize(window->shell_surface,
1376 input_get_input_device(input),
1377 time, location);
1378 break;
1379 }
1380 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001381 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001382 }
1383}
1384
1385struct widget *
1386frame_create(struct window *window, void *data)
1387{
1388 struct frame *frame;
1389
1390 frame = malloc(sizeof *frame);
1391 memset(frame, 0, sizeof *frame);
1392
1393 frame->widget = window_add_widget(window, frame);
1394 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001395 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001396 frame->width = 4;
1397 frame->titlebar_height = 30
1398;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001399 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1400 widget_set_resize_handler(frame->widget, frame_resize_handler);
1401 widget_set_enter_handler(frame->widget, frame_enter_handler);
1402 widget_set_motion_handler(frame->widget, frame_motion_handler);
1403 widget_set_button_handler(frame->widget, frame_button_handler);
1404
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001405 window->frame = frame;
1406
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001407 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001408}
1409
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001410static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001411frame_destroy(struct frame *frame)
1412{
1413 /* frame->child must be destroyed by the application */
1414 widget_destroy(frame->widget);
1415 free(frame);
1416}
1417
1418static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001419input_set_focus_widget(struct input *input, struct widget *focus,
1420 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001421{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001422 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001423 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001424
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001425 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001426 return;
1427
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001428 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001429 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001430 widget = old;
1431 if (input->grab)
1432 widget = input->grab;
1433 if (widget->leave_handler)
1434 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001435 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001436 }
1437
1438 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001439 widget = focus;
1440 if (input->grab)
1441 widget = input->grab;
1442 if (widget->enter_handler)
1443 pointer = widget->enter_handler(focus, input, time,
1444 x, y,
1445 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001446 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001447
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001448 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001449 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001450}
1451
1452static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001453input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001454 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001455{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001456 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001457 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001458 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001459 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001460
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001461 input->sx = sx;
1462 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001463
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001464 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001465 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001466 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001467 }
1468
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001469 if (input->grab)
1470 widget = input->grab;
1471 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001473 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001474 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001475 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001476 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001477
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001478 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001479}
1480
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001481void
1482input_grab(struct input *input, struct widget *widget, uint32_t button)
1483{
1484 input->grab = widget;
1485 input->grab_button = button;
1486}
1487
1488void
1489input_ungrab(struct input *input, uint32_t time)
1490{
1491 struct widget *widget;
1492
1493 input->grab = NULL;
1494 if (input->pointer_focus) {
1495 widget = widget_find_widget(input->pointer_focus->widget,
1496 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001497 input_set_focus_widget(input, widget,
1498 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001499 }
1500}
1501
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001502static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001503input_handle_button(void *data,
1504 struct wl_input_device *input_device,
1505 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001506{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001507 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001508 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001509
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001510 if (input->focus_widget && input->grab == NULL && state)
1511 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001512
Neil Roberts6b28aad2012-01-23 19:11:18 +00001513 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001514 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001515 (*widget->button_handler)(widget,
1516 input, time,
1517 button, state,
1518 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001519
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001520 if (input->grab && input->grab_button == button && !state)
1521 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001522}
1523
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001524static void
Scott Moreau210d0792012-03-22 10:47:01 -06001525input_handle_axis(void *data,
1526 struct wl_input_device *input_device,
1527 uint32_t time, uint32_t axis, int32_t value)
1528{
1529}
1530
1531static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001532input_handle_key(void *data, struct wl_input_device *input_device,
1533 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001534{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001535 struct input *input = data;
1536 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001537 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001538 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001539
Daniel Stone0d5a5092012-02-16 12:48:00 +00001540 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001541 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001542 return;
1543
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001544 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001545 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001546 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1547 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001548
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001549 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1550
1551 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001552 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001553 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001554 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001555
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001556 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1557 if (state)
1558 window_set_maximized(window,
1559 window->type != TYPE_MAXIMIZED);
1560 } else if (window->key_handler) {
1561 (*window->key_handler)(window, input, time, key,
1562 sym, state, window->user_data);
1563 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001564}
1565
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001566static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001567input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001568{
1569 struct window *window = input->pointer_focus;
1570
1571 if (!window)
1572 return;
1573
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001574 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001575
Pekka Paalanene1207c72011-12-16 12:02:09 +02001576 input->pointer_focus = NULL;
1577 input->current_pointer_image = POINTER_UNSET;
1578}
1579
1580static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001581input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001582 struct wl_input_device *input_device,
1583 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001584 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001585{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001586 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001587 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001588 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001589
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001590 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001591 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001592
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001593 input->sx = sx;
1594 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001595
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001596 widget = widget_find_widget(window->widget, sx, sy);
1597 input_set_focus_widget(input, widget, time, sx, sy);
1598}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001599
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001600static void
1601input_handle_pointer_leave(void *data,
1602 struct wl_input_device *input_device,
1603 uint32_t time, struct wl_surface *surface)
1604{
1605 struct input *input = data;
1606
1607 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001608}
1609
1610static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001611input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001612{
1613 struct window *window = input->keyboard_focus;
1614
1615 if (!window)
1616 return;
1617
1618 window->keyboard_device = NULL;
1619 if (window->keyboard_focus_handler)
1620 (*window->keyboard_focus_handler)(window, NULL,
1621 window->user_data);
1622
1623 input->keyboard_focus = NULL;
1624}
1625
1626static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001627input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001628 struct wl_input_device *input_device,
1629 uint32_t time,
1630 struct wl_surface *surface,
1631 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001632{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001633 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001634 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001635 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001636 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001637
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001638 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001639
1640 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001641 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001642 for (k = keys->data; k < end; k++)
1643 input->modifiers |= d->xkb->map->modmap[*k];
1644
1645 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001646 window->keyboard_device = input;
1647 if (window->keyboard_focus_handler)
1648 (*window->keyboard_focus_handler)(window,
1649 window->keyboard_device,
1650 window->user_data);
1651}
1652
1653static void
1654input_handle_keyboard_leave(void *data,
1655 struct wl_input_device *input_device,
1656 uint32_t time,
1657 struct wl_surface *surface)
1658{
1659 struct input *input = data;
1660
1661 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001662}
1663
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001664static void
1665input_handle_touch_down(void *data,
1666 struct wl_input_device *wl_input_device,
1667 uint32_t time, struct wl_surface *surface,
1668 int32_t id, int32_t x, int32_t y)
1669{
1670}
1671
1672static void
1673input_handle_touch_up(void *data,
1674 struct wl_input_device *wl_input_device,
1675 uint32_t time, int32_t id)
1676{
1677}
1678
1679static void
1680input_handle_touch_motion(void *data,
1681 struct wl_input_device *wl_input_device,
1682 uint32_t time, int32_t id, int32_t x, int32_t y)
1683{
1684}
1685
1686static void
1687input_handle_touch_frame(void *data,
1688 struct wl_input_device *wl_input_device)
1689{
1690}
1691
1692static void
1693input_handle_touch_cancel(void *data,
1694 struct wl_input_device *wl_input_device)
1695{
1696}
1697
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001698static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001699 input_handle_motion,
1700 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001701 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001702 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001703 input_handle_pointer_enter,
1704 input_handle_pointer_leave,
1705 input_handle_keyboard_enter,
1706 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001707 input_handle_touch_down,
1708 input_handle_touch_up,
1709 input_handle_touch_motion,
1710 input_handle_touch_frame,
1711 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001712};
1713
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001714void
1715input_get_position(struct input *input, int32_t *x, int32_t *y)
1716{
1717 *x = input->sx;
1718 *y = input->sy;
1719}
1720
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001721struct wl_input_device *
1722input_get_input_device(struct input *input)
1723{
1724 return input->input_device;
1725}
1726
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001727uint32_t
1728input_get_modifiers(struct input *input)
1729{
1730 return input->modifiers;
1731}
1732
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733struct widget *
1734input_get_focus_widget(struct input *input)
1735{
1736 return input->focus_widget;
1737}
1738
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001739struct data_offer {
1740 struct wl_data_offer *offer;
1741 struct input *input;
1742 struct wl_array types;
1743 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001744
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001745 struct task io_task;
1746 int fd;
1747 data_func_t func;
1748 int32_t x, y;
1749 void *user_data;
1750};
1751
1752static void
1753data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1754{
1755 struct data_offer *offer = data;
1756 char **p;
1757
1758 p = wl_array_add(&offer->types, sizeof *p);
1759 *p = strdup(type);
1760}
1761
1762static const struct wl_data_offer_listener data_offer_listener = {
1763 data_offer_offer,
1764};
1765
1766static void
1767data_offer_destroy(struct data_offer *offer)
1768{
1769 char **p;
1770
1771 offer->refcount--;
1772 if (offer->refcount == 0) {
1773 wl_data_offer_destroy(offer->offer);
1774 for (p = offer->types.data; *p; p++)
1775 free(*p);
1776 wl_array_release(&offer->types);
1777 free(offer);
1778 }
1779}
1780
1781static void
1782data_device_data_offer(void *data,
1783 struct wl_data_device *data_device, uint32_t id)
1784{
1785 struct data_offer *offer;
1786
1787 offer = malloc(sizeof *offer);
1788
1789 wl_array_init(&offer->types);
1790 offer->refcount = 1;
1791 offer->input = data;
1792
1793 /* FIXME: Generate typesafe wrappers for this */
1794 offer->offer = (struct wl_data_offer *)
1795 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1796 id, &wl_data_offer_interface);
1797
1798 wl_data_offer_add_listener(offer->offer,
1799 &data_offer_listener, offer);
1800}
1801
1802static void
1803data_device_enter(void *data, struct wl_data_device *data_device,
1804 uint32_t time, struct wl_surface *surface,
1805 int32_t x, int32_t y, struct wl_data_offer *offer)
1806{
1807 struct input *input = data;
1808 struct window *window;
1809 char **p;
1810
1811 input->drag_offer = wl_data_offer_get_user_data(offer);
1812 window = wl_surface_get_user_data(surface);
1813 input->pointer_focus = window;
1814
1815 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1816 *p = NULL;
1817
1818 window = input->pointer_focus;
1819 if (window->data_handler)
1820 window->data_handler(window, input, time, x, y,
1821 input->drag_offer->types.data,
1822 window->user_data);
1823}
1824
1825static void
1826data_device_leave(void *data, struct wl_data_device *data_device)
1827{
1828 struct input *input = data;
1829
1830 data_offer_destroy(input->drag_offer);
1831 input->drag_offer = NULL;
1832}
1833
1834static void
1835data_device_motion(void *data, struct wl_data_device *data_device,
1836 uint32_t time, int32_t x, int32_t y)
1837{
1838 struct input *input = data;
1839 struct window *window = input->pointer_focus;
1840
1841 input->sx = x;
1842 input->sy = y;
1843
1844 if (window->data_handler)
1845 window->data_handler(window, input, time, x, y,
1846 input->drag_offer->types.data,
1847 window->user_data);
1848}
1849
1850static void
1851data_device_drop(void *data, struct wl_data_device *data_device)
1852{
1853 struct input *input = data;
1854 struct window *window = input->pointer_focus;
1855
1856 if (window->drop_handler)
1857 window->drop_handler(window, input,
1858 input->sx, input->sy, window->user_data);
1859}
1860
1861static void
1862data_device_selection(void *data,
1863 struct wl_data_device *wl_data_device,
1864 struct wl_data_offer *offer)
1865{
1866 struct input *input = data;
1867 char **p;
1868
1869 if (input->selection_offer)
1870 data_offer_destroy(input->selection_offer);
1871
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001872 if (offer) {
1873 input->selection_offer = wl_data_offer_get_user_data(offer);
1874 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1875 *p = NULL;
1876 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001877}
1878
1879static const struct wl_data_device_listener data_device_listener = {
1880 data_device_data_offer,
1881 data_device_enter,
1882 data_device_leave,
1883 data_device_motion,
1884 data_device_drop,
1885 data_device_selection
1886};
1887
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001888void
1889input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1890{
1891 struct display *display = input->display;
1892 struct wl_buffer *buffer;
1893 cairo_surface_t *surface;
1894
1895 if (pointer == input->current_pointer_image)
1896 return;
1897
1898 input->current_pointer_image = pointer;
1899 surface = display->pointer_surfaces[pointer];
1900
1901 if (!surface)
1902 return;
1903
1904 buffer = display_get_buffer_for_surface(display, surface);
1905 wl_input_device_attach(input->input_device, time, buffer,
1906 pointer_images[pointer].hotspot_x,
1907 pointer_images[pointer].hotspot_y);
1908}
1909
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001910struct wl_data_device *
1911input_get_data_device(struct input *input)
1912{
1913 return input->data_device;
1914}
1915
1916void
1917input_set_selection(struct input *input,
1918 struct wl_data_source *source, uint32_t time)
1919{
1920 wl_data_device_set_selection(input->data_device, source, time);
1921}
1922
1923void
1924input_accept(struct input *input, uint32_t time, const char *type)
1925{
1926 wl_data_offer_accept(input->drag_offer->offer, time, type);
1927}
1928
1929static void
1930offer_io_func(struct task *task, uint32_t events)
1931{
1932 struct data_offer *offer =
1933 container_of(task, struct data_offer, io_task);
1934 unsigned int len;
1935 char buffer[4096];
1936
1937 len = read(offer->fd, buffer, sizeof buffer);
1938 offer->func(buffer, len,
1939 offer->x, offer->y, offer->user_data);
1940
1941 if (len == 0) {
1942 close(offer->fd);
1943 data_offer_destroy(offer);
1944 }
1945}
1946
1947static void
1948data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1949 data_func_t func, void *user_data)
1950{
1951 int p[2];
1952
1953 pipe2(p, O_CLOEXEC);
1954 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1955 close(p[1]);
1956
1957 offer->io_task.run = offer_io_func;
1958 offer->fd = p[0];
1959 offer->func = func;
1960 offer->refcount++;
1961 offer->user_data = user_data;
1962
1963 display_watch_fd(offer->input->display,
1964 offer->fd, EPOLLIN, &offer->io_task);
1965}
1966
1967void
1968input_receive_drag_data(struct input *input, const char *mime_type,
1969 data_func_t func, void *data)
1970{
1971 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1972 input->drag_offer->x = input->sx;
1973 input->drag_offer->y = input->sy;
1974}
1975
1976int
1977input_receive_selection_data(struct input *input, const char *mime_type,
1978 data_func_t func, void *data)
1979{
1980 char **p;
1981
1982 if (input->selection_offer == NULL)
1983 return -1;
1984
1985 for (p = input->selection_offer->types.data; *p; p++)
1986 if (strcmp(mime_type, *p) == 0)
1987 break;
1988
1989 if (*p == NULL)
1990 return -1;
1991
1992 data_offer_receive_data(input->selection_offer,
1993 mime_type, func, data);
1994 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001995}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001996
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001997int
1998input_receive_selection_data_to_fd(struct input *input,
1999 const char *mime_type, int fd)
2000{
2001 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2002
2003 return 0;
2004}
2005
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002006void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002007window_move(struct window *window, struct input *input, uint32_t time)
2008{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002009 if (!window->shell_surface)
2010 return;
2011
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002012 wl_shell_surface_move(window->shell_surface,
2013 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002014}
2015
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002016static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002017idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002018{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002019 struct window *window =
2020 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002021 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002022
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002023 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002024 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002025 widget_set_allocation(widget,
2026 window->pending_allocation.x,
2027 window->pending_allocation.y,
2028 window->pending_allocation.width,
2029 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002030
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002031 if (window->input_region) {
2032 wl_region_destroy(window->input_region);
2033 window->input_region = NULL;
2034 }
2035
2036 if (window->opaque_region) {
2037 wl_region_destroy(window->opaque_region);
2038 window->opaque_region = NULL;
2039 }
2040
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002041 if (widget->resize_handler)
2042 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002043 widget->allocation.width,
2044 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002045 widget->user_data);
2046
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002047 if (window->allocation.width != widget->allocation.width ||
2048 window->allocation.height != widget->allocation.height) {
2049 window->allocation = widget->allocation;
2050 window_schedule_redraw(window);
2051 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002052}
2053
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002054void
2055window_schedule_resize(struct window *window, int width, int height)
2056{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002057 window->pending_allocation.x = 0;
2058 window->pending_allocation.y = 0;
2059 window->pending_allocation.width = width;
2060 window->pending_allocation.height = height;
2061
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002062 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002063 window->resize_task.run = idle_resize;
2064 display_defer(window->display, &window->resize_task);
2065 window->resize_scheduled = 1;
2066 }
2067}
2068
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002069void
2070widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2071{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002072 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002073}
2074
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002075static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002076handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002077 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002078 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002079{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002080 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002081
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002082 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002083 return;
2084
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002085 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002086 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002087}
2088
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002090menu_destroy(struct menu *menu)
2091{
2092 widget_destroy(menu->widget);
2093 window_destroy(menu->window);
2094 free(menu);
2095}
2096
2097static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002098handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2099{
2100 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002101 struct menu *menu = window->widget->user_data;
2102
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002103 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002104 * device. Or just use wl_callback. And this really needs to
2105 * be a window vfunc that the menu can set. And we need the
2106 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002107
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002108 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002109 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002110 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002111}
2112
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002114 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002115 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002116};
2117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002118void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002119window_get_allocation(struct window *window,
2120 struct rectangle *allocation)
2121{
2122 *allocation = window->allocation;
2123}
2124
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002125static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002126widget_redraw(struct widget *widget)
2127{
2128 struct widget *child;
2129
2130 if (widget->redraw_handler)
2131 widget->redraw_handler(widget, widget->user_data);
2132 wl_list_for_each(child, &widget->child_list, link)
2133 widget_redraw(child);
2134}
2135
2136static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002137frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2138{
2139 struct window *window = data;
2140
2141 wl_callback_destroy(callback);
2142 window->redraw_scheduled = 0;
2143 if (window->redraw_needed)
2144 window_schedule_redraw(window);
2145}
2146
2147static const struct wl_callback_listener listener = {
2148 frame_callback
2149};
2150
2151static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002152idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002153{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002154 struct window *window =
2155 container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002156 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002157
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002158 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002159 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002160 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002161 window->redraw_needed = 0;
2162
2163 callback = wl_surface_frame(window->surface);
2164 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002165}
2166
2167void
2168window_schedule_redraw(struct window *window)
2169{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002170 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002171 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002172 window->redraw_task.run = idle_redraw;
2173 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002174 window->redraw_scheduled = 1;
2175 }
2176}
2177
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002178void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002179window_set_custom(struct window *window)
2180{
2181 window->type = TYPE_CUSTOM;
2182}
2183
2184void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002185window_set_fullscreen(struct window *window, int fullscreen)
2186{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002187 if (!window->display->shell)
2188 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002189
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002190 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002191 return;
2192
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002193 if (fullscreen) {
2194 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002195 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002196 wl_shell_surface_set_fullscreen(window->shell_surface,
2197 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2198 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002199 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002200 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002201 wl_shell_surface_set_toplevel(window->shell_surface);
2202 window_schedule_resize(window,
2203 window->saved_allocation.width,
2204 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002205 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002206}
2207
2208void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002209window_set_maximized(struct window *window, int maximized)
2210{
2211 if (!window->display->shell)
2212 return;
2213
2214 if ((window->type == TYPE_MAXIMIZED) == maximized)
2215 return;
2216
2217 if (window->type == TYPE_TOPLEVEL) {
2218 window->saved_allocation = window->allocation;
2219 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2220 window->type = TYPE_MAXIMIZED;
2221 } else {
2222 wl_shell_surface_set_toplevel(window->shell_surface);
2223 window->type = TYPE_TOPLEVEL;
2224 window_schedule_resize(window,
2225 window->saved_allocation.width,
2226 window->saved_allocation.height);
2227 }
2228}
2229
2230void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002231window_set_user_data(struct window *window, void *data)
2232{
2233 window->user_data = data;
2234}
2235
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002236void *
2237window_get_user_data(struct window *window)
2238{
2239 return window->user_data;
2240}
2241
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002242void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002243window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002244 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002245{
2246 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002247}
2248
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002249void
2250window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002251 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002252{
2253 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002254}
2255
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002256void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002257window_set_data_handler(struct window *window, window_data_handler_t handler)
2258{
2259 window->data_handler = handler;
2260}
2261
2262void
2263window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2264{
2265 window->drop_handler = handler;
2266}
2267
2268void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002269window_set_close_handler(struct window *window,
2270 window_close_handler_t handler)
2271{
2272 window->close_handler = handler;
2273}
2274
2275void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002276window_set_title(struct window *window, const char *title)
2277{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002278 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002279 window->title = strdup(title);
2280}
2281
2282const char *
2283window_get_title(struct window *window)
2284{
2285 return window->title;
2286}
2287
2288void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002289display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2290 int32_t x, int32_t y, int32_t width, int32_t height)
2291{
2292 struct wl_buffer *buffer;
2293
2294 buffer = display_get_buffer_for_surface(display, cairo_surface);
2295
2296 wl_buffer_damage(buffer, x, y, width, height);
2297}
2298
2299void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002300window_damage(struct window *window, int32_t x, int32_t y,
2301 int32_t width, int32_t height)
2302{
2303 wl_surface_damage(window->surface, x, y, width, height);
2304}
2305
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002306static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002307window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002308{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002309 struct window *window;
2310
2311 window = malloc(sizeof *window);
2312 if (window == NULL)
2313 return NULL;
2314
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002315 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002316 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002317 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002318 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002319 if (display->shell) {
2320 window->shell_surface =
2321 wl_shell_get_shell_surface(display->shell,
2322 window->surface);
2323 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002324 window->allocation.x = 0;
2325 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002326 window->allocation.width = 0;
2327 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002328 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002329 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002330 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002331 window->input_region = NULL;
2332 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002333
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002334 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002335#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002336 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2337 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002338#else
2339 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2340#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002341 else
2342 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002343
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002344 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002345 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002346
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002347 if (window->shell_surface) {
2348 wl_shell_surface_set_user_data(window->shell_surface, window);
2349 wl_shell_surface_add_listener(window->shell_surface,
2350 &shell_surface_listener, window);
2351 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002352
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002353 return window;
2354}
2355
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002356struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002357window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002358{
2359 struct window *window;
2360
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002361 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002362 if (!window)
2363 return NULL;
2364
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002365 window->type = TYPE_TOPLEVEL;
2366 if (display->shell)
2367 wl_shell_surface_set_toplevel(window->shell_surface);
2368
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002369 return window;
2370}
2371
2372struct window *
2373window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002374 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002375{
2376 struct window *window;
2377
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002378 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002379 if (!window)
2380 return NULL;
2381
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002382 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002383 window->x = x;
2384 window->y = y;
2385
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002386 if (display->shell)
2387 wl_shell_surface_set_transient(window->shell_surface,
2388 window->parent->shell_surface,
2389 window->x, window->y, 0);
2390
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002391 return window;
2392}
2393
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002394static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002395menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002396{
2397 int next;
2398
2399 next = (sy - 8) / 20;
2400 if (menu->current != next) {
2401 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002402 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002403 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002404}
2405
2406static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002407menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002408 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002409 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002410{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002411 struct menu *menu = data;
2412
2413 if (widget == menu->widget)
2414 menu_set_item(data, y);
2415
2416 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002417}
2418
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002419static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002420menu_enter_handler(struct widget *widget,
2421 struct input *input, uint32_t time,
2422 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002423{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002424 struct menu *menu = data;
2425
2426 if (widget == menu->widget)
2427 menu_set_item(data, y);
2428
2429 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002430}
2431
2432static void
2433menu_leave_handler(struct widget *widget, struct input *input, void *data)
2434{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002435 struct menu *menu = data;
2436
2437 if (widget == menu->widget)
2438 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002439}
2440
2441static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002442menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002443 struct input *input, uint32_t time,
2444 int button, int state, void *data)
2445
2446{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002447 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002449 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002450 /* Either relase after press-drag-release or
2451 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002452 menu->func(menu->window->parent,
2453 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002454 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002455 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002456 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002457}
2458
2459static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002460menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002461{
2462 cairo_t *cr;
2463 const int32_t r = 3, margin = 3;
2464 struct menu *menu = data;
2465 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002466 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002467
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002468 cr = cairo_create(window->cairo_surface);
2469 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2470 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2471 cairo_paint(cr);
2472
2473 width = window->allocation.width;
2474 height = window->allocation.height;
2475 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002476
2477 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002478 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2479 cairo_fill(cr);
2480
2481 for (i = 0; i < menu->count; i++) {
2482 if (i == menu->current) {
2483 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2484 cairo_rectangle(cr, margin, i * 20 + margin,
2485 width - 2 * margin, 20);
2486 cairo_fill(cr);
2487 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2488 cairo_move_to(cr, 10, i * 20 + 16);
2489 cairo_show_text(cr, menu->entries[i]);
2490 } else {
2491 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2492 cairo_move_to(cr, 10, i * 20 + 16);
2493 cairo_show_text(cr, menu->entries[i]);
2494 }
2495 }
2496
2497 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002498}
2499
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002500void
2501window_show_menu(struct display *display,
2502 struct input *input, uint32_t time, struct window *parent,
2503 int32_t x, int32_t y,
2504 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002505{
2506 struct window *window;
2507 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002508 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002509
2510 menu = malloc(sizeof *menu);
2511 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002512 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002513
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002514 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002515 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002516 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002517
2518 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002519 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002520 menu->entries = entries;
2521 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002522 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002523 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002524 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002526 window->type = TYPE_MENU;
2527 window->x = x;
2528 window->y = y;
2529
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002530 wl_shell_surface_set_popup(window->shell_surface,
2531 input->input_device, time,
2532 window->parent->shell_surface,
2533 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002534
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002535 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002536 widget_set_enter_handler(menu->widget, menu_enter_handler);
2537 widget_set_leave_handler(menu->widget, menu_leave_handler);
2538 widget_set_motion_handler(menu->widget, menu_motion_handler);
2539 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002540
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002541 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002542 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002543}
2544
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002545void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002546window_set_buffer_type(struct window *window, enum window_buffer_type type)
2547{
2548 window->buffer_type = type;
2549}
2550
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002551
2552static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002553display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002554 struct wl_output *wl_output,
2555 int x, int y,
2556 int physical_width,
2557 int physical_height,
2558 int subpixel,
2559 const char *make,
2560 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002561{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002562 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002563
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002564 output->allocation.x = x;
2565 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002566}
2567
2568static void
2569display_handle_mode(void *data,
2570 struct wl_output *wl_output,
2571 uint32_t flags,
2572 int width,
2573 int height,
2574 int refresh)
2575{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002576 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002577 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002578
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579 if (flags & WL_OUTPUT_MODE_CURRENT) {
2580 output->allocation.width = width;
2581 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002582 if (display->output_configure_handler)
2583 (*display->output_configure_handler)(
2584 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002585 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002586}
2587
2588static const struct wl_output_listener output_listener = {
2589 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002590 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002591};
2592
2593static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002594display_add_output(struct display *d, uint32_t id)
2595{
2596 struct output *output;
2597
2598 output = malloc(sizeof *output);
2599 if (output == NULL)
2600 return;
2601
2602 memset(output, 0, sizeof *output);
2603 output->display = d;
2604 output->output =
2605 wl_display_bind(d->display, id, &wl_output_interface);
2606 wl_list_insert(d->output_list.prev, &output->link);
2607
2608 wl_output_add_listener(output->output, &output_listener, output);
2609}
2610
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002611static void
2612output_destroy(struct output *output)
2613{
2614 if (output->destroy_handler)
2615 (*output->destroy_handler)(output, output->user_data);
2616
2617 wl_output_destroy(output->output);
2618 wl_list_remove(&output->link);
2619 free(output);
2620}
2621
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002622void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002623display_set_output_configure_handler(struct display *display,
2624 display_output_handler_t handler)
2625{
2626 struct output *output;
2627
2628 display->output_configure_handler = handler;
2629 if (!handler)
2630 return;
2631
2632 wl_list_for_each(output, &display->output_list, link)
2633 (*display->output_configure_handler)(output,
2634 display->user_data);
2635}
2636
2637void
2638output_set_user_data(struct output *output, void *data)
2639{
2640 output->user_data = data;
2641}
2642
2643void *
2644output_get_user_data(struct output *output)
2645{
2646 return output->user_data;
2647}
2648
2649void
2650output_set_destroy_handler(struct output *output,
2651 display_output_handler_t handler)
2652{
2653 output->destroy_handler = handler;
2654 /* FIXME: implement this, once we have way to remove outputs */
2655}
2656
2657void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002658output_get_allocation(struct output *output, struct rectangle *allocation)
2659{
2660 *allocation = output->allocation;
2661}
2662
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002663struct wl_output *
2664output_get_wl_output(struct output *output)
2665{
2666 return output->output;
2667}
2668
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002669static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002670display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002671{
2672 struct input *input;
2673
2674 input = malloc(sizeof *input);
2675 if (input == NULL)
2676 return;
2677
2678 memset(input, 0, sizeof *input);
2679 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002680 input->input_device =
2681 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682 input->pointer_focus = NULL;
2683 input->keyboard_focus = NULL;
2684 wl_list_insert(d->input_list.prev, &input->link);
2685
2686 wl_input_device_add_listener(input->input_device,
2687 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002688 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002689
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002690 input->data_device =
2691 wl_data_device_manager_get_data_device(d->data_device_manager,
2692 input->input_device);
2693 wl_data_device_add_listener(input->data_device,
2694 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002695}
2696
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002697static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002698input_destroy(struct input *input)
2699{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002700 input_remove_keyboard_focus(input);
2701 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002702
2703 if (input->drag_offer)
2704 data_offer_destroy(input->drag_offer);
2705
2706 if (input->selection_offer)
2707 data_offer_destroy(input->selection_offer);
2708
2709 wl_data_device_destroy(input->data_device);
2710 wl_list_remove(&input->link);
2711 wl_input_device_destroy(input->input_device);
2712 free(input);
2713}
2714
2715static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002716display_handle_global(struct wl_display *display, uint32_t id,
2717 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002718{
2719 struct display *d = data;
2720
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002721 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002722 d->compositor =
2723 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002724 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002725 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002726 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002727 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002728 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002729 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002730 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002731 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002732 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2733 d->data_device_manager =
2734 wl_display_bind(display, id,
2735 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002736 }
2737}
2738
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002739static void
2740display_render_frame(struct display *d)
2741{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002742 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002743 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002744
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002745 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002746 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2747 cr = cairo_create(d->shadow);
2748 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2749 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002750 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002751 cairo_fill(cr);
2752 cairo_destroy(cr);
2753 blur_surface(d->shadow, 64);
2754
2755 d->active_frame =
2756 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2757 cr = cairo_create(d->active_frame);
2758 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002759
2760 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2761 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2762 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2763 cairo_set_source(cr, pattern);
2764 cairo_pattern_destroy(pattern);
2765
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002766 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002767 cairo_fill(cr);
2768 cairo_destroy(cr);
2769
2770 d->inactive_frame =
2771 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2772 cr = cairo_create(d->inactive_frame);
2773 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002774 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002775 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002776 cairo_fill(cr);
2777 cairo_destroy(cr);
2778}
2779
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002780static void
2781init_xkb(struct display *d)
2782{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002783 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002784
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002785 names.rules = "evdev";
2786 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002787 names.layout = option_xkb_layout;
2788 names.variant = option_xkb_variant;
2789 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002790
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002791 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002792 if (!d->xkb) {
2793 fprintf(stderr, "Failed to compile keymap\n");
2794 exit(1);
2795 }
2796}
2797
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002798static void
2799fini_xkb(struct display *display)
2800{
2801 xkb_free_keymap(display->xkb);
2802}
2803
Yuval Fledel45568f62010-12-06 09:18:12 -05002804static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002805init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002806{
2807 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002808 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002809
Rob Clark6396ed32012-03-11 19:48:41 -05002810#ifdef USE_CAIRO_GLESV2
2811# define GL_BIT EGL_OPENGL_ES2_BIT
2812#else
2813# define GL_BIT EGL_OPENGL_BIT
2814#endif
2815
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002816 static const EGLint argb_cfg_attribs[] = {
2817 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002818 EGL_RED_SIZE, 1,
2819 EGL_GREEN_SIZE, 1,
2820 EGL_BLUE_SIZE, 1,
2821 EGL_ALPHA_SIZE, 1,
2822 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002823 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002824 EGL_NONE
2825 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002826
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002827#ifdef USE_CAIRO_GLESV2
2828 static const EGLint context_attribs[] = {
2829 EGL_CONTEXT_CLIENT_VERSION, 2,
2830 EGL_NONE
2831 };
2832 EGLint api = EGL_OPENGL_ES_API;
2833#else
2834 EGLint *context_attribs = NULL;
2835 EGLint api = EGL_OPENGL_API;
2836#endif
2837
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002838 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002839 if (!eglInitialize(d->dpy, &major, &minor)) {
2840 fprintf(stderr, "failed to initialize display\n");
2841 return -1;
2842 }
2843
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002844 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002845 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2846 return -1;
2847 }
2848
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002849 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2850 &d->argb_config, 1, &n) || n != 1) {
2851 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002852 return -1;
2853 }
2854
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002855 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002856 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002857 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002858 fprintf(stderr, "failed to create context\n");
2859 return -1;
2860 }
2861
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002862 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002863 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002864 return -1;
2865 }
2866
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002867#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002868 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2869 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2870 fprintf(stderr, "failed to get cairo egl argb device\n");
2871 return -1;
2872 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002873#endif
2874
2875 return 0;
2876}
2877
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002878static void
2879fini_egl(struct display *display)
2880{
2881#ifdef HAVE_CAIRO_EGL
2882 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002883#endif
2884
2885 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2886 EGL_NO_CONTEXT);
2887
2888 eglTerminate(display->dpy);
2889 eglReleaseThread();
2890}
2891
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002892static int
2893event_mask_update(uint32_t mask, void *data)
2894{
2895 struct display *d = data;
2896
2897 d->mask = mask;
2898
2899 return 0;
2900}
2901
2902static void
2903handle_display_data(struct task *task, uint32_t events)
2904{
2905 struct display *display =
2906 container_of(task, struct display, display_task);
2907
2908 wl_display_iterate(display->display, display->mask);
2909}
2910
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002911struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002912display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002913{
2914 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002915
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002916 argc = parse_options(xkb_options,
2917 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002918
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002919 d = malloc(sizeof *d);
2920 if (d == NULL)
2921 return NULL;
2922
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002923 memset(d, 0, sizeof *d);
2924
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002925 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002926 if (d->display == NULL) {
2927 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002928 return NULL;
2929 }
2930
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002931 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2932 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2933 d->display_task.run = handle_display_data;
2934 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2935
2936 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002937 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002938 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002939
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002940 /* Set up listener so we'll catch all events. */
2941 wl_display_add_global_listener(d->display,
2942 display_handle_global, d);
2943
2944 /* Process connection events. */
2945 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002946 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002947 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002948
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002949 d->image_target_texture_2d =
2950 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2951 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2952 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2953
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002954 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002955
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002956 display_render_frame(d);
2957
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002958 wl_list_init(&d->window_list);
2959
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002960 init_xkb(d);
2961
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002962 return d;
2963}
2964
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002965static void
2966display_destroy_outputs(struct display *display)
2967{
2968 struct output *tmp;
2969 struct output *output;
2970
2971 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2972 output_destroy(output);
2973}
2974
Pekka Paalanene1207c72011-12-16 12:02:09 +02002975static void
2976display_destroy_inputs(struct display *display)
2977{
2978 struct input *tmp;
2979 struct input *input;
2980
2981 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2982 input_destroy(input);
2983}
2984
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002985void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002986display_destroy(struct display *display)
2987{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002988 if (!wl_list_empty(&display->window_list))
2989 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2990
2991 if (!wl_list_empty(&display->deferred_list))
2992 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2993
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002994 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002995 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002996
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002997 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002998
2999 cairo_surface_destroy(display->active_frame);
3000 cairo_surface_destroy(display->inactive_frame);
3001 cairo_surface_destroy(display->shadow);
3002 destroy_pointer_surfaces(display);
3003
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003004 fini_egl(display);
3005
Pekka Paalanenc2052982011-12-16 11:41:32 +02003006 if (display->shell)
3007 wl_shell_destroy(display->shell);
3008
3009 if (display->shm)
3010 wl_shm_destroy(display->shm);
3011
3012 if (display->data_device_manager)
3013 wl_data_device_manager_destroy(display->data_device_manager);
3014
3015 wl_compositor_destroy(display->compositor);
3016
3017 close(display->epoll_fd);
3018
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003019 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003020 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003021 free(display);
3022}
3023
3024void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003025display_set_user_data(struct display *display, void *data)
3026{
3027 display->user_data = data;
3028}
3029
3030void *
3031display_get_user_data(struct display *display)
3032{
3033 return display->user_data;
3034}
3035
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003036struct wl_display *
3037display_get_display(struct display *display)
3038{
3039 return display->display;
3040}
3041
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003042struct output *
3043display_get_output(struct display *display)
3044{
3045 return container_of(display->output_list.next, struct output, link);
3046}
3047
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003048struct wl_compositor *
3049display_get_compositor(struct display *display)
3050{
3051 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003052}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003053
3054EGLDisplay
3055display_get_egl_display(struct display *d)
3056{
3057 return d->dpy;
3058}
3059
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003060struct wl_data_source *
3061display_create_data_source(struct display *display)
3062{
3063 return wl_data_device_manager_create_data_source(display->data_device_manager);
3064}
3065
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003066EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003067display_get_argb_egl_config(struct display *d)
3068{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003069 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003070}
3071
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003072struct wl_shell *
3073display_get_shell(struct display *display)
3074{
3075 return display->shell;
3076}
3077
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003078int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003079display_acquire_window_surface(struct display *display,
3080 struct window *window,
3081 EGLContext ctx)
3082{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003083#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003084 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003085 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003086
3087 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003089 device = cairo_surface_get_device(window->cairo_surface);
3090 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003091 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003092
Benjamin Franzke0c991632011-09-27 21:57:31 +02003093 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003094 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003095 ctx = display->argb_ctx;
3096 else
3097 assert(0);
3098 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003099
3100 data = cairo_surface_get_user_data(window->cairo_surface,
3101 &surface_data_key);
3102
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003103 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003104 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003105 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3106 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003107
3108 return 0;
3109#else
3110 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003111#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003112}
3113
3114void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003115display_release_window_surface(struct display *display,
3116 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003117{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003118#ifdef HAVE_CAIRO_EGL
3119 cairo_device_t *device;
3120
3121 device = cairo_surface_get_device(window->cairo_surface);
3122 if (!device)
3123 return;
3124
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003125 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003126 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003127 cairo_device_release(device);
3128#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003129}
3130
3131void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003132display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003133{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003134 wl_list_insert(&display->deferred_list, &task->link);
3135}
3136
3137void
3138display_watch_fd(struct display *display,
3139 int fd, uint32_t events, struct task *task)
3140{
3141 struct epoll_event ep;
3142
3143 ep.events = events;
3144 ep.data.ptr = task;
3145 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3146}
3147
3148void
3149display_run(struct display *display)
3150{
3151 struct task *task;
3152 struct epoll_event ep[16];
3153 int i, count;
3154
Pekka Paalanen826d7952011-12-15 10:14:07 +02003155 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003156 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003157 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003158
3159 while (!wl_list_empty(&display->deferred_list)) {
3160 task = container_of(display->deferred_list.next,
3161 struct task, link);
3162 wl_list_remove(&task->link);
3163 task->run(task, 0);
3164 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003165
3166 if (!display->running)
3167 break;
3168
3169 wl_display_flush(display->display);
3170
3171 count = epoll_wait(display->epoll_fd,
3172 ep, ARRAY_LENGTH(ep), -1);
3173 for (i = 0; i < count; i++) {
3174 task = ep[i].data.ptr;
3175 task->run(task, ep[i].events);
3176 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003177 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003178}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003179
3180void
3181display_exit(struct display *display)
3182{
3183 display->running = 0;
3184}