blob: b09de5b65b0f7abceb9c83f3acf8f82a34b490d5 [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øgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Daniel Stone9d4f0302012-02-15 16:33:21 +000052#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050053#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040054
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020056#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040057#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050060
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050062 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050063 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040064 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040065 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050068 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040071
72 int display_fd;
73 uint32_t mask;
74 struct task display_task;
75
76 int epoll_fd;
77 struct wl_list deferred_list;
78
Pekka Paalanen826d7952011-12-15 10:14:07 +020079 int running;
80
Kristian Høgsberg478d9262010-06-08 20:34:11 -040081 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040082 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050083 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040084 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050085 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040086 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040087 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040088
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050089 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
90 PFNEGLCREATEIMAGEKHRPROC create_image;
91 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020092
93 display_output_handler_t output_configure_handler;
94
95 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050096};
97
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040098enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400100 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500101 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500112 struct wl_region *input_region;
113 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500114 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500115 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500116 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500117 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400118 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400119 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400120 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500121 int resize_scheduled;
122 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400123 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400124 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400126 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500127
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500129
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500130 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500131 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400132 window_data_handler_t data_handler;
133 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500134 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400135
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200136 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500137 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500138
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500139 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400140 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500141};
142
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500143struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500144 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500145 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400146 struct wl_list link;
147 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500148 widget_resize_handler_t resize_handler;
149 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500150 widget_enter_handler_t enter_handler;
151 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500152 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500153 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400154 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500155 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156};
157
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400158struct input {
159 struct display *display;
160 struct wl_input_device *input_device;
161 struct window *pointer_focus;
162 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400163 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400164 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200165 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400166 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400167
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500168 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500169 struct widget *grab;
170 uint32_t grab_button;
171
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400172 struct wl_data_device *data_device;
173 struct data_offer *drag_offer;
174 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400175};
176
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500177struct output {
178 struct display *display;
179 struct wl_output *output;
180 struct rectangle allocation;
181 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200182
183 display_output_handler_t destroy_handler;
184 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500185};
186
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500187struct frame {
188 struct widget *widget;
189 struct widget *child;
190 int margin;
191};
192
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500193struct menu {
194 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500195 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500196 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500197 const char **entries;
198 uint32_t time;
199 int current;
200 int count;
201 menu_func_t func;
202};
203
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400204enum {
205 POINTER_DEFAULT = 100,
206 POINTER_UNSET
207};
208
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500209enum window_location {
210 WINDOW_INTERIOR = 0,
211 WINDOW_RESIZING_TOP = 1,
212 WINDOW_RESIZING_BOTTOM = 2,
213 WINDOW_RESIZING_LEFT = 4,
214 WINDOW_RESIZING_TOP_LEFT = 5,
215 WINDOW_RESIZING_BOTTOM_LEFT = 6,
216 WINDOW_RESIZING_RIGHT = 8,
217 WINDOW_RESIZING_TOP_RIGHT = 9,
218 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
219 WINDOW_RESIZING_MASK = 15,
220 WINDOW_EXTERIOR = 16,
221 WINDOW_TITLEBAR = 17,
222 WINDOW_CLIENT_AREA = 18,
223};
224
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400225const char *option_xkb_layout = "us";
226const char *option_xkb_variant = "";
227const char *option_xkb_options = "";
228
229static const GOptionEntry xkb_option_entries[] = {
230 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_layout, "XKB Layout" },
232 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
233 &option_xkb_variant, "XKB Variant" },
234 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
235 &option_xkb_options, "XKB Options" },
236 { NULL }
237};
238
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400239static const cairo_user_data_key_t surface_data_key;
240struct surface_data {
241 struct wl_buffer *buffer;
242};
243
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500244#define MULT(_d,c,a,t) \
245 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
246
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500247#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400248
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100249struct egl_window_surface_data {
250 struct display *display;
251 struct wl_surface *surface;
252 struct wl_egl_window *window;
253 EGLSurface surf;
254};
255
256static void
257egl_window_surface_data_destroy(void *p)
258{
259 struct egl_window_surface_data *data = p;
260 struct display *d = data->display;
261
262 eglDestroySurface(d->dpy, data->surf);
263 wl_egl_window_destroy(data->window);
264 data->surface = NULL;
265
266 free(p);
267}
268
269static cairo_surface_t *
270display_create_egl_window_surface(struct display *display,
271 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400272 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100273 struct rectangle *rectangle)
274{
275 cairo_surface_t *cairo_surface;
276 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200278 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100279
280 data = malloc(sizeof *data);
281 if (data == NULL)
282 return NULL;
283
284 data->display = display;
285 data->surface = surface;
286
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500287 config = display->argb_config;
288 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400290 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100291 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400292 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500295 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296
Benjamin Franzke0c991632011-09-27 21:57:31 +0200297 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298 data->surf,
299 rectangle->width,
300 rectangle->height);
301
302 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
303 data, egl_window_surface_data_destroy);
304
305 return cairo_surface;
306}
307
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500308struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400309 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200310 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 EGLImageKHR image;
312 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800313 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500314 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315};
316
317static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400319{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800321 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322
Benjamin Franzke0c991632011-09-27 21:57:31 +0200323 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200325 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500327 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500329 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500330 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331}
332
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500333EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334display_get_image_for_egl_image_surface(struct display *display,
335 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500336{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338
339 data = cairo_surface_get_user_data (surface, &surface_data_key);
340
341 return data->image;
342}
343
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500344static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400346 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400348{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350 EGLDisplay dpy = display->dpy;
351 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200352 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353
354 data = malloc(sizeof *data);
355 if (data == NULL)
356 return NULL;
357
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800358 data->display = display;
359
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400360 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400361 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500362 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000363 free(data);
364 return NULL;
365 }
366
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500367 data->device = display->argb_device;
368 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500370 data->image = display->create_image(dpy, NULL,
371 EGL_NATIVE_PIXMAP_KHR,
372 (EGLClientBuffer) data->pixmap,
373 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500374 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500375 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500376 free(data);
377 return NULL;
378 }
379
380 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400381 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400384 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400385 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500386 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200387 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400388
Benjamin Franzke0c991632011-09-27 21:57:31 +0200389 surface = cairo_gl_surface_create_for_texture(data->device,
390 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 data->texture,
392 rectangle->width,
393 rectangle->height);
394
395 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500396 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397
398 return surface;
399}
400
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500401static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500402display_create_egl_image_surface_from_file(struct display *display,
403 const char *filename,
404 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405{
406 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400407 GdkPixbuf *pixbuf;
408 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400409 int stride, i;
410 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500411 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400412
413 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415 FALSE, &error);
416 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400417 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400419 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
420 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500421 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400423 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425
426 stride = gdk_pixbuf_get_rowstride(pixbuf);
427 pixels = gdk_pixbuf_get_pixels(pixbuf);
428
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 while (p < end) {
433 unsigned int t;
434
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435 MULT(p[0], p[0], p[3], t);
436 MULT(p[1], p[1], p[3], t);
437 MULT(p[2], p[2], p[3], t);
438 p += 4;
439
440 }
441 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400442
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200443 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000444 if (surface == NULL) {
445 g_object_unref(pixbuf);
446 return NULL;
447 }
448
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800449 data = cairo_surface_get_user_data(surface, &surface_data_key);
450
Benjamin Franzke0c991632011-09-27 21:57:31 +0200451 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800452 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800453 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
454 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200455 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400456
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500457 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400458
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459 return surface;
460}
461
462#endif
463
464struct wl_buffer *
465display_get_buffer_for_surface(struct display *display,
466 cairo_surface_t *surface)
467{
468 struct surface_data *data;
469
470 data = cairo_surface_get_user_data (surface, &surface_data_key);
471
472 return data->buffer;
473}
474
475struct shm_surface_data {
476 struct surface_data data;
477 void *map;
478 size_t length;
479};
480
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500481static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400482shm_surface_data_destroy(void *p)
483{
484 struct shm_surface_data *data = p;
485
486 wl_buffer_destroy(data->data.buffer);
487 munmap(data->map, data->length);
488}
489
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500490static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400492 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400493{
494 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400495 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400496 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800497 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498 char filename[] = "/tmp/wayland-shm-XXXXXX";
499
500 data = malloc(sizeof *data);
501 if (data == NULL)
502 return NULL;
503
504 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
505 rectangle->width);
506 data->length = stride * rectangle->height;
507 fd = mkstemp(filename);
508 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000509 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400510 return NULL;
511 }
512 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000513 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400514 close(fd);
515 return NULL;
516 }
517
518 data->map = mmap(NULL, data->length,
519 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
520 unlink(filename);
521
522 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000523 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 close(fd);
525 return NULL;
526 }
527
528 surface = cairo_image_surface_create_for_data (data->map,
529 CAIRO_FORMAT_ARGB32,
530 rectangle->width,
531 rectangle->height,
532 stride);
533
534 cairo_surface_set_user_data (surface, &surface_data_key,
535 data, shm_surface_data_destroy);
536
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400537 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500538 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400539 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500540 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400541
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400542 data->data.buffer = wl_shm_create_buffer(display->shm,
543 fd,
544 rectangle->width,
545 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400546 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
548 close(fd);
549
550 return surface;
551}
552
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500553static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554display_create_shm_surface_from_file(struct display *display,
555 const char *filename,
556 struct rectangle *rect)
557{
558 cairo_surface_t *surface;
559 GdkPixbuf *pixbuf;
560 GError *error = NULL;
561 int stride, i;
562 unsigned char *pixels, *p, *end, *dest_data;
563 int dest_stride;
564 uint32_t *d;
565
566 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
567 rect->width, rect->height,
568 FALSE, &error);
569 if (error != NULL)
570 return NULL;
571
572 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
573 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500574 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575 return NULL;
576 }
577
578 stride = gdk_pixbuf_get_rowstride(pixbuf);
579 pixels = gdk_pixbuf_get_pixels(pixbuf);
580
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400581 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000582 if (surface == NULL) {
583 g_object_unref(pixbuf);
584 return NULL;
585 }
586
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400587 dest_data = cairo_image_surface_get_data (surface);
588 dest_stride = cairo_image_surface_get_stride (surface);
589
590 for (i = 0; i < rect->height; i++) {
591 d = (uint32_t *) (dest_data + i * dest_stride);
592 p = pixels + i * stride;
593 end = p + rect->width * 4;
594 while (p < end) {
595 unsigned int t;
596 unsigned char a, r, g, b;
597
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400598 a = p[3];
599 MULT(r, p[0], a, t);
600 MULT(g, p[1], a, t);
601 MULT(b, p[2], a, t);
602 p += 4;
603 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
604 }
605 }
606
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500607 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608
609 return surface;
610}
611
nobled7b87cb02011-02-01 18:51:47 +0000612static int
613check_size(struct rectangle *rect)
614{
615 if (rect->width && rect->height)
616 return 0;
617
618 fprintf(stderr, "tried to create surface of "
619 "width: %d, height: %d\n", rect->width, rect->height);
620 return -1;
621}
622
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400623cairo_surface_t *
624display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100625 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400626 struct rectangle *rectangle,
627 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628{
nobled7b87cb02011-02-01 18:51:47 +0000629 if (check_size(rectangle) < 0)
630 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500631#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500632 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633 if (surface)
634 return display_create_egl_window_surface(display,
635 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400636 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637 rectangle);
638 else
639 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400640 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500642 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400643#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400644 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400645}
646
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500647static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648display_create_surface_from_file(struct display *display,
649 const char *filename,
650 struct rectangle *rectangle)
651{
nobled7b87cb02011-02-01 18:51:47 +0000652 if (check_size(rectangle) < 0)
653 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500654#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500655 if (display->dpy) {
656 return display_create_egl_image_surface_from_file(display,
657 filename,
658 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500659 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500661 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662}
Yuval Fledel45568f62010-12-06 09:18:12 -0500663 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400664 const char *filename;
665 int hotspot_x, hotspot_y;
666} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500667 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
668 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
669 { DATADIR "/weston/bottom_side.png", 16, 20 },
670 { DATADIR "/weston/grabbing.png", 20, 17 },
671 { DATADIR "/weston/left_ptr.png", 10, 5 },
672 { DATADIR "/weston/left_side.png", 10, 20 },
673 { DATADIR "/weston/right_side.png", 30, 19 },
674 { DATADIR "/weston/top_left_corner.png", 8, 8 },
675 { DATADIR "/weston/top_right_corner.png", 26, 8 },
676 { DATADIR "/weston/top_side.png", 18, 8 },
677 { DATADIR "/weston/xterm.png", 15, 15 },
678 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400679};
680
681static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400682create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400683{
684 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400686 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400687
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689 display->pointer_surfaces =
690 malloc(count * sizeof *display->pointer_surfaces);
691 rect.width = width;
692 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400695 display_create_surface_from_file(display,
696 pointer_images[i].filename,
697 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100698 if (!display->pointer_surfaces[i]) {
699 fprintf(stderr, "Error loading pointer image: %s\n",
700 pointer_images[i].filename);
701 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 }
703
704}
705
Pekka Paalanen325bb602011-12-19 10:31:45 +0200706static void
707destroy_pointer_surfaces(struct display *display)
708{
709 int i, count;
710
711 count = ARRAY_LENGTH(pointer_images);
712 for (i = 0; i < count; ++i) {
713 if (display->pointer_surfaces[i])
714 cairo_surface_destroy(display->pointer_surfaces[i]);
715 }
716 free(display->pointer_surfaces);
717}
718
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400719cairo_surface_t *
720display_get_pointer_surface(struct display *display, int pointer,
721 int *width, int *height,
722 int *hotspot_x, int *hotspot_y)
723{
724 cairo_surface_t *surface;
725
726 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500727#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728 *width = cairo_gl_surface_get_width(surface);
729 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000730#else
731 *width = cairo_image_surface_get_width(surface);
732 *height = cairo_image_surface_get_height(surface);
733#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 *hotspot_x = pointer_images[pointer].hotspot_x;
735 *hotspot_y = pointer_images[pointer].hotspot_y;
736
737 return cairo_surface_reference(surface);
738}
739
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400740static void
741window_attach_surface(struct window *window);
742
743static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400744free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400745{
746 struct window *window = data;
747
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400748 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400749 cairo_surface_destroy(window->pending_surface);
750 window->pending_surface = NULL;
751 if (window->cairo_surface)
752 window_attach_surface(window);
753}
754
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400755static const struct wl_callback_listener free_surface_listener = {
756 free_surface
757};
758
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500759static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200760window_get_resize_dx_dy(struct window *window, int *x, int *y)
761{
762 if (window->resize_edges & WINDOW_RESIZING_LEFT)
763 *x = window->server_allocation.width - window->allocation.width;
764 else
765 *x = 0;
766
767 if (window->resize_edges & WINDOW_RESIZING_TOP)
768 *y = window->server_allocation.height -
769 window->allocation.height;
770 else
771 *y = 0;
772
773 window->resize_edges = 0;
774}
775
776static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500777window_attach_surface(struct window *window)
778{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400779 struct display *display = window->display;
780 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400781 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000782#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100783 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000784#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500785 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100786
787 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000788#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
790 data = cairo_surface_get_user_data(window->cairo_surface,
791 &surface_data_key);
792
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100793 cairo_gl_surface_swapbuffers(window->cairo_surface);
794 wl_egl_window_get_attached_size(data->window,
795 &window->server_allocation.width,
796 &window->server_allocation.height);
797 break;
798 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000799#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100800 case WINDOW_BUFFER_TYPE_SHM:
801 if (window->pending_surface != NULL)
802 return;
803
804 window->pending_surface = window->cairo_surface;
805 window->cairo_surface = NULL;
806
807 buffer =
808 display_get_buffer_for_surface(display,
809 window->pending_surface);
810
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200811 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100812 wl_surface_attach(window->surface, buffer, x, y);
813 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400814 cb = wl_display_sync(display->display);
815 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817 default:
818 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500820
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500821 if (window->input_region) {
822 wl_surface_set_input_region(window->surface,
823 window->input_region);
824 wl_region_destroy(window->input_region);
825 window->input_region = NULL;
826 }
827
828 if (window->opaque_region) {
829 wl_surface_set_opaque_region(window->surface,
830 window->opaque_region);
831 wl_region_destroy(window->opaque_region);
832 window->opaque_region = NULL;
833 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500834
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500835 wl_surface_damage(window->surface, 0, 0,
836 window->allocation.width,
837 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500838}
839
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500840void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400841window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500842{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100843 if (window->cairo_surface) {
844 switch (window->buffer_type) {
845 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
846 case WINDOW_BUFFER_TYPE_SHM:
847 display_surface_damage(window->display,
848 window->cairo_surface,
849 0, 0,
850 window->allocation.width,
851 window->allocation.height);
852 break;
853 default:
854 break;
855 }
856 window_attach_surface(window);
857 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500858}
859
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400860void
861window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400862{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500863 cairo_surface_reference(surface);
864
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400865 if (window->cairo_surface != NULL)
866 cairo_surface_destroy(window->cairo_surface);
867
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500868 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400869}
870
Benjamin Franzke22d54812011-07-16 19:50:32 +0000871#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872static void
873window_resize_cairo_window_surface(struct window *window)
874{
875 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200876 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877
878 data = cairo_surface_get_user_data(window->cairo_surface,
879 &surface_data_key);
880
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200881 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100882 wl_egl_window_resize(data->window,
883 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200884 window->allocation.height,
885 x,y);
886
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887 cairo_gl_surface_set_size(window->cairo_surface,
888 window->allocation.width,
889 window->allocation.height);
890}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000891#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400893struct display *
894window_get_display(struct window *window)
895{
896 return window->display;
897}
898
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400899void
900window_create_surface(struct window *window)
901{
902 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400903 uint32_t flags = 0;
904
905 if (!window->transparent)
906 flags = SURFACE_OPAQUE;
907
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500909#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
911 if (window->cairo_surface) {
912 window_resize_cairo_window_surface(window);
913 return;
914 }
915 surface = display_create_surface(window->display,
916 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400917 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100918 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500919 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100921 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923 break;
924#endif
925 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400926 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800929 default:
930 surface = NULL;
931 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400933
934 window_set_surface(window, surface);
935 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936}
937
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200938static void frame_destroy(struct frame *frame);
939
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400940void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500941window_destroy(struct window *window)
942{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200943 struct display *display = window->display;
944 struct input *input;
945
946 if (window->redraw_scheduled)
947 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500948 if (window->resize_scheduled)
949 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200950
951 wl_list_for_each(input, &display->input_list, link) {
952 if (input->pointer_focus == window)
953 input->pointer_focus = NULL;
954 if (input->keyboard_focus == window)
955 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500956 if (input->focus_widget &&
957 input->focus_widget->window == window)
958 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200959 }
960
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500961 if (window->input_region)
962 wl_region_destroy(window->input_region);
963 if (window->opaque_region)
964 wl_region_destroy(window->opaque_region);
965
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200966 if (window->frame)
967 frame_destroy(window->frame);
968
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200969 if (window->shell_surface)
970 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500971 wl_surface_destroy(window->surface);
972 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200973
974 if (window->cairo_surface != NULL)
975 cairo_surface_destroy(window->cairo_surface);
976 if (window->pending_surface != NULL)
977 cairo_surface_destroy(window->pending_surface);
978
979 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500980 free(window);
981}
982
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500983static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500984widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400985{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500986 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500988 wl_list_for_each(child, &widget->child_list, link) {
989 target = widget_find_widget(child, x, y);
990 if (target)
991 return target;
992 }
993
994 if (widget->allocation.x <= x &&
995 x < widget->allocation.x + widget->allocation.width &&
996 widget->allocation.y <= y &&
997 y < widget->allocation.y + widget->allocation.height) {
998 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400999 }
1000
1001 return NULL;
1002}
1003
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004static struct widget *
1005widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001007 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009 widget = malloc(sizeof *widget);
1010 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001011 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001012 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001013 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001014 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001015 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001016
1017 return widget;
1018}
1019
1020struct widget *
1021window_add_widget(struct window *window, void *data)
1022{
1023 window->widget = widget_create(window, data);
1024 wl_list_init(&window->widget->link);
1025
1026 return window->widget;
1027}
1028
1029struct widget *
1030widget_add_widget(struct widget *parent, void *data)
1031{
1032 struct widget *widget;
1033
1034 widget = widget_create(parent->window, data);
1035 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001036
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001037 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001038}
1039
1040void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001041widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001043 struct display *display = widget->window->display;
1044 struct input *input;
1045
1046 wl_list_for_each(input, &display->input_list, link) {
1047 if (input->focus_widget == widget)
1048 input->focus_widget = NULL;
1049 }
1050
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001051 wl_list_remove(&widget->link);
1052 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001053}
1054
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001056widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059}
1060
1061void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001062widget_set_size(struct widget *widget, int32_t width, int32_t height)
1063{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001064 widget->allocation.width = width;
1065 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001066}
1067
1068void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069widget_set_allocation(struct widget *widget,
1070 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 widget->allocation.x = x;
1073 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001074 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075}
1076
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001077void
1078widget_set_transparent(struct widget *widget, int transparent)
1079{
1080 widget->opaque = !transparent;
1081}
1082
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001083void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001089void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001090widget_set_resize_handler(struct widget *widget,
1091 widget_resize_handler_t handler)
1092{
1093 widget->resize_handler = handler;
1094}
1095
1096void
1097widget_set_redraw_handler(struct widget *widget,
1098 widget_redraw_handler_t handler)
1099{
1100 widget->redraw_handler = handler;
1101}
1102
1103void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001104widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001105{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001106 widget->enter_handler = handler;
1107}
1108
1109void
1110widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1111{
1112 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001113}
1114
1115void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001116widget_set_motion_handler(struct widget *widget,
1117 widget_motion_handler_t handler)
1118{
1119 widget->motion_handler = handler;
1120}
1121
1122void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001123widget_set_button_handler(struct widget *widget,
1124 widget_button_handler_t handler)
1125{
1126 widget->button_handler = handler;
1127}
1128
1129void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001130widget_schedule_redraw(struct widget *widget)
1131{
1132 window_schedule_redraw(widget->window);
1133}
1134
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001135cairo_surface_t *
1136window_get_surface(struct window *window)
1137{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001138 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001139}
1140
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001141struct wl_surface *
1142window_get_wl_surface(struct window *window)
1143{
1144 return window->surface;
1145}
1146
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001147struct wl_shell_surface *
1148window_get_wl_shell_surface(struct window *window)
1149{
1150 return window->shell_surface;
1151}
1152
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001153static void
1154frame_resize_handler(struct widget *widget,
1155 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001156{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001157 struct frame *frame = data;
1158 struct widget *child = frame->child;
1159 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001160 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001161 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001162 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001163
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001164 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001165 decoration_width = 20 + frame->margin * 2;
1166 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001168 allocation.x = 10 + frame->margin;
1169 allocation.y = 50 + frame->margin;
1170 allocation.width = width - decoration_width;
1171 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001172
1173 widget->window->input_region =
1174 wl_compositor_create_region(display->compositor);
1175 wl_region_add(widget->window->input_region,
1176 frame->margin, frame->margin,
1177 width - 2 * frame->margin,
1178 height - 2 * frame->margin);
1179
1180 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001181 } else {
1182 decoration_width = 0;
1183 decoration_height = 0;
1184
1185 allocation.x = 0;
1186 allocation.y = 0;
1187 allocation.width = width;
1188 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001189 opaque_margin = 0;
1190 }
1191
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001192 widget_set_allocation(child, allocation.x, allocation.y,
1193 allocation.width, allocation.height);
1194
1195 if (child->resize_handler)
1196 child->resize_handler(child,
1197 allocation.width,
1198 allocation.height,
1199 child->user_data);
1200
1201 widget_set_allocation(widget, 0, 0,
1202 child->allocation.width + decoration_width,
1203 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001204
1205 if (child->opaque) {
1206 widget->window->opaque_region =
1207 wl_compositor_create_region(display->compositor);
1208 wl_region_add(widget->window->opaque_region,
1209 opaque_margin, opaque_margin,
1210 widget->allocation.width - 2 * opaque_margin,
1211 widget->allocation.height - 2 * opaque_margin);
1212 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001213}
1214
1215static void
1216frame_redraw_handler(struct widget *widget, void *data)
1217{
1218 struct frame *frame = data;
1219 cairo_t *cr;
1220 cairo_text_extents_t extents;
1221 cairo_surface_t *source;
1222 int width, height, shadow_dx = 3, shadow_dy = 3;
1223 struct window *window = widget->window;
1224
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001225 if (window->type == TYPE_FULLSCREEN)
1226 return;
1227
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228 width = widget->allocation.width;
1229 height = widget->allocation.height;
1230
1231 cr = cairo_create(window->cairo_surface);
1232
1233 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1234 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1235 cairo_paint(cr);
1236
1237 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1238 tile_mask(cr, window->display->shadow,
1239 shadow_dx, shadow_dy, width, height,
1240 frame->margin + 10 - shadow_dx,
1241 frame->margin + 10 - shadow_dy);
1242
1243 if (window->keyboard_device)
1244 source = window->display->active_frame;
1245 else
1246 source = window->display->inactive_frame;
1247
1248 tile_source(cr, source, 0, 0, width, height,
1249 frame->margin + 10, frame->margin + 50);
1250
1251 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1252 cairo_set_font_size(cr, 14);
1253 cairo_text_extents(cr, window->title, &extents);
1254 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1255 if (window->keyboard_device)
1256 cairo_set_source_rgb(cr, 0, 0, 0);
1257 else
1258 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1259 cairo_show_text(cr, window->title);
1260
1261 cairo_destroy(cr);
1262}
1263
1264static int
1265frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1266{
1267 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001268 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 const int grip_size = 8;
1270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001271 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001272 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 hlocation = WINDOW_RESIZING_RIGHT;
1279 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001280 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001281
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001282 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001283 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001284 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001287 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001288 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289 vlocation = WINDOW_RESIZING_BOTTOM;
1290 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001291 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001293 location = vlocation | hlocation;
1294 if (location & WINDOW_EXTERIOR)
1295 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001296 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001297 location = WINDOW_TITLEBAR;
1298 else if (location == WINDOW_INTERIOR)
1299 location = WINDOW_CLIENT_AREA;
1300
1301 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001302}
1303
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001305frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001306{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001308
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001309 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 switch (location) {
1311 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001312 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001313 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001315 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001316 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001317 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001318 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001319 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001320 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001322 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001323 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001324 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001325 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001326 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001327 case WINDOW_EXTERIOR:
1328 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001330 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001331 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001332}
1333
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001334static void
1335frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001336{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001337 switch (index) {
1338 case 0: /* close */
1339 if (window->close_handler)
1340 window->close_handler(window->parent,
1341 window->user_data);
1342 else
1343 display_exit(window->display);
1344 break;
1345 case 1: /* fullscreen */
1346 /* we don't have a way to get out of fullscreen for now */
1347 window_set_fullscreen(window, 1);
1348 break;
1349 case 2: /* rotate */
1350 case 3: /* scale */
1351 break;
1352 }
1353}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001354
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001355void
1356window_show_frame_menu(struct window *window,
1357 struct input *input, uint32_t time)
1358{
1359 int32_t x, y;
1360
1361 static const char *entries[] = {
1362 "Close", "Fullscreen", "Rotate", "Scale"
1363 };
1364
1365 input_get_position(input, &x, &y);
1366 window_show_menu(window->display, input, time, window,
1367 x - 10, y - 10, frame_menu_func, entries, 4);
1368}
1369
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001370static int
1371frame_enter_handler(struct widget *widget,
1372 struct input *input, uint32_t time,
1373 int32_t x, int32_t y, void *data)
1374{
1375 return frame_get_pointer_image_for_location(data, input);
1376}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001377
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001378static int
1379frame_motion_handler(struct widget *widget,
1380 struct input *input, uint32_t time,
1381 int32_t x, int32_t y, void *data)
1382{
1383 return frame_get_pointer_image_for_location(data, input);
1384}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001385
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001386static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001387frame_button_handler(struct widget *widget,
1388 struct input *input, uint32_t time,
1389 int button, int state, void *data)
1390
1391{
1392 struct frame *frame = data;
1393 struct window *window = widget->window;
1394 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001395 location = frame_get_pointer_location(frame, input->sx, input->sy);
1396
1397 if (window->display->shell && button == BTN_LEFT && state == 1) {
1398 switch (location) {
1399 case WINDOW_TITLEBAR:
1400 if (!window->shell_surface)
1401 break;
1402 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001403 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001404 wl_shell_surface_move(window->shell_surface,
1405 input_get_input_device(input),
1406 time);
1407 break;
1408 case WINDOW_RESIZING_TOP:
1409 case WINDOW_RESIZING_BOTTOM:
1410 case WINDOW_RESIZING_LEFT:
1411 case WINDOW_RESIZING_RIGHT:
1412 case WINDOW_RESIZING_TOP_LEFT:
1413 case WINDOW_RESIZING_TOP_RIGHT:
1414 case WINDOW_RESIZING_BOTTOM_LEFT:
1415 case WINDOW_RESIZING_BOTTOM_RIGHT:
1416 if (!window->shell_surface)
1417 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001418 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001419 wl_shell_surface_resize(window->shell_surface,
1420 input_get_input_device(input),
1421 time, location);
1422 break;
1423 }
1424 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001425 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001426 }
1427}
1428
1429struct widget *
1430frame_create(struct window *window, void *data)
1431{
1432 struct frame *frame;
1433
1434 frame = malloc(sizeof *frame);
1435 memset(frame, 0, sizeof *frame);
1436
1437 frame->widget = window_add_widget(window, frame);
1438 frame->child = widget_add_widget(frame->widget, data);
1439 frame->margin = 16;
1440
1441 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1442 widget_set_resize_handler(frame->widget, frame_resize_handler);
1443 widget_set_enter_handler(frame->widget, frame_enter_handler);
1444 widget_set_motion_handler(frame->widget, frame_motion_handler);
1445 widget_set_button_handler(frame->widget, frame_button_handler);
1446
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001447 window->frame = frame;
1448
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001449 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001450}
1451
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001452static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001453frame_destroy(struct frame *frame)
1454{
1455 /* frame->child must be destroyed by the application */
1456 widget_destroy(frame->widget);
1457 free(frame);
1458}
1459
1460static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001461input_set_focus_widget(struct input *input, struct widget *focus,
1462 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001463{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001464 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001465 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001466
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001467 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468 return;
1469
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001470 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001471 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001472 widget = old;
1473 if (input->grab)
1474 widget = input->grab;
1475 if (widget->leave_handler)
1476 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001477 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001478 }
1479
1480 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001481 widget = focus;
1482 if (input->grab)
1483 widget = input->grab;
1484 if (widget->enter_handler)
1485 pointer = widget->enter_handler(focus, input, time,
1486 x, y,
1487 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001488 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001489
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001490 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001491 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001492}
1493
1494static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001495input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001496 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001497{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001498 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001499 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001500 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001501 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001502
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001503 input->sx = sx;
1504 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001505
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001506 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001507 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001508 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001509 }
1510
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001511 if (input->grab)
1512 widget = input->grab;
1513 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001514 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001515 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001516 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001517 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001518 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001519
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001520 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001521}
1522
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001523void
1524input_grab(struct input *input, struct widget *widget, uint32_t button)
1525{
1526 input->grab = widget;
1527 input->grab_button = button;
1528}
1529
1530void
1531input_ungrab(struct input *input, uint32_t time)
1532{
1533 struct widget *widget;
1534
1535 input->grab = NULL;
1536 if (input->pointer_focus) {
1537 widget = widget_find_widget(input->pointer_focus->widget,
1538 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001539 input_set_focus_widget(input, widget,
1540 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001541 }
1542}
1543
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001544static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001545input_handle_button(void *data,
1546 struct wl_input_device *input_device,
1547 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001548{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001549 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001550 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001551
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001552 if (input->focus_widget && input->grab == NULL && state)
1553 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554
Neil Roberts6b28aad2012-01-23 19:11:18 +00001555 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001556 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001557 (*widget->button_handler)(widget,
1558 input, time,
1559 button, state,
1560 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001561
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001562 if (input->grab && input->grab_button == button && !state)
1563 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001564}
1565
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001566static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001567input_handle_key(void *data, struct wl_input_device *input_device,
1568 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001569{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 struct input *input = data;
1571 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001572 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001573 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001574
Daniel Stone0d5a5092012-02-16 12:48:00 +00001575 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001576 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001577 return;
1578
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001579 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001580 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001581 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1582 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001583
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001584 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1585
1586 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001587 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001588 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001589 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001590
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001591 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1592 if (state)
1593 window_set_maximized(window,
1594 window->type != TYPE_MAXIMIZED);
1595 } else if (window->key_handler) {
1596 (*window->key_handler)(window, input, time, key,
1597 sym, state, window->user_data);
1598 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001599}
1600
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001601static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001602input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001603{
1604 struct window *window = input->pointer_focus;
1605
1606 if (!window)
1607 return;
1608
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001609 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001610
Pekka Paalanene1207c72011-12-16 12:02:09 +02001611 input->pointer_focus = NULL;
1612 input->current_pointer_image = POINTER_UNSET;
1613}
1614
1615static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001616input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001617 struct wl_input_device *input_device,
1618 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001619 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001620{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001621 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001622 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001623 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001624
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001625 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001626 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001627
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001628 input->sx = sx;
1629 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001630
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001631 widget = widget_find_widget(window->widget, sx, sy);
1632 input_set_focus_widget(input, widget, time, sx, sy);
1633}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001634
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001635static void
1636input_handle_pointer_leave(void *data,
1637 struct wl_input_device *input_device,
1638 uint32_t time, struct wl_surface *surface)
1639{
1640 struct input *input = data;
1641
1642 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001643}
1644
1645static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001646input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001647{
1648 struct window *window = input->keyboard_focus;
1649
1650 if (!window)
1651 return;
1652
1653 window->keyboard_device = NULL;
1654 if (window->keyboard_focus_handler)
1655 (*window->keyboard_focus_handler)(window, NULL,
1656 window->user_data);
1657
1658 input->keyboard_focus = NULL;
1659}
1660
1661static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001662input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001663 struct wl_input_device *input_device,
1664 uint32_t time,
1665 struct wl_surface *surface,
1666 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001667{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001668 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001669 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001670 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001671 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001672
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001673 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001674
1675 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001676 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001677 for (k = keys->data; k < end; k++)
1678 input->modifiers |= d->xkb->map->modmap[*k];
1679
1680 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001681 window->keyboard_device = input;
1682 if (window->keyboard_focus_handler)
1683 (*window->keyboard_focus_handler)(window,
1684 window->keyboard_device,
1685 window->user_data);
1686}
1687
1688static void
1689input_handle_keyboard_leave(void *data,
1690 struct wl_input_device *input_device,
1691 uint32_t time,
1692 struct wl_surface *surface)
1693{
1694 struct input *input = data;
1695
1696 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001697}
1698
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001699static void
1700input_handle_touch_down(void *data,
1701 struct wl_input_device *wl_input_device,
1702 uint32_t time, struct wl_surface *surface,
1703 int32_t id, int32_t x, int32_t y)
1704{
1705}
1706
1707static void
1708input_handle_touch_up(void *data,
1709 struct wl_input_device *wl_input_device,
1710 uint32_t time, int32_t id)
1711{
1712}
1713
1714static void
1715input_handle_touch_motion(void *data,
1716 struct wl_input_device *wl_input_device,
1717 uint32_t time, int32_t id, int32_t x, int32_t y)
1718{
1719}
1720
1721static void
1722input_handle_touch_frame(void *data,
1723 struct wl_input_device *wl_input_device)
1724{
1725}
1726
1727static void
1728input_handle_touch_cancel(void *data,
1729 struct wl_input_device *wl_input_device)
1730{
1731}
1732
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001733static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001734 input_handle_motion,
1735 input_handle_button,
1736 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001737 input_handle_pointer_enter,
1738 input_handle_pointer_leave,
1739 input_handle_keyboard_enter,
1740 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001741 input_handle_touch_down,
1742 input_handle_touch_up,
1743 input_handle_touch_motion,
1744 input_handle_touch_frame,
1745 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001746};
1747
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001748void
1749input_get_position(struct input *input, int32_t *x, int32_t *y)
1750{
1751 *x = input->sx;
1752 *y = input->sy;
1753}
1754
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001755struct wl_input_device *
1756input_get_input_device(struct input *input)
1757{
1758 return input->input_device;
1759}
1760
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001761uint32_t
1762input_get_modifiers(struct input *input)
1763{
1764 return input->modifiers;
1765}
1766
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001767struct widget *
1768input_get_focus_widget(struct input *input)
1769{
1770 return input->focus_widget;
1771}
1772
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001773struct data_offer {
1774 struct wl_data_offer *offer;
1775 struct input *input;
1776 struct wl_array types;
1777 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001778
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001779 struct task io_task;
1780 int fd;
1781 data_func_t func;
1782 int32_t x, y;
1783 void *user_data;
1784};
1785
1786static void
1787data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1788{
1789 struct data_offer *offer = data;
1790 char **p;
1791
1792 p = wl_array_add(&offer->types, sizeof *p);
1793 *p = strdup(type);
1794}
1795
1796static const struct wl_data_offer_listener data_offer_listener = {
1797 data_offer_offer,
1798};
1799
1800static void
1801data_offer_destroy(struct data_offer *offer)
1802{
1803 char **p;
1804
1805 offer->refcount--;
1806 if (offer->refcount == 0) {
1807 wl_data_offer_destroy(offer->offer);
1808 for (p = offer->types.data; *p; p++)
1809 free(*p);
1810 wl_array_release(&offer->types);
1811 free(offer);
1812 }
1813}
1814
1815static void
1816data_device_data_offer(void *data,
1817 struct wl_data_device *data_device, uint32_t id)
1818{
1819 struct data_offer *offer;
1820
1821 offer = malloc(sizeof *offer);
1822
1823 wl_array_init(&offer->types);
1824 offer->refcount = 1;
1825 offer->input = data;
1826
1827 /* FIXME: Generate typesafe wrappers for this */
1828 offer->offer = (struct wl_data_offer *)
1829 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1830 id, &wl_data_offer_interface);
1831
1832 wl_data_offer_add_listener(offer->offer,
1833 &data_offer_listener, offer);
1834}
1835
1836static void
1837data_device_enter(void *data, struct wl_data_device *data_device,
1838 uint32_t time, struct wl_surface *surface,
1839 int32_t x, int32_t y, struct wl_data_offer *offer)
1840{
1841 struct input *input = data;
1842 struct window *window;
1843 char **p;
1844
1845 input->drag_offer = wl_data_offer_get_user_data(offer);
1846 window = wl_surface_get_user_data(surface);
1847 input->pointer_focus = window;
1848
1849 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1850 *p = NULL;
1851
1852 window = input->pointer_focus;
1853 if (window->data_handler)
1854 window->data_handler(window, input, time, x, y,
1855 input->drag_offer->types.data,
1856 window->user_data);
1857}
1858
1859static void
1860data_device_leave(void *data, struct wl_data_device *data_device)
1861{
1862 struct input *input = data;
1863
1864 data_offer_destroy(input->drag_offer);
1865 input->drag_offer = NULL;
1866}
1867
1868static void
1869data_device_motion(void *data, struct wl_data_device *data_device,
1870 uint32_t time, int32_t x, int32_t y)
1871{
1872 struct input *input = data;
1873 struct window *window = input->pointer_focus;
1874
1875 input->sx = x;
1876 input->sy = y;
1877
1878 if (window->data_handler)
1879 window->data_handler(window, input, time, x, y,
1880 input->drag_offer->types.data,
1881 window->user_data);
1882}
1883
1884static void
1885data_device_drop(void *data, struct wl_data_device *data_device)
1886{
1887 struct input *input = data;
1888 struct window *window = input->pointer_focus;
1889
1890 if (window->drop_handler)
1891 window->drop_handler(window, input,
1892 input->sx, input->sy, window->user_data);
1893}
1894
1895static void
1896data_device_selection(void *data,
1897 struct wl_data_device *wl_data_device,
1898 struct wl_data_offer *offer)
1899{
1900 struct input *input = data;
1901 char **p;
1902
1903 if (input->selection_offer)
1904 data_offer_destroy(input->selection_offer);
1905
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001906 if (offer) {
1907 input->selection_offer = wl_data_offer_get_user_data(offer);
1908 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1909 *p = NULL;
1910 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001911}
1912
1913static const struct wl_data_device_listener data_device_listener = {
1914 data_device_data_offer,
1915 data_device_enter,
1916 data_device_leave,
1917 data_device_motion,
1918 data_device_drop,
1919 data_device_selection
1920};
1921
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001922void
1923input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1924{
1925 struct display *display = input->display;
1926 struct wl_buffer *buffer;
1927 cairo_surface_t *surface;
1928
1929 if (pointer == input->current_pointer_image)
1930 return;
1931
1932 input->current_pointer_image = pointer;
1933 surface = display->pointer_surfaces[pointer];
1934
1935 if (!surface)
1936 return;
1937
1938 buffer = display_get_buffer_for_surface(display, surface);
1939 wl_input_device_attach(input->input_device, time, buffer,
1940 pointer_images[pointer].hotspot_x,
1941 pointer_images[pointer].hotspot_y);
1942}
1943
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001944struct wl_data_device *
1945input_get_data_device(struct input *input)
1946{
1947 return input->data_device;
1948}
1949
1950void
1951input_set_selection(struct input *input,
1952 struct wl_data_source *source, uint32_t time)
1953{
1954 wl_data_device_set_selection(input->data_device, source, time);
1955}
1956
1957void
1958input_accept(struct input *input, uint32_t time, const char *type)
1959{
1960 wl_data_offer_accept(input->drag_offer->offer, time, type);
1961}
1962
1963static void
1964offer_io_func(struct task *task, uint32_t events)
1965{
1966 struct data_offer *offer =
1967 container_of(task, struct data_offer, io_task);
1968 unsigned int len;
1969 char buffer[4096];
1970
1971 len = read(offer->fd, buffer, sizeof buffer);
1972 offer->func(buffer, len,
1973 offer->x, offer->y, offer->user_data);
1974
1975 if (len == 0) {
1976 close(offer->fd);
1977 data_offer_destroy(offer);
1978 }
1979}
1980
1981static void
1982data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1983 data_func_t func, void *user_data)
1984{
1985 int p[2];
1986
1987 pipe2(p, O_CLOEXEC);
1988 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1989 close(p[1]);
1990
1991 offer->io_task.run = offer_io_func;
1992 offer->fd = p[0];
1993 offer->func = func;
1994 offer->refcount++;
1995 offer->user_data = user_data;
1996
1997 display_watch_fd(offer->input->display,
1998 offer->fd, EPOLLIN, &offer->io_task);
1999}
2000
2001void
2002input_receive_drag_data(struct input *input, const char *mime_type,
2003 data_func_t func, void *data)
2004{
2005 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2006 input->drag_offer->x = input->sx;
2007 input->drag_offer->y = input->sy;
2008}
2009
2010int
2011input_receive_selection_data(struct input *input, const char *mime_type,
2012 data_func_t func, void *data)
2013{
2014 char **p;
2015
2016 if (input->selection_offer == NULL)
2017 return -1;
2018
2019 for (p = input->selection_offer->types.data; *p; p++)
2020 if (strcmp(mime_type, *p) == 0)
2021 break;
2022
2023 if (*p == NULL)
2024 return -1;
2025
2026 data_offer_receive_data(input->selection_offer,
2027 mime_type, func, data);
2028 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002029}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002030
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002031int
2032input_receive_selection_data_to_fd(struct input *input,
2033 const char *mime_type, int fd)
2034{
2035 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2036
2037 return 0;
2038}
2039
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002040void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002041window_move(struct window *window, struct input *input, uint32_t time)
2042{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002043 if (!window->shell_surface)
2044 return;
2045
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002046 wl_shell_surface_move(window->shell_surface,
2047 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002048}
2049
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002050static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002051idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002052{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002053 struct window *window =
2054 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002055 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002056
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002057 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002058 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002059 widget_set_allocation(widget,
2060 window->pending_allocation.x,
2061 window->pending_allocation.y,
2062 window->pending_allocation.width,
2063 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002064
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002065 if (window->input_region) {
2066 wl_region_destroy(window->input_region);
2067 window->input_region = NULL;
2068 }
2069
2070 if (window->opaque_region) {
2071 wl_region_destroy(window->opaque_region);
2072 window->opaque_region = NULL;
2073 }
2074
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002075 if (widget->resize_handler)
2076 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002077 widget->allocation.width,
2078 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002079 widget->user_data);
2080
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002081 if (window->allocation.width != widget->allocation.width ||
2082 window->allocation.height != widget->allocation.height) {
2083 window->allocation = widget->allocation;
2084 window_schedule_redraw(window);
2085 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002086}
2087
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002088void
2089window_schedule_resize(struct window *window, int width, int height)
2090{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002091 window->pending_allocation.x = 0;
2092 window->pending_allocation.y = 0;
2093 window->pending_allocation.width = width;
2094 window->pending_allocation.height = height;
2095
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002096 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002097 window->resize_task.run = idle_resize;
2098 display_defer(window->display, &window->resize_task);
2099 window->resize_scheduled = 1;
2100 }
2101}
2102
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002103void
2104widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2105{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002106 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002107}
2108
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002109static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002110handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002111 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002112 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002113{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002114 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002115
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002116 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002117 return;
2118
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002119 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002120 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002121}
2122
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002123static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002124menu_destroy(struct menu *menu)
2125{
2126 widget_destroy(menu->widget);
2127 window_destroy(menu->window);
2128 free(menu);
2129}
2130
2131static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002132handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2133{
2134 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002135 struct menu *menu = window->widget->user_data;
2136
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002137 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002138 * device. Or just use wl_callback. And this really needs to
2139 * be a window vfunc that the menu can set. And we need the
2140 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002141
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002142 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002143 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002144 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002145}
2146
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002147static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002148 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002149 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002150};
2151
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002152void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002153window_get_allocation(struct window *window,
2154 struct rectangle *allocation)
2155{
2156 *allocation = window->allocation;
2157}
2158
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002159static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002160widget_redraw(struct widget *widget)
2161{
2162 struct widget *child;
2163
2164 if (widget->redraw_handler)
2165 widget->redraw_handler(widget, widget->user_data);
2166 wl_list_for_each(child, &widget->child_list, link)
2167 widget_redraw(child);
2168}
2169
2170static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002171idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002172{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002173 struct window *window =
2174 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002175
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002176 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002177 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002178 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002179 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002180}
2181
2182void
2183window_schedule_redraw(struct window *window)
2184{
2185 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002186 window->redraw_task.run = idle_redraw;
2187 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002188 window->redraw_scheduled = 1;
2189 }
2190}
2191
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002192void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002193window_set_custom(struct window *window)
2194{
2195 window->type = TYPE_CUSTOM;
2196}
2197
2198void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002199window_set_fullscreen(struct window *window, int fullscreen)
2200{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002201 if (!window->display->shell)
2202 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002203
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002204 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002205 return;
2206
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002207 if (fullscreen) {
2208 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002209 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002210 wl_shell_surface_set_fullscreen(window->shell_surface,
2211 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2212 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002213 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002214 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002215 wl_shell_surface_set_toplevel(window->shell_surface);
2216 window_schedule_resize(window,
2217 window->saved_allocation.width,
2218 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002219 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002220}
2221
2222void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002223window_set_maximized(struct window *window, int maximized)
2224{
2225 if (!window->display->shell)
2226 return;
2227
2228 if ((window->type == TYPE_MAXIMIZED) == maximized)
2229 return;
2230
2231 if (window->type == TYPE_TOPLEVEL) {
2232 window->saved_allocation = window->allocation;
2233 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2234 window->type = TYPE_MAXIMIZED;
2235 } else {
2236 wl_shell_surface_set_toplevel(window->shell_surface);
2237 window->type = TYPE_TOPLEVEL;
2238 window_schedule_resize(window,
2239 window->saved_allocation.width,
2240 window->saved_allocation.height);
2241 }
2242}
2243
2244void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002245window_set_user_data(struct window *window, void *data)
2246{
2247 window->user_data = data;
2248}
2249
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002250void *
2251window_get_user_data(struct window *window)
2252{
2253 return window->user_data;
2254}
2255
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002256void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002257window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002258 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002259{
2260 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002261}
2262
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002263void
2264window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002265 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002266{
2267 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002268}
2269
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002270void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002271window_set_data_handler(struct window *window, window_data_handler_t handler)
2272{
2273 window->data_handler = handler;
2274}
2275
2276void
2277window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2278{
2279 window->drop_handler = handler;
2280}
2281
2282void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002283window_set_close_handler(struct window *window,
2284 window_close_handler_t handler)
2285{
2286 window->close_handler = handler;
2287}
2288
2289void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002290window_set_title(struct window *window, const char *title)
2291{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002292 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002293 window->title = strdup(title);
2294}
2295
2296const char *
2297window_get_title(struct window *window)
2298{
2299 return window->title;
2300}
2301
2302void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002303display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2304 int32_t x, int32_t y, int32_t width, int32_t height)
2305{
2306 struct wl_buffer *buffer;
2307
2308 buffer = display_get_buffer_for_surface(display, cairo_surface);
2309
2310 wl_buffer_damage(buffer, x, y, width, height);
2311}
2312
2313void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002314window_damage(struct window *window, int32_t x, int32_t y,
2315 int32_t width, int32_t height)
2316{
2317 wl_surface_damage(window->surface, x, y, width, height);
2318}
2319
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002320static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002321window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002322{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002323 struct window *window;
2324
2325 window = malloc(sizeof *window);
2326 if (window == NULL)
2327 return NULL;
2328
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002329 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002330 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002331 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002332 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002333 if (display->shell) {
2334 window->shell_surface =
2335 wl_shell_get_shell_surface(display->shell,
2336 window->surface);
2337 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002338 window->allocation.x = 0;
2339 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002340 window->allocation.width = 0;
2341 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002342 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002343 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002344 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002345 window->input_region = NULL;
2346 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002347
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002348 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002349#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002350 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2351 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002352#else
2353 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2354#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002355 else
2356 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002357
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002358 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002359 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002360
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002361 if (window->shell_surface) {
2362 wl_shell_surface_set_user_data(window->shell_surface, window);
2363 wl_shell_surface_add_listener(window->shell_surface,
2364 &shell_surface_listener, window);
2365 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002366
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002367 return window;
2368}
2369
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002370struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002371window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002372{
2373 struct window *window;
2374
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002375 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002376 if (!window)
2377 return NULL;
2378
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002379 window->type = TYPE_TOPLEVEL;
2380 if (display->shell)
2381 wl_shell_surface_set_toplevel(window->shell_surface);
2382
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002383 return window;
2384}
2385
2386struct window *
2387window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002388 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002389{
2390 struct window *window;
2391
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002392 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002393 if (!window)
2394 return NULL;
2395
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002396 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002397 window->x = x;
2398 window->y = y;
2399
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002400 if (display->shell)
2401 wl_shell_surface_set_transient(window->shell_surface,
2402 window->parent->shell_surface,
2403 window->x, window->y, 0);
2404
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002405 return window;
2406}
2407
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002408static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002409menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002410{
2411 int next;
2412
2413 next = (sy - 8) / 20;
2414 if (menu->current != next) {
2415 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002416 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002417 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418}
2419
2420static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002421menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002423 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002424{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002425 struct menu *menu = data;
2426
2427 if (widget == menu->widget)
2428 menu_set_item(data, y);
2429
2430 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002431}
2432
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002433static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002434menu_enter_handler(struct widget *widget,
2435 struct input *input, uint32_t time,
2436 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002437{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002438 struct menu *menu = data;
2439
2440 if (widget == menu->widget)
2441 menu_set_item(data, y);
2442
2443 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002444}
2445
2446static void
2447menu_leave_handler(struct widget *widget, struct input *input, void *data)
2448{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002449 struct menu *menu = data;
2450
2451 if (widget == menu->widget)
2452 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002453}
2454
2455static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002456menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002457 struct input *input, uint32_t time,
2458 int button, int state, void *data)
2459
2460{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002461 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002463 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002464 /* Either relase after press-drag-release or
2465 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002466 menu->func(menu->window->parent,
2467 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002468 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002469 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002470 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002471}
2472
2473static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002474menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002475{
2476 cairo_t *cr;
2477 const int32_t r = 3, margin = 3;
2478 struct menu *menu = data;
2479 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002480 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002481
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002482 cr = cairo_create(window->cairo_surface);
2483 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2484 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2485 cairo_paint(cr);
2486
2487 width = window->allocation.width;
2488 height = window->allocation.height;
2489 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002490
2491 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002492 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2493 cairo_fill(cr);
2494
2495 for (i = 0; i < menu->count; i++) {
2496 if (i == menu->current) {
2497 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2498 cairo_rectangle(cr, margin, i * 20 + margin,
2499 width - 2 * margin, 20);
2500 cairo_fill(cr);
2501 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2502 cairo_move_to(cr, 10, i * 20 + 16);
2503 cairo_show_text(cr, menu->entries[i]);
2504 } else {
2505 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2506 cairo_move_to(cr, 10, i * 20 + 16);
2507 cairo_show_text(cr, menu->entries[i]);
2508 }
2509 }
2510
2511 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002512}
2513
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002514void
2515window_show_menu(struct display *display,
2516 struct input *input, uint32_t time, struct window *parent,
2517 int32_t x, int32_t y,
2518 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002519{
2520 struct window *window;
2521 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002522 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002523
2524 menu = malloc(sizeof *menu);
2525 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002526 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002527
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002528 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002529 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002530 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002531
2532 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002533 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002534 menu->entries = entries;
2535 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002536 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002537 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002538 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002539 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002540 window->type = TYPE_MENU;
2541 window->x = x;
2542 window->y = y;
2543
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002544 wl_shell_surface_set_popup(window->shell_surface,
2545 input->input_device, time,
2546 window->parent->shell_surface,
2547 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002548
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002549 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002550 widget_set_enter_handler(menu->widget, menu_enter_handler);
2551 widget_set_leave_handler(menu->widget, menu_leave_handler);
2552 widget_set_motion_handler(menu->widget, menu_motion_handler);
2553 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002554
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002555 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002556 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002557}
2558
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002559void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002560window_set_buffer_type(struct window *window, enum window_buffer_type type)
2561{
2562 window->buffer_type = type;
2563}
2564
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002565
2566static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002567display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002568 struct wl_output *wl_output,
2569 int x, int y,
2570 int physical_width,
2571 int physical_height,
2572 int subpixel,
2573 const char *make,
2574 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002575{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002576 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002577
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002578 output->allocation.x = x;
2579 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002580}
2581
2582static void
2583display_handle_mode(void *data,
2584 struct wl_output *wl_output,
2585 uint32_t flags,
2586 int width,
2587 int height,
2588 int refresh)
2589{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002590 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002591 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002592
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002593 if (flags & WL_OUTPUT_MODE_CURRENT) {
2594 output->allocation.width = width;
2595 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002596 if (display->output_configure_handler)
2597 (*display->output_configure_handler)(
2598 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002599 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002600}
2601
2602static const struct wl_output_listener output_listener = {
2603 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002604 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002605};
2606
2607static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002608display_add_output(struct display *d, uint32_t id)
2609{
2610 struct output *output;
2611
2612 output = malloc(sizeof *output);
2613 if (output == NULL)
2614 return;
2615
2616 memset(output, 0, sizeof *output);
2617 output->display = d;
2618 output->output =
2619 wl_display_bind(d->display, id, &wl_output_interface);
2620 wl_list_insert(d->output_list.prev, &output->link);
2621
2622 wl_output_add_listener(output->output, &output_listener, output);
2623}
2624
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002625static void
2626output_destroy(struct output *output)
2627{
2628 if (output->destroy_handler)
2629 (*output->destroy_handler)(output, output->user_data);
2630
2631 wl_output_destroy(output->output);
2632 wl_list_remove(&output->link);
2633 free(output);
2634}
2635
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002636void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002637display_set_output_configure_handler(struct display *display,
2638 display_output_handler_t handler)
2639{
2640 struct output *output;
2641
2642 display->output_configure_handler = handler;
2643 if (!handler)
2644 return;
2645
2646 wl_list_for_each(output, &display->output_list, link)
2647 (*display->output_configure_handler)(output,
2648 display->user_data);
2649}
2650
2651void
2652output_set_user_data(struct output *output, void *data)
2653{
2654 output->user_data = data;
2655}
2656
2657void *
2658output_get_user_data(struct output *output)
2659{
2660 return output->user_data;
2661}
2662
2663void
2664output_set_destroy_handler(struct output *output,
2665 display_output_handler_t handler)
2666{
2667 output->destroy_handler = handler;
2668 /* FIXME: implement this, once we have way to remove outputs */
2669}
2670
2671void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002672output_get_allocation(struct output *output, struct rectangle *allocation)
2673{
2674 *allocation = output->allocation;
2675}
2676
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002677struct wl_output *
2678output_get_wl_output(struct output *output)
2679{
2680 return output->output;
2681}
2682
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002683static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002684display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002685{
2686 struct input *input;
2687
2688 input = malloc(sizeof *input);
2689 if (input == NULL)
2690 return;
2691
2692 memset(input, 0, sizeof *input);
2693 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002694 input->input_device =
2695 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002696 input->pointer_focus = NULL;
2697 input->keyboard_focus = NULL;
2698 wl_list_insert(d->input_list.prev, &input->link);
2699
2700 wl_input_device_add_listener(input->input_device,
2701 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002702 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002703
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002704 input->data_device =
2705 wl_data_device_manager_get_data_device(d->data_device_manager,
2706 input->input_device);
2707 wl_data_device_add_listener(input->data_device,
2708 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002709}
2710
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002711static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002712input_destroy(struct input *input)
2713{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002714 input_remove_keyboard_focus(input);
2715 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002716
2717 if (input->drag_offer)
2718 data_offer_destroy(input->drag_offer);
2719
2720 if (input->selection_offer)
2721 data_offer_destroy(input->selection_offer);
2722
2723 wl_data_device_destroy(input->data_device);
2724 wl_list_remove(&input->link);
2725 wl_input_device_destroy(input->input_device);
2726 free(input);
2727}
2728
2729static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002730display_handle_global(struct wl_display *display, uint32_t id,
2731 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002732{
2733 struct display *d = data;
2734
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002735 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002736 d->compositor =
2737 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002738 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002739 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002740 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002741 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002742 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002743 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002744 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002745 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002746 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2747 d->data_device_manager =
2748 wl_display_bind(display, id,
2749 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002750 }
2751}
2752
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002753static void
2754display_render_frame(struct display *d)
2755{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002756 cairo_t *cr;
2757
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002758 d->frame_radius = 8;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002759 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2760 cr = cairo_create(d->shadow);
2761 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2762 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002763 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002764 cairo_fill(cr);
2765 cairo_destroy(cr);
2766 blur_surface(d->shadow, 64);
2767
2768 d->active_frame =
2769 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2770 cr = cairo_create(d->active_frame);
2771 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2772 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002773 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002774 cairo_fill(cr);
2775 cairo_destroy(cr);
2776
2777 d->inactive_frame =
2778 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2779 cr = cairo_create(d->inactive_frame);
2780 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2781 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002782 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002783 cairo_fill(cr);
2784 cairo_destroy(cr);
2785}
2786
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002787static void
2788init_xkb(struct display *d)
2789{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002790 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002791
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002792 names.rules = "evdev";
2793 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002794 names.layout = option_xkb_layout;
2795 names.variant = option_xkb_variant;
2796 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002797
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002798 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002799 if (!d->xkb) {
2800 fprintf(stderr, "Failed to compile keymap\n");
2801 exit(1);
2802 }
2803}
2804
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002805static void
2806fini_xkb(struct display *display)
2807{
2808 xkb_free_keymap(display->xkb);
2809}
2810
Yuval Fledel45568f62010-12-06 09:18:12 -05002811static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002812init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002813{
2814 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002815 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002816
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002817 static const EGLint argb_cfg_attribs[] = {
2818 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002819 EGL_RED_SIZE, 1,
2820 EGL_GREEN_SIZE, 1,
2821 EGL_BLUE_SIZE, 1,
2822 EGL_ALPHA_SIZE, 1,
2823 EGL_DEPTH_SIZE, 1,
2824 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2825 EGL_NONE
2826 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002827
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002828#ifdef USE_CAIRO_GLESV2
2829 static const EGLint context_attribs[] = {
2830 EGL_CONTEXT_CLIENT_VERSION, 2,
2831 EGL_NONE
2832 };
2833 EGLint api = EGL_OPENGL_ES_API;
2834#else
2835 EGLint *context_attribs = NULL;
2836 EGLint api = EGL_OPENGL_API;
2837#endif
2838
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002839 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002840 if (!eglInitialize(d->dpy, &major, &minor)) {
2841 fprintf(stderr, "failed to initialize display\n");
2842 return -1;
2843 }
2844
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002845 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002846 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2847 return -1;
2848 }
2849
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002850 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2851 &d->argb_config, 1, &n) || n != 1) {
2852 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002853 return -1;
2854 }
2855
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002856 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002857 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002858 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002859 fprintf(stderr, "failed to create context\n");
2860 return -1;
2861 }
2862
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002863 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002864 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002865 return -1;
2866 }
2867
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002868#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002869 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2870 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2871 fprintf(stderr, "failed to get cairo egl argb device\n");
2872 return -1;
2873 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002874#endif
2875
2876 return 0;
2877}
2878
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002879static void
2880fini_egl(struct display *display)
2881{
2882#ifdef HAVE_CAIRO_EGL
2883 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002884#endif
2885
2886 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2887 EGL_NO_CONTEXT);
2888
2889 eglTerminate(display->dpy);
2890 eglReleaseThread();
2891}
2892
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002893static int
2894event_mask_update(uint32_t mask, void *data)
2895{
2896 struct display *d = data;
2897
2898 d->mask = mask;
2899
2900 return 0;
2901}
2902
2903static void
2904handle_display_data(struct task *task, uint32_t events)
2905{
2906 struct display *display =
2907 container_of(task, struct display, display_task);
2908
2909 wl_display_iterate(display->display, display->mask);
2910}
2911
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002912struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002913display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002914{
2915 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002916 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002917 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002918 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002919
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002920 g_type_init();
2921
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002922 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002923 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002924 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002925
2926 xkb_option_group = g_option_group_new("xkb",
2927 "Keyboard options",
2928 "Show all XKB options",
2929 NULL, NULL);
2930 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2931 g_option_context_add_group (context, xkb_option_group);
2932
2933 if (!g_option_context_parse(context, argc, argv, &error)) {
2934 fprintf(stderr, "option parsing failed: %s\n", error->message);
2935 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002936 }
2937
Tim Wiederhake748f6722011-01-23 23:25:25 +01002938 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002939
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002940 d = malloc(sizeof *d);
2941 if (d == NULL)
2942 return NULL;
2943
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002944 memset(d, 0, sizeof *d);
2945
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002946 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002947 if (d->display == NULL) {
2948 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002949 return NULL;
2950 }
2951
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002952 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2953 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2954 d->display_task.run = handle_display_data;
2955 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2956
2957 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002958 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002959 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002960
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002961 /* Set up listener so we'll catch all events. */
2962 wl_display_add_global_listener(d->display,
2963 display_handle_global, d);
2964
2965 /* Process connection events. */
2966 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002967 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002968 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002969
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002970 d->image_target_texture_2d =
2971 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2972 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2973 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2974
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002975 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002976
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002977 display_render_frame(d);
2978
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002979 wl_list_init(&d->window_list);
2980
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002981 init_xkb(d);
2982
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002983 return d;
2984}
2985
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002986static void
2987display_destroy_outputs(struct display *display)
2988{
2989 struct output *tmp;
2990 struct output *output;
2991
2992 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2993 output_destroy(output);
2994}
2995
Pekka Paalanene1207c72011-12-16 12:02:09 +02002996static void
2997display_destroy_inputs(struct display *display)
2998{
2999 struct input *tmp;
3000 struct input *input;
3001
3002 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3003 input_destroy(input);
3004}
3005
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003006void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003007display_destroy(struct display *display)
3008{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003009 if (!wl_list_empty(&display->window_list))
3010 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3011
3012 if (!wl_list_empty(&display->deferred_list))
3013 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3014
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003015 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003016 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003017
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003018 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003019
3020 cairo_surface_destroy(display->active_frame);
3021 cairo_surface_destroy(display->inactive_frame);
3022 cairo_surface_destroy(display->shadow);
3023 destroy_pointer_surfaces(display);
3024
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003025 fini_egl(display);
3026
Pekka Paalanenc2052982011-12-16 11:41:32 +02003027 if (display->shell)
3028 wl_shell_destroy(display->shell);
3029
3030 if (display->shm)
3031 wl_shm_destroy(display->shm);
3032
3033 if (display->data_device_manager)
3034 wl_data_device_manager_destroy(display->data_device_manager);
3035
3036 wl_compositor_destroy(display->compositor);
3037
3038 close(display->epoll_fd);
3039
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003040 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003041 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003042 free(display);
3043}
3044
3045void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003046display_set_user_data(struct display *display, void *data)
3047{
3048 display->user_data = data;
3049}
3050
3051void *
3052display_get_user_data(struct display *display)
3053{
3054 return display->user_data;
3055}
3056
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003057struct wl_display *
3058display_get_display(struct display *display)
3059{
3060 return display->display;
3061}
3062
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003063struct output *
3064display_get_output(struct display *display)
3065{
3066 return container_of(display->output_list.next, struct output, link);
3067}
3068
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003069struct wl_compositor *
3070display_get_compositor(struct display *display)
3071{
3072 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003073}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003074
3075EGLDisplay
3076display_get_egl_display(struct display *d)
3077{
3078 return d->dpy;
3079}
3080
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003081struct wl_data_source *
3082display_create_data_source(struct display *display)
3083{
3084 return wl_data_device_manager_create_data_source(display->data_device_manager);
3085}
3086
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003087EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003088display_get_argb_egl_config(struct display *d)
3089{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003090 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003091}
3092
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003093struct wl_shell *
3094display_get_shell(struct display *display)
3095{
3096 return display->shell;
3097}
3098
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003099int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003100display_acquire_window_surface(struct display *display,
3101 struct window *window,
3102 EGLContext ctx)
3103{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003104#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003105 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003106 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003107
3108 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003109 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003110 device = cairo_surface_get_device(window->cairo_surface);
3111 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003112 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003113
Benjamin Franzke0c991632011-09-27 21:57:31 +02003114 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003115 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003116 ctx = display->argb_ctx;
3117 else
3118 assert(0);
3119 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003120
3121 data = cairo_surface_get_user_data(window->cairo_surface,
3122 &surface_data_key);
3123
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003124 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003125 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003126 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3127 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003128
3129 return 0;
3130#else
3131 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003132#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003133}
3134
3135void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003136display_release_window_surface(struct display *display,
3137 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003138{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003139#ifdef HAVE_CAIRO_EGL
3140 cairo_device_t *device;
3141
3142 device = cairo_surface_get_device(window->cairo_surface);
3143 if (!device)
3144 return;
3145
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003146 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003147 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003148 cairo_device_release(device);
3149#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003150}
3151
3152void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003153display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003154{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003155 wl_list_insert(&display->deferred_list, &task->link);
3156}
3157
3158void
3159display_watch_fd(struct display *display,
3160 int fd, uint32_t events, struct task *task)
3161{
3162 struct epoll_event ep;
3163
3164 ep.events = events;
3165 ep.data.ptr = task;
3166 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3167}
3168
3169void
3170display_run(struct display *display)
3171{
3172 struct task *task;
3173 struct epoll_event ep[16];
3174 int i, count;
3175
Pekka Paalanen826d7952011-12-15 10:14:07 +02003176 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003177 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003178 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003179
3180 while (!wl_list_empty(&display->deferred_list)) {
3181 task = container_of(display->deferred_list.next,
3182 struct task, link);
3183 wl_list_remove(&task->link);
3184 task->run(task, 0);
3185 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003186
3187 if (!display->running)
3188 break;
3189
3190 wl_display_flush(display->display);
3191
3192 count = epoll_wait(display->epoll_fd,
3193 ep, ARRAY_LENGTH(ep), -1);
3194 for (i = 0; i < count; i++) {
3195 task = ep[i].data.ptr;
3196 task->run(task, ep[i].events);
3197 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003198 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003199}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003200
3201void
3202display_exit(struct display *display)
3203{
3204 display->running = 0;
3205}