blob: 0dfd7f30fd2365b361eea0a2e5cb8449a9adeb7d [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
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/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øgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050069 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Pekka Paalanen826d7952011-12-15 10:14:07 +020082 int running;
83
Kristian Høgsberg478d9262010-06-08 20:34:11 -040084 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040085 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050086 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040087 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040088 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040089 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040090
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050091 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
92 PFNEGLCREATEIMAGEKHRPROC create_image;
93 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020094
95 display_output_handler_t output_configure_handler;
96
97 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050098};
99
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400101 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400102 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500103 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500105 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400106 TYPE_CUSTOM
107};
108
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500109struct window {
110 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500111 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500112 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200113 struct wl_shell_surface *shell_surface;
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;
155};
156
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400157struct input {
158 struct display *display;
159 struct wl_input_device *input_device;
160 struct window *pointer_focus;
161 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400162 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400163 uint32_t modifiers;
164 int32_t x, y, sx, sy;
165 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400166
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500167 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500168 struct widget *grab;
169 uint32_t grab_button;
170
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400171 struct wl_data_device *data_device;
172 struct data_offer *drag_offer;
173 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174};
175
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500176struct output {
177 struct display *display;
178 struct wl_output *output;
179 struct rectangle allocation;
180 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200181
182 display_output_handler_t destroy_handler;
183 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500184};
185
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500186struct frame {
187 struct widget *widget;
188 struct widget *child;
189 int margin;
190};
191
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500192struct menu {
193 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500194 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500195 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500196 const char **entries;
197 uint32_t time;
198 int current;
199 int count;
200 menu_func_t func;
201};
202
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400203enum {
204 POINTER_DEFAULT = 100,
205 POINTER_UNSET
206};
207
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500208enum window_location {
209 WINDOW_INTERIOR = 0,
210 WINDOW_RESIZING_TOP = 1,
211 WINDOW_RESIZING_BOTTOM = 2,
212 WINDOW_RESIZING_LEFT = 4,
213 WINDOW_RESIZING_TOP_LEFT = 5,
214 WINDOW_RESIZING_BOTTOM_LEFT = 6,
215 WINDOW_RESIZING_RIGHT = 8,
216 WINDOW_RESIZING_TOP_RIGHT = 9,
217 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
218 WINDOW_RESIZING_MASK = 15,
219 WINDOW_EXTERIOR = 16,
220 WINDOW_TITLEBAR = 17,
221 WINDOW_CLIENT_AREA = 18,
222};
223
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400224const char *option_xkb_layout = "us";
225const char *option_xkb_variant = "";
226const char *option_xkb_options = "";
227
228static const GOptionEntry xkb_option_entries[] = {
229 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
230 &option_xkb_layout, "XKB Layout" },
231 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
232 &option_xkb_variant, "XKB Variant" },
233 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
234 &option_xkb_options, "XKB Options" },
235 { NULL }
236};
237
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400238static const cairo_user_data_key_t surface_data_key;
239struct surface_data {
240 struct wl_buffer *buffer;
241};
242
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500243#define MULT(_d,c,a,t) \
244 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
245
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500246#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100248struct egl_window_surface_data {
249 struct display *display;
250 struct wl_surface *surface;
251 struct wl_egl_window *window;
252 EGLSurface surf;
253};
254
255static void
256egl_window_surface_data_destroy(void *p)
257{
258 struct egl_window_surface_data *data = p;
259 struct display *d = data->display;
260
261 eglDestroySurface(d->dpy, data->surf);
262 wl_egl_window_destroy(data->window);
263 data->surface = NULL;
264
265 free(p);
266}
267
268static cairo_surface_t *
269display_create_egl_window_surface(struct display *display,
270 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400271 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272 struct rectangle *rectangle)
273{
274 cairo_surface_t *cairo_surface;
275 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400276 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200277 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278
279 data = malloc(sizeof *data);
280 if (data == NULL)
281 return NULL;
282
283 data->display = display;
284 data->surface = surface;
285
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 if (flags & SURFACE_OPAQUE) {
287 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500290 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400292 }
293
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400294 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500299 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300
Benjamin Franzke0c991632011-09-27 21:57:31 +0200301 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 data->surf,
303 rectangle->width,
304 rectangle->height);
305
306 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
307 data, egl_window_surface_data_destroy);
308
309 return cairo_surface;
310}
311
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500312struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200314 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400315 EGLImageKHR image;
316 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800317 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500318 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319};
320
321static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400323{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500324 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800325 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200329 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500331 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500333 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500334 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335}
336
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338display_get_image_for_egl_image_surface(struct display *display,
339 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342
343 data = cairo_surface_get_user_data (surface, &surface_data_key);
344
345 return data->image;
346}
347
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500348static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400350 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 EGLDisplay dpy = display->dpy;
355 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200356 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357
358 data = malloc(sizeof *data);
359 if (data == NULL)
360 return NULL;
361
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800362 data->display = display;
363
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400364 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400365 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500366 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000367 free(data);
368 return NULL;
369 }
370
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 if (flags & SURFACE_OPAQUE) {
372 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR;
374 } else {
375 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 content = CAIRO_CONTENT_COLOR_ALPHA;
377 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400378
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500379 data->image = display->create_image(dpy, NULL,
380 EGL_NATIVE_PIXMAP_KHR,
381 (EGLClientBuffer) data->pixmap,
382 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500384 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500385 free(data);
386 return NULL;
387 }
388
389 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400390 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500391
Benjamin Franzke0c991632011-09-27 21:57:31 +0200392 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500395 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397
Benjamin Franzke0c991632011-09-27 21:57:31 +0200398 surface = cairo_gl_surface_create_for_texture(data->device,
399 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 data->texture,
401 rectangle->width,
402 rectangle->height);
403
404 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500405 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406
407 return surface;
408}
409
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500410static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500411display_create_egl_image_surface_from_file(struct display *display,
412 const char *filename,
413 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400414{
415 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400416 GdkPixbuf *pixbuf;
417 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400418 int stride, i;
419 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500420 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421
422 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424 FALSE, &error);
425 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400426 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400427
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400428 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
429 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500430 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400433
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400434
435 stride = gdk_pixbuf_get_rowstride(pixbuf);
436 pixels = gdk_pixbuf_get_pixels(pixbuf);
437
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400440 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 while (p < end) {
442 unsigned int t;
443
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400444 MULT(p[0], p[0], p[3], t);
445 MULT(p[1], p[1], p[3], t);
446 MULT(p[2], p[2], p[3], t);
447 p += 4;
448
449 }
450 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400451
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200452 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000453 if (surface == NULL) {
454 g_object_unref(pixbuf);
455 return NULL;
456 }
457
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 data = cairo_surface_get_user_data(surface, &surface_data_key);
459
Benjamin Franzke0c991632011-09-27 21:57:31 +0200460 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800461 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800462 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
463 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200464 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500466 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400467
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400468 return surface;
469}
470
471#endif
472
473struct wl_buffer *
474display_get_buffer_for_surface(struct display *display,
475 cairo_surface_t *surface)
476{
477 struct surface_data *data;
478
479 data = cairo_surface_get_user_data (surface, &surface_data_key);
480
481 return data->buffer;
482}
483
484struct shm_surface_data {
485 struct surface_data data;
486 void *map;
487 size_t length;
488};
489
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500490static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491shm_surface_data_destroy(void *p)
492{
493 struct shm_surface_data *data = p;
494
495 wl_buffer_destroy(data->data.buffer);
496 munmap(data->map, data->length);
497}
498
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500499static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400501 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400502{
503 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400504 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400505 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800506 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507 char filename[] = "/tmp/wayland-shm-XXXXXX";
508
509 data = malloc(sizeof *data);
510 if (data == NULL)
511 return NULL;
512
513 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
514 rectangle->width);
515 data->length = stride * rectangle->height;
516 fd = mkstemp(filename);
517 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000518 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400519 return NULL;
520 }
521 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000522 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 close(fd);
524 return NULL;
525 }
526
527 data->map = mmap(NULL, data->length,
528 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
529 unlink(filename);
530
531 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000532 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400533 close(fd);
534 return NULL;
535 }
536
537 surface = cairo_image_surface_create_for_data (data->map,
538 CAIRO_FORMAT_ARGB32,
539 rectangle->width,
540 rectangle->height,
541 stride);
542
543 cairo_surface_set_user_data (surface, &surface_data_key,
544 data, shm_surface_data_destroy);
545
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500547 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500549 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 data->data.buffer = wl_shm_create_buffer(display->shm,
552 fd,
553 rectangle->width,
554 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 close(fd);
558
559 return surface;
560}
561
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500562static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563display_create_shm_surface_from_file(struct display *display,
564 const char *filename,
565 struct rectangle *rect)
566{
567 cairo_surface_t *surface;
568 GdkPixbuf *pixbuf;
569 GError *error = NULL;
570 int stride, i;
571 unsigned char *pixels, *p, *end, *dest_data;
572 int dest_stride;
573 uint32_t *d;
574
575 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
576 rect->width, rect->height,
577 FALSE, &error);
578 if (error != NULL)
579 return NULL;
580
581 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
582 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500583 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 return NULL;
585 }
586
587 stride = gdk_pixbuf_get_rowstride(pixbuf);
588 pixels = gdk_pixbuf_get_pixels(pixbuf);
589
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400590 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000591 if (surface == NULL) {
592 g_object_unref(pixbuf);
593 return NULL;
594 }
595
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596 dest_data = cairo_image_surface_get_data (surface);
597 dest_stride = cairo_image_surface_get_stride (surface);
598
599 for (i = 0; i < rect->height; i++) {
600 d = (uint32_t *) (dest_data + i * dest_stride);
601 p = pixels + i * stride;
602 end = p + rect->width * 4;
603 while (p < end) {
604 unsigned int t;
605 unsigned char a, r, g, b;
606
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607 a = p[3];
608 MULT(r, p[0], a, t);
609 MULT(g, p[1], a, t);
610 MULT(b, p[2], a, t);
611 p += 4;
612 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
613 }
614 }
615
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500616 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617
618 return surface;
619}
620
nobled7b87cb02011-02-01 18:51:47 +0000621static int
622check_size(struct rectangle *rect)
623{
624 if (rect->width && rect->height)
625 return 0;
626
627 fprintf(stderr, "tried to create surface of "
628 "width: %d, height: %d\n", rect->width, rect->height);
629 return -1;
630}
631
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632cairo_surface_t *
633display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400635 struct rectangle *rectangle,
636 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637{
nobled7b87cb02011-02-01 18:51:47 +0000638 if (check_size(rectangle) < 0)
639 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500641 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 if (surface)
643 return display_create_egl_window_surface(display,
644 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 rectangle);
647 else
648 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400649 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500651 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400653 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654}
655
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500656static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657display_create_surface_from_file(struct display *display,
658 const char *filename,
659 struct rectangle *rectangle)
660{
nobled7b87cb02011-02-01 18:51:47 +0000661 if (check_size(rectangle) < 0)
662 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500663#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500664 if (display->dpy) {
665 return display_create_egl_image_surface_from_file(display,
666 filename,
667 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671}
Yuval Fledel45568f62010-12-06 09:18:12 -0500672 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673 const char *filename;
674 int hotspot_x, hotspot_y;
675} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500676 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
677 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
678 { DATADIR "/weston/bottom_side.png", 16, 20 },
679 { DATADIR "/weston/grabbing.png", 20, 17 },
680 { DATADIR "/weston/left_ptr.png", 10, 5 },
681 { DATADIR "/weston/left_side.png", 10, 20 },
682 { DATADIR "/weston/right_side.png", 30, 19 },
683 { DATADIR "/weston/top_left_corner.png", 8, 8 },
684 { DATADIR "/weston/top_right_corner.png", 26, 8 },
685 { DATADIR "/weston/top_side.png", 18, 8 },
686 { DATADIR "/weston/xterm.png", 15, 15 },
687 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688};
689
690static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692{
693 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400698 display->pointer_surfaces =
699 malloc(count * sizeof *display->pointer_surfaces);
700 rect.width = width;
701 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 display_create_surface_from_file(display,
705 pointer_images[i].filename,
706 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100707 if (!display->pointer_surfaces[i]) {
708 fprintf(stderr, "Error loading pointer image: %s\n",
709 pointer_images[i].filename);
710 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400711 }
712
713}
714
Pekka Paalanen325bb602011-12-19 10:31:45 +0200715static void
716destroy_pointer_surfaces(struct display *display)
717{
718 int i, count;
719
720 count = ARRAY_LENGTH(pointer_images);
721 for (i = 0; i < count; ++i) {
722 if (display->pointer_surfaces[i])
723 cairo_surface_destroy(display->pointer_surfaces[i]);
724 }
725 free(display->pointer_surfaces);
726}
727
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728cairo_surface_t *
729display_get_pointer_surface(struct display *display, int pointer,
730 int *width, int *height,
731 int *hotspot_x, int *hotspot_y)
732{
733 cairo_surface_t *surface;
734
735 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500736#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400737 *width = cairo_gl_surface_get_width(surface);
738 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000739#else
740 *width = cairo_image_surface_get_width(surface);
741 *height = cairo_image_surface_get_height(surface);
742#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400743 *hotspot_x = pointer_images[pointer].hotspot_x;
744 *hotspot_y = pointer_images[pointer].hotspot_y;
745
746 return cairo_surface_reference(surface);
747}
748
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400749static void
750window_attach_surface(struct window *window);
751
752static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754{
755 struct window *window = data;
756
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758 cairo_surface_destroy(window->pending_surface);
759 window->pending_surface = NULL;
760 if (window->cairo_surface)
761 window_attach_surface(window);
762}
763
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400764static const struct wl_callback_listener free_surface_listener = {
765 free_surface
766};
767
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500768static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200769window_get_resize_dx_dy(struct window *window, int *x, int *y)
770{
771 if (window->resize_edges & WINDOW_RESIZING_LEFT)
772 *x = window->server_allocation.width - window->allocation.width;
773 else
774 *x = 0;
775
776 if (window->resize_edges & WINDOW_RESIZING_TOP)
777 *y = window->server_allocation.height -
778 window->allocation.height;
779 else
780 *y = 0;
781
782 window->resize_edges = 0;
783}
784
785static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400786window_set_type(struct window *window)
787{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200788 if (!window->shell_surface)
789 return;
790
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 switch (window->type) {
792 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500795 case TYPE_MAXIMIZED:
796 wl_shell_surface_set_maximized(window->shell_surface, NULL);
797 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400798 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200799 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400800 break;
801 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200802 wl_shell_surface_set_transient(window->shell_surface,
803 window->parent->shell_surface,
804 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400805 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500806 case TYPE_MENU:
807 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400808 case TYPE_CUSTOM:
809 break;
810 }
811}
812
813static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500814window_attach_surface(struct window *window)
815{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400816 struct display *display = window->display;
817 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400818 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000819#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000821#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500822 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400824 if (display->shell)
825 window_set_type(window);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000828#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
830 data = cairo_surface_get_user_data(window->cairo_surface,
831 &surface_data_key);
832
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 cairo_gl_surface_swapbuffers(window->cairo_surface);
834 wl_egl_window_get_attached_size(data->window,
835 &window->server_allocation.width,
836 &window->server_allocation.height);
837 break;
838 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000839#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840 case WINDOW_BUFFER_TYPE_SHM:
841 if (window->pending_surface != NULL)
842 return;
843
844 window->pending_surface = window->cairo_surface;
845 window->cairo_surface = NULL;
846
847 buffer =
848 display_get_buffer_for_surface(display,
849 window->pending_surface);
850
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200851 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 wl_surface_attach(window->surface, buffer, x, y);
853 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400854 cb = wl_display_sync(display->display);
855 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000857 default:
858 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500860
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500861 wl_surface_damage(window->surface, 0, 0,
862 window->allocation.width,
863 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500864}
865
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500866void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400867window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500868{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100869 if (window->cairo_surface) {
870 switch (window->buffer_type) {
871 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
872 case WINDOW_BUFFER_TYPE_SHM:
873 display_surface_damage(window->display,
874 window->cairo_surface,
875 0, 0,
876 window->allocation.width,
877 window->allocation.height);
878 break;
879 default:
880 break;
881 }
882 window_attach_surface(window);
883 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500884}
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886void
887window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400888{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 cairo_surface_reference(surface);
890
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891 if (window->cairo_surface != NULL)
892 cairo_surface_destroy(window->cairo_surface);
893
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500894 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400895}
896
Benjamin Franzke22d54812011-07-16 19:50:32 +0000897#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898static void
899window_resize_cairo_window_surface(struct window *window)
900{
901 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903
904 data = cairo_surface_get_user_data(window->cairo_surface,
905 &surface_data_key);
906
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 wl_egl_window_resize(data->window,
909 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200910 window->allocation.height,
911 x,y);
912
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913 cairo_gl_surface_set_size(window->cairo_surface,
914 window->allocation.width,
915 window->allocation.height);
916}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000917#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100918
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400919struct display *
920window_get_display(struct window *window)
921{
922 return window->display;
923}
924
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400925void
926window_create_surface(struct window *window)
927{
928 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400929 uint32_t flags = 0;
930
931 if (!window->transparent)
932 flags = SURFACE_OPAQUE;
933
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400934 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500935#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100936 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
937 if (window->cairo_surface) {
938 window_resize_cairo_window_surface(window);
939 return;
940 }
941 surface = display_create_surface(window->display,
942 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500945 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400946 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100947 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400948 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949 break;
950#endif
951 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400952 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400953 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800955 default:
956 surface = NULL;
957 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400958 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400959
960 window_set_surface(window, surface);
961 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400962}
963
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200964static void frame_destroy(struct frame *frame);
965
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400966void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500967window_destroy(struct window *window)
968{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200969 struct display *display = window->display;
970 struct input *input;
971
972 if (window->redraw_scheduled)
973 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500974 if (window->resize_scheduled)
975 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200976
977 wl_list_for_each(input, &display->input_list, link) {
978 if (input->pointer_focus == window)
979 input->pointer_focus = NULL;
980 if (input->keyboard_focus == window)
981 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500982 if (input->focus_widget &&
983 input->focus_widget->window == window)
984 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200985 }
986
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200987 if (window->frame)
988 frame_destroy(window->frame);
989
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200990 if (window->shell_surface)
991 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500992 wl_surface_destroy(window->surface);
993 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200994
995 if (window->cairo_surface != NULL)
996 cairo_surface_destroy(window->cairo_surface);
997 if (window->pending_surface != NULL)
998 cairo_surface_destroy(window->pending_surface);
999
1000 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001001 free(window);
1002}
1003
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001004static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001006{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001007 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001009 wl_list_for_each(child, &widget->child_list, link) {
1010 target = widget_find_widget(child, x, y);
1011 if (target)
1012 return target;
1013 }
1014
1015 if (widget->allocation.x <= x &&
1016 x < widget->allocation.x + widget->allocation.width &&
1017 widget->allocation.y <= y &&
1018 y < widget->allocation.y + widget->allocation.height) {
1019 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001020 }
1021
1022 return NULL;
1023}
1024
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001025static struct widget *
1026widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001029
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001030 widget = malloc(sizeof *widget);
1031 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001032 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001033 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001034 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001035 wl_list_init(&widget->child_list);
1036
1037 return widget;
1038}
1039
1040struct widget *
1041window_add_widget(struct window *window, void *data)
1042{
1043 window->widget = widget_create(window, data);
1044 wl_list_init(&window->widget->link);
1045
1046 return window->widget;
1047}
1048
1049struct widget *
1050widget_add_widget(struct widget *parent, void *data)
1051{
1052 struct widget *widget;
1053
1054 widget = widget_create(parent->window, data);
1055 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001057 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058}
1059
1060void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001061widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001063 struct display *display = widget->window->display;
1064 struct input *input;
1065
1066 wl_list_for_each(input, &display->input_list, link) {
1067 if (input->focus_widget == widget)
1068 input->focus_widget = NULL;
1069 }
1070
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001071 wl_list_remove(&widget->link);
1072 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073}
1074
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001076widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001078 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001079}
1080
1081void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001082widget_set_size(struct widget *widget, int32_t width, int32_t height)
1083{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001084 widget->allocation.width = width;
1085 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001086}
1087
1088void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_set_allocation(struct widget *widget,
1090 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 widget->allocation.x = x;
1093 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001094 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095}
1096
1097void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101}
1102
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001103void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001104widget_set_resize_handler(struct widget *widget,
1105 widget_resize_handler_t handler)
1106{
1107 widget->resize_handler = handler;
1108}
1109
1110void
1111widget_set_redraw_handler(struct widget *widget,
1112 widget_redraw_handler_t handler)
1113{
1114 widget->redraw_handler = handler;
1115}
1116
1117void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001119{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001120 widget->enter_handler = handler;
1121}
1122
1123void
1124widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1125{
1126 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001127}
1128
1129void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001130widget_set_motion_handler(struct widget *widget,
1131 widget_motion_handler_t handler)
1132{
1133 widget->motion_handler = handler;
1134}
1135
1136void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001137widget_set_button_handler(struct widget *widget,
1138 widget_button_handler_t handler)
1139{
1140 widget->button_handler = handler;
1141}
1142
1143void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001144widget_schedule_redraw(struct widget *widget)
1145{
1146 window_schedule_redraw(widget->window);
1147}
1148
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001149cairo_surface_t *
1150window_get_surface(struct window *window)
1151{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001152 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001153}
1154
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001155struct wl_surface *
1156window_get_wl_surface(struct window *window)
1157{
1158 return window->surface;
1159}
1160
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161struct wl_shell_surface *
1162window_get_wl_shell_surface(struct window *window)
1163{
1164 return window->shell_surface;
1165}
1166
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167static void
1168frame_resize_handler(struct widget *widget,
1169 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001170{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001171 struct frame *frame = data;
1172 struct widget *child = frame->child;
1173 struct rectangle allocation;
1174 int decoration_width, decoration_height;
1175
1176 decoration_width = 20 + frame->margin * 2;
1177 decoration_height = 60 + frame->margin * 2;
1178
1179 allocation.x = 10 + frame->margin;
1180 allocation.y = 50 + frame->margin;
1181 allocation.width = width - decoration_width;
1182 allocation.height = height - decoration_height;
1183
1184 widget_set_allocation(child, allocation.x, allocation.y,
1185 allocation.width, allocation.height);
1186
1187 if (child->resize_handler)
1188 child->resize_handler(child,
1189 allocation.width,
1190 allocation.height,
1191 child->user_data);
1192
1193 widget_set_allocation(widget, 0, 0,
1194 child->allocation.width + decoration_width,
1195 child->allocation.height + decoration_height);
1196}
1197
1198static void
1199frame_redraw_handler(struct widget *widget, void *data)
1200{
1201 struct frame *frame = data;
1202 cairo_t *cr;
1203 cairo_text_extents_t extents;
1204 cairo_surface_t *source;
1205 int width, height, shadow_dx = 3, shadow_dy = 3;
1206 struct window *window = widget->window;
1207
1208 width = widget->allocation.width;
1209 height = widget->allocation.height;
1210
1211 cr = cairo_create(window->cairo_surface);
1212
1213 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1214 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1215 cairo_paint(cr);
1216
1217 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1218 tile_mask(cr, window->display->shadow,
1219 shadow_dx, shadow_dy, width, height,
1220 frame->margin + 10 - shadow_dx,
1221 frame->margin + 10 - shadow_dy);
1222
1223 if (window->keyboard_device)
1224 source = window->display->active_frame;
1225 else
1226 source = window->display->inactive_frame;
1227
1228 tile_source(cr, source, 0, 0, width, height,
1229 frame->margin + 10, frame->margin + 50);
1230
1231 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1232 cairo_set_font_size(cr, 14);
1233 cairo_text_extents(cr, window->title, &extents);
1234 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1235 if (window->keyboard_device)
1236 cairo_set_source_rgb(cr, 0, 0, 0);
1237 else
1238 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1239 cairo_show_text(cr, window->title);
1240
1241 cairo_destroy(cr);
1242}
1243
1244static int
1245frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1246{
1247 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001248 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249 const int grip_size = 8;
1250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001252 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001256 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001257 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258 hlocation = WINDOW_RESIZING_RIGHT;
1259 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001260 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001262 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001267 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001268 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001269 vlocation = WINDOW_RESIZING_BOTTOM;
1270 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 location = vlocation | hlocation;
1274 if (location & WINDOW_EXTERIOR)
1275 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001276 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 location = WINDOW_TITLEBAR;
1278 else if (location == WINDOW_INTERIOR)
1279 location = WINDOW_CLIENT_AREA;
1280
1281 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282}
1283
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001285frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001287 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001289 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001290 switch (location) {
1291 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001305 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307 case WINDOW_EXTERIOR:
1308 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001311 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001312}
1313
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001314static void
1315frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001316{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001317 switch (index) {
1318 case 0: /* close */
1319 if (window->close_handler)
1320 window->close_handler(window->parent,
1321 window->user_data);
1322 else
1323 display_exit(window->display);
1324 break;
1325 case 1: /* fullscreen */
1326 /* we don't have a way to get out of fullscreen for now */
1327 window_set_fullscreen(window, 1);
1328 break;
1329 case 2: /* rotate */
1330 case 3: /* scale */
1331 break;
1332 }
1333}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001334
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001335void
1336window_show_frame_menu(struct window *window,
1337 struct input *input, uint32_t time)
1338{
1339 int32_t x, y;
1340
1341 static const char *entries[] = {
1342 "Close", "Fullscreen", "Rotate", "Scale"
1343 };
1344
1345 input_get_position(input, &x, &y);
1346 window_show_menu(window->display, input, time, window,
1347 x - 10, y - 10, frame_menu_func, entries, 4);
1348}
1349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001350static int
1351frame_enter_handler(struct widget *widget,
1352 struct input *input, uint32_t time,
1353 int32_t x, int32_t y, void *data)
1354{
1355 return frame_get_pointer_image_for_location(data, input);
1356}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001357
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001358static int
1359frame_motion_handler(struct widget *widget,
1360 struct input *input, uint32_t time,
1361 int32_t x, int32_t y, void *data)
1362{
1363 return frame_get_pointer_image_for_location(data, input);
1364}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001365
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001366static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001367frame_button_handler(struct widget *widget,
1368 struct input *input, uint32_t time,
1369 int button, int state, void *data)
1370
1371{
1372 struct frame *frame = data;
1373 struct window *window = widget->window;
1374 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001375 location = frame_get_pointer_location(frame, input->sx, input->sy);
1376
1377 if (window->display->shell && button == BTN_LEFT && state == 1) {
1378 switch (location) {
1379 case WINDOW_TITLEBAR:
1380 if (!window->shell_surface)
1381 break;
1382 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001383 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001384 wl_shell_surface_move(window->shell_surface,
1385 input_get_input_device(input),
1386 time);
1387 break;
1388 case WINDOW_RESIZING_TOP:
1389 case WINDOW_RESIZING_BOTTOM:
1390 case WINDOW_RESIZING_LEFT:
1391 case WINDOW_RESIZING_RIGHT:
1392 case WINDOW_RESIZING_TOP_LEFT:
1393 case WINDOW_RESIZING_TOP_RIGHT:
1394 case WINDOW_RESIZING_BOTTOM_LEFT:
1395 case WINDOW_RESIZING_BOTTOM_RIGHT:
1396 if (!window->shell_surface)
1397 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001398 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001399 wl_shell_surface_resize(window->shell_surface,
1400 input_get_input_device(input),
1401 time, location);
1402 break;
1403 }
1404 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001405 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001406 }
1407}
1408
1409struct widget *
1410frame_create(struct window *window, void *data)
1411{
1412 struct frame *frame;
1413
1414 frame = malloc(sizeof *frame);
1415 memset(frame, 0, sizeof *frame);
1416
1417 frame->widget = window_add_widget(window, frame);
1418 frame->child = widget_add_widget(frame->widget, data);
1419 frame->margin = 16;
1420
1421 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1422 widget_set_resize_handler(frame->widget, frame_resize_handler);
1423 widget_set_enter_handler(frame->widget, frame_enter_handler);
1424 widget_set_motion_handler(frame->widget, frame_motion_handler);
1425 widget_set_button_handler(frame->widget, frame_button_handler);
1426
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001427 window->frame = frame;
1428
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001429 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001430}
1431
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001432static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001433frame_destroy(struct frame *frame)
1434{
1435 /* frame->child must be destroyed by the application */
1436 widget_destroy(frame->widget);
1437 free(frame);
1438}
1439
1440static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001441input_set_focus_widget(struct input *input, struct widget *focus,
1442 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001443{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001444 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001445 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001446
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001447 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001448 return;
1449
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001450 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001451 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001452 widget = old;
1453 if (input->grab)
1454 widget = input->grab;
1455 if (widget->leave_handler)
1456 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001457 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001458 }
1459
1460 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001461 widget = focus;
1462 if (input->grab)
1463 widget = input->grab;
1464 if (widget->enter_handler)
1465 pointer = widget->enter_handler(focus, input, time,
1466 x, y,
1467 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001468 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001469
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001470 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001471 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001472}
1473
1474static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001475input_handle_motion(void *data, struct wl_input_device *input_device,
1476 uint32_t time,
1477 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001478{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001479 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001480 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001481 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001482 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001483
1484 input->x = x;
1485 input->y = y;
1486 input->sx = sx;
1487 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001488
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001489 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001490 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001491 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001492 }
1493
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001494 if (input->grab)
1495 widget = input->grab;
1496 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001497 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001498 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001499 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001500 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001501 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001502
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001503 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001504}
1505
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001506void
1507input_grab(struct input *input, struct widget *widget, uint32_t button)
1508{
1509 input->grab = widget;
1510 input->grab_button = button;
1511}
1512
1513void
1514input_ungrab(struct input *input, uint32_t time)
1515{
1516 struct widget *widget;
1517
1518 input->grab = NULL;
1519 if (input->pointer_focus) {
1520 widget = widget_find_widget(input->pointer_focus->widget,
1521 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001522 input_set_focus_widget(input, widget,
1523 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001524 }
1525}
1526
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001527static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001528input_handle_button(void *data,
1529 struct wl_input_device *input_device,
1530 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001531{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001532 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001533 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001534
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001535 if (input->focus_widget && input->grab == NULL && state)
1536 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001537
Neil Roberts6b28aad2012-01-23 19:11:18 +00001538 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001539 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001540 (*widget->button_handler)(widget,
1541 input, time,
1542 button, state,
1543 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001544
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001545 if (input->grab && input->grab_button == button && !state)
1546 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001547}
1548
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001549static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001550input_handle_key(void *data, struct wl_input_device *input_device,
1551 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001552{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001553 struct input *input = data;
1554 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001555 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001556 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001557
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001558 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001559 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001560 return;
1561
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001562 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001563 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001564 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1565 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001566
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001567 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1568
1569 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001571 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001572 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001573
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001574 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1575 if (state)
1576 window_set_maximized(window,
1577 window->type != TYPE_MAXIMIZED);
1578 } else if (window->key_handler) {
1579 (*window->key_handler)(window, input, time, key,
1580 sym, state, window->user_data);
1581 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001582}
1583
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001584static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001585input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001586{
1587 struct window *window = input->pointer_focus;
1588
1589 if (!window)
1590 return;
1591
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001592 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001593
Pekka Paalanene1207c72011-12-16 12:02:09 +02001594 input->pointer_focus = NULL;
1595 input->current_pointer_image = POINTER_UNSET;
1596}
1597
1598static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001599input_handle_pointer_focus(void *data,
1600 struct wl_input_device *input_device,
1601 uint32_t time, struct wl_surface *surface,
1602 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001603{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001604 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001605 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001607
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001608 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001609 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001610 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001611
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001612 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001613 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001614 window = input->pointer_focus;
1615
Kristian Høgsberg59826582011-01-20 11:56:57 -05001616 input->x = x;
1617 input->y = y;
1618 input->sx = sx;
1619 input->sy = sy;
1620
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001621 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001622 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001623 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001624}
1625
1626static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001627input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001628{
1629 struct window *window = input->keyboard_focus;
1630
1631 if (!window)
1632 return;
1633
1634 window->keyboard_device = NULL;
1635 if (window->keyboard_focus_handler)
1636 (*window->keyboard_focus_handler)(window, NULL,
1637 window->user_data);
1638
1639 input->keyboard_focus = NULL;
1640}
1641
1642static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001643input_handle_keyboard_focus(void *data,
1644 struct wl_input_device *input_device,
1645 uint32_t time,
1646 struct wl_surface *surface,
1647 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001648{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001649 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001650 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001651 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001652 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001653
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001654 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001655
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001656 if (surface)
1657 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001658
1659 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001660 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001661 for (k = keys->data; k < end; k++)
1662 input->modifiers |= d->xkb->map->modmap[*k];
1663
1664 window = input->keyboard_focus;
1665 if (window) {
1666 window->keyboard_device = input;
1667 if (window->keyboard_focus_handler)
1668 (*window->keyboard_focus_handler)(window,
1669 window->keyboard_device,
1670 window->user_data);
1671 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001672}
1673
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001674static void
1675input_handle_touch_down(void *data,
1676 struct wl_input_device *wl_input_device,
1677 uint32_t time, struct wl_surface *surface,
1678 int32_t id, int32_t x, int32_t y)
1679{
1680}
1681
1682static void
1683input_handle_touch_up(void *data,
1684 struct wl_input_device *wl_input_device,
1685 uint32_t time, int32_t id)
1686{
1687}
1688
1689static void
1690input_handle_touch_motion(void *data,
1691 struct wl_input_device *wl_input_device,
1692 uint32_t time, int32_t id, int32_t x, int32_t y)
1693{
1694}
1695
1696static void
1697input_handle_touch_frame(void *data,
1698 struct wl_input_device *wl_input_device)
1699{
1700}
1701
1702static void
1703input_handle_touch_cancel(void *data,
1704 struct wl_input_device *wl_input_device)
1705{
1706}
1707
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001708static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001709 input_handle_motion,
1710 input_handle_button,
1711 input_handle_key,
1712 input_handle_pointer_focus,
1713 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001714 input_handle_touch_down,
1715 input_handle_touch_up,
1716 input_handle_touch_motion,
1717 input_handle_touch_frame,
1718 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001719};
1720
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001721void
1722input_get_position(struct input *input, int32_t *x, int32_t *y)
1723{
1724 *x = input->sx;
1725 *y = input->sy;
1726}
1727
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001728struct wl_input_device *
1729input_get_input_device(struct input *input)
1730{
1731 return input->input_device;
1732}
1733
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001734uint32_t
1735input_get_modifiers(struct input *input)
1736{
1737 return input->modifiers;
1738}
1739
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001740struct widget *
1741input_get_focus_widget(struct input *input)
1742{
1743 return input->focus_widget;
1744}
1745
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001746struct data_offer {
1747 struct wl_data_offer *offer;
1748 struct input *input;
1749 struct wl_array types;
1750 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001751
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001752 struct task io_task;
1753 int fd;
1754 data_func_t func;
1755 int32_t x, y;
1756 void *user_data;
1757};
1758
1759static void
1760data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1761{
1762 struct data_offer *offer = data;
1763 char **p;
1764
1765 p = wl_array_add(&offer->types, sizeof *p);
1766 *p = strdup(type);
1767}
1768
1769static const struct wl_data_offer_listener data_offer_listener = {
1770 data_offer_offer,
1771};
1772
1773static void
1774data_offer_destroy(struct data_offer *offer)
1775{
1776 char **p;
1777
1778 offer->refcount--;
1779 if (offer->refcount == 0) {
1780 wl_data_offer_destroy(offer->offer);
1781 for (p = offer->types.data; *p; p++)
1782 free(*p);
1783 wl_array_release(&offer->types);
1784 free(offer);
1785 }
1786}
1787
1788static void
1789data_device_data_offer(void *data,
1790 struct wl_data_device *data_device, uint32_t id)
1791{
1792 struct data_offer *offer;
1793
1794 offer = malloc(sizeof *offer);
1795
1796 wl_array_init(&offer->types);
1797 offer->refcount = 1;
1798 offer->input = data;
1799
1800 /* FIXME: Generate typesafe wrappers for this */
1801 offer->offer = (struct wl_data_offer *)
1802 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1803 id, &wl_data_offer_interface);
1804
1805 wl_data_offer_add_listener(offer->offer,
1806 &data_offer_listener, offer);
1807}
1808
1809static void
1810data_device_enter(void *data, struct wl_data_device *data_device,
1811 uint32_t time, struct wl_surface *surface,
1812 int32_t x, int32_t y, struct wl_data_offer *offer)
1813{
1814 struct input *input = data;
1815 struct window *window;
1816 char **p;
1817
1818 input->drag_offer = wl_data_offer_get_user_data(offer);
1819 window = wl_surface_get_user_data(surface);
1820 input->pointer_focus = window;
1821
1822 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1823 *p = NULL;
1824
1825 window = input->pointer_focus;
1826 if (window->data_handler)
1827 window->data_handler(window, input, time, x, y,
1828 input->drag_offer->types.data,
1829 window->user_data);
1830}
1831
1832static void
1833data_device_leave(void *data, struct wl_data_device *data_device)
1834{
1835 struct input *input = data;
1836
1837 data_offer_destroy(input->drag_offer);
1838 input->drag_offer = NULL;
1839}
1840
1841static void
1842data_device_motion(void *data, struct wl_data_device *data_device,
1843 uint32_t time, int32_t x, int32_t y)
1844{
1845 struct input *input = data;
1846 struct window *window = input->pointer_focus;
1847
1848 input->sx = x;
1849 input->sy = y;
1850
1851 if (window->data_handler)
1852 window->data_handler(window, input, time, x, y,
1853 input->drag_offer->types.data,
1854 window->user_data);
1855}
1856
1857static void
1858data_device_drop(void *data, struct wl_data_device *data_device)
1859{
1860 struct input *input = data;
1861 struct window *window = input->pointer_focus;
1862
1863 if (window->drop_handler)
1864 window->drop_handler(window, input,
1865 input->sx, input->sy, window->user_data);
1866}
1867
1868static void
1869data_device_selection(void *data,
1870 struct wl_data_device *wl_data_device,
1871 struct wl_data_offer *offer)
1872{
1873 struct input *input = data;
1874 char **p;
1875
1876 if (input->selection_offer)
1877 data_offer_destroy(input->selection_offer);
1878
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001879 if (offer) {
1880 input->selection_offer = wl_data_offer_get_user_data(offer);
1881 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1882 *p = NULL;
1883 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001884}
1885
1886static const struct wl_data_device_listener data_device_listener = {
1887 data_device_data_offer,
1888 data_device_enter,
1889 data_device_leave,
1890 data_device_motion,
1891 data_device_drop,
1892 data_device_selection
1893};
1894
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001895void
1896input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1897{
1898 struct display *display = input->display;
1899 struct wl_buffer *buffer;
1900 cairo_surface_t *surface;
1901
1902 if (pointer == input->current_pointer_image)
1903 return;
1904
1905 input->current_pointer_image = pointer;
1906 surface = display->pointer_surfaces[pointer];
1907
1908 if (!surface)
1909 return;
1910
1911 buffer = display_get_buffer_for_surface(display, surface);
1912 wl_input_device_attach(input->input_device, time, buffer,
1913 pointer_images[pointer].hotspot_x,
1914 pointer_images[pointer].hotspot_y);
1915}
1916
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001917struct wl_data_device *
1918input_get_data_device(struct input *input)
1919{
1920 return input->data_device;
1921}
1922
1923void
1924input_set_selection(struct input *input,
1925 struct wl_data_source *source, uint32_t time)
1926{
1927 wl_data_device_set_selection(input->data_device, source, time);
1928}
1929
1930void
1931input_accept(struct input *input, uint32_t time, const char *type)
1932{
1933 wl_data_offer_accept(input->drag_offer->offer, time, type);
1934}
1935
1936static void
1937offer_io_func(struct task *task, uint32_t events)
1938{
1939 struct data_offer *offer =
1940 container_of(task, struct data_offer, io_task);
1941 unsigned int len;
1942 char buffer[4096];
1943
1944 len = read(offer->fd, buffer, sizeof buffer);
1945 offer->func(buffer, len,
1946 offer->x, offer->y, offer->user_data);
1947
1948 if (len == 0) {
1949 close(offer->fd);
1950 data_offer_destroy(offer);
1951 }
1952}
1953
1954static void
1955data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1956 data_func_t func, void *user_data)
1957{
1958 int p[2];
1959
1960 pipe2(p, O_CLOEXEC);
1961 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1962 close(p[1]);
1963
1964 offer->io_task.run = offer_io_func;
1965 offer->fd = p[0];
1966 offer->func = func;
1967 offer->refcount++;
1968 offer->user_data = user_data;
1969
1970 display_watch_fd(offer->input->display,
1971 offer->fd, EPOLLIN, &offer->io_task);
1972}
1973
1974void
1975input_receive_drag_data(struct input *input, const char *mime_type,
1976 data_func_t func, void *data)
1977{
1978 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1979 input->drag_offer->x = input->sx;
1980 input->drag_offer->y = input->sy;
1981}
1982
1983int
1984input_receive_selection_data(struct input *input, const char *mime_type,
1985 data_func_t func, void *data)
1986{
1987 char **p;
1988
1989 if (input->selection_offer == NULL)
1990 return -1;
1991
1992 for (p = input->selection_offer->types.data; *p; p++)
1993 if (strcmp(mime_type, *p) == 0)
1994 break;
1995
1996 if (*p == NULL)
1997 return -1;
1998
1999 data_offer_receive_data(input->selection_offer,
2000 mime_type, func, data);
2001 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002002}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002003
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002004int
2005input_receive_selection_data_to_fd(struct input *input,
2006 const char *mime_type, int fd)
2007{
2008 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2009
2010 return 0;
2011}
2012
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002013void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002014window_move(struct window *window, struct input *input, uint32_t time)
2015{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002016 if (!window->shell_surface)
2017 return;
2018
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002019 wl_shell_surface_move(window->shell_surface,
2020 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002021}
2022
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002023static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002024idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002025{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002026 struct window *window =
2027 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002028 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002029
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002030 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002031 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002032 widget_set_allocation(widget,
2033 window->pending_allocation.x,
2034 window->pending_allocation.y,
2035 window->pending_allocation.width,
2036 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002037
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002038 if (widget->resize_handler)
2039 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002040 widget->allocation.width,
2041 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002042 widget->user_data);
2043
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002044 if (window->allocation.width != widget->allocation.width ||
2045 window->allocation.height != widget->allocation.height) {
2046 window->allocation = widget->allocation;
2047 window_schedule_redraw(window);
2048 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002049}
2050
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002051void
2052window_schedule_resize(struct window *window, int width, int height)
2053{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002054 window->pending_allocation.x = 0;
2055 window->pending_allocation.y = 0;
2056 window->pending_allocation.width = width;
2057 window->pending_allocation.height = height;
2058
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002059 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002060 window->resize_task.run = idle_resize;
2061 display_defer(window->display, &window->resize_task);
2062 window->resize_scheduled = 1;
2063 }
2064}
2065
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002066void
2067widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2068{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002069 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002070}
2071
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002072static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002074 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002075 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002076{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002077 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002078
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002079 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002080 return;
2081
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002082 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002083 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002084}
2085
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002086static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002087menu_destroy(struct menu *menu)
2088{
2089 widget_destroy(menu->widget);
2090 window_destroy(menu->window);
2091 free(menu);
2092}
2093
2094static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002095handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2096{
2097 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002098 struct menu *menu = window->widget->user_data;
2099
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002100 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002101 * device. Or just use wl_callback. And this really needs to
2102 * be a window vfunc that the menu can set. And we need the
2103 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002104
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002105 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002106 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002107 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002108}
2109
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002110static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002111 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002112 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002113};
2114
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002115void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002116window_get_allocation(struct window *window,
2117 struct rectangle *allocation)
2118{
2119 *allocation = window->allocation;
2120}
2121
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002122static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002123widget_redraw(struct widget *widget)
2124{
2125 struct widget *child;
2126
2127 if (widget->redraw_handler)
2128 widget->redraw_handler(widget, widget->user_data);
2129 wl_list_for_each(child, &widget->child_list, link)
2130 widget_redraw(child);
2131}
2132
2133static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002134idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002135{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002136 struct window *window =
2137 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002138
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002139 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002140 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002141 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002142 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002143}
2144
2145void
2146window_schedule_redraw(struct window *window)
2147{
2148 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002149 window->redraw_task.run = idle_redraw;
2150 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002151 window->redraw_scheduled = 1;
2152 }
2153}
2154
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002155void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002156window_set_custom(struct window *window)
2157{
2158 window->type = TYPE_CUSTOM;
2159}
2160
2161void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002162window_set_fullscreen(struct window *window, int fullscreen)
2163{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002164 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002165 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002166
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002167 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002168 return;
2169
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002170 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002171 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002172 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002173 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002174 width = output->allocation.width;
2175 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002176 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002177 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178 width = window->saved_allocation.width;
2179 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002180 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002181
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002182 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002183}
2184
2185void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002186window_set_maximized(struct window *window, int maximized)
2187{
2188 if (!window->display->shell)
2189 return;
2190
2191 if ((window->type == TYPE_MAXIMIZED) == maximized)
2192 return;
2193
2194 if (window->type == TYPE_TOPLEVEL) {
2195 window->saved_allocation = window->allocation;
2196 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2197 window->type = TYPE_MAXIMIZED;
2198 } else {
2199 wl_shell_surface_set_toplevel(window->shell_surface);
2200 window->type = TYPE_TOPLEVEL;
2201 window_schedule_resize(window,
2202 window->saved_allocation.width,
2203 window->saved_allocation.height);
2204 }
2205}
2206
2207void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002208window_set_user_data(struct window *window, void *data)
2209{
2210 window->user_data = data;
2211}
2212
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002213void *
2214window_get_user_data(struct window *window)
2215{
2216 return window->user_data;
2217}
2218
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002219void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002220window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002221 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002222{
2223 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002224}
2225
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002226void
2227window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002228 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002229{
2230 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002231}
2232
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002233void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002234window_set_data_handler(struct window *window, window_data_handler_t handler)
2235{
2236 window->data_handler = handler;
2237}
2238
2239void
2240window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2241{
2242 window->drop_handler = handler;
2243}
2244
2245void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002246window_set_close_handler(struct window *window,
2247 window_close_handler_t handler)
2248{
2249 window->close_handler = handler;
2250}
2251
2252void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002253window_set_transparent(struct window *window, int transparent)
2254{
2255 window->transparent = transparent;
2256}
2257
2258void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002259window_set_title(struct window *window, const char *title)
2260{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002261 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002262 window->title = strdup(title);
2263}
2264
2265const char *
2266window_get_title(struct window *window)
2267{
2268 return window->title;
2269}
2270
2271void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002272display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2273 int32_t x, int32_t y, int32_t width, int32_t height)
2274{
2275 struct wl_buffer *buffer;
2276
2277 buffer = display_get_buffer_for_surface(display, cairo_surface);
2278
2279 wl_buffer_damage(buffer, x, y, width, height);
2280}
2281
2282void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002283window_damage(struct window *window, int32_t x, int32_t y,
2284 int32_t width, int32_t height)
2285{
2286 wl_surface_damage(window->surface, x, y, width, height);
2287}
2288
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002289static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002290window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002291{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002292 struct window *window;
2293
2294 window = malloc(sizeof *window);
2295 if (window == NULL)
2296 return NULL;
2297
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002298 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002299 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002300 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002301 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002302 if (display->shell) {
2303 window->shell_surface =
2304 wl_shell_get_shell_surface(display->shell,
2305 window->surface);
2306 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002307 window->allocation.x = 0;
2308 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002309 window->allocation.width = 0;
2310 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002311 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002312 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002313 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002314
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002315 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002316#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002317 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2318 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002319#else
2320 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2321#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002322 else
2323 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002324
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002325 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002326 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002327
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002328 if (window->shell_surface) {
2329 wl_shell_surface_set_user_data(window->shell_surface, window);
2330 wl_shell_surface_add_listener(window->shell_surface,
2331 &shell_surface_listener, window);
2332 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002333
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002334 return window;
2335}
2336
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002337struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002338window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002339{
2340 struct window *window;
2341
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002342 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002343 if (!window)
2344 return NULL;
2345
2346 return window;
2347}
2348
2349struct window *
2350window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002351 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002352{
2353 struct window *window;
2354
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002355 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002356 if (!window)
2357 return NULL;
2358
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002359 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002360 window->x = x;
2361 window->y = y;
2362
2363 return window;
2364}
2365
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002366static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002367menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002368{
2369 int next;
2370
2371 next = (sy - 8) / 20;
2372 if (menu->current != next) {
2373 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002374 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002375 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002376}
2377
2378static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002379menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002380 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002381 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002382{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002383 struct menu *menu = data;
2384
2385 if (widget == menu->widget)
2386 menu_set_item(data, y);
2387
2388 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002389}
2390
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002391static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002392menu_enter_handler(struct widget *widget,
2393 struct input *input, uint32_t time,
2394 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002395{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002396 struct menu *menu = data;
2397
2398 if (widget == menu->widget)
2399 menu_set_item(data, y);
2400
2401 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002402}
2403
2404static void
2405menu_leave_handler(struct widget *widget, struct input *input, void *data)
2406{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002407 struct menu *menu = data;
2408
2409 if (widget == menu->widget)
2410 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411}
2412
2413static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002414menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002415 struct input *input, uint32_t time,
2416 int button, int state, void *data)
2417
2418{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002419 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002420
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002421 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002422 /* Either relase after press-drag-release or
2423 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002424 menu->func(menu->window->parent,
2425 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002426 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002427 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002428 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002429}
2430
2431static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002432menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433{
2434 cairo_t *cr;
2435 const int32_t r = 3, margin = 3;
2436 struct menu *menu = data;
2437 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002438 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002439
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002440 cr = cairo_create(window->cairo_surface);
2441 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2442 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2443 cairo_paint(cr);
2444
2445 width = window->allocation.width;
2446 height = window->allocation.height;
2447 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002448
2449 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002450 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2451 cairo_fill(cr);
2452
2453 for (i = 0; i < menu->count; i++) {
2454 if (i == menu->current) {
2455 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2456 cairo_rectangle(cr, margin, i * 20 + margin,
2457 width - 2 * margin, 20);
2458 cairo_fill(cr);
2459 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2460 cairo_move_to(cr, 10, i * 20 + 16);
2461 cairo_show_text(cr, menu->entries[i]);
2462 } else {
2463 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2464 cairo_move_to(cr, 10, i * 20 + 16);
2465 cairo_show_text(cr, menu->entries[i]);
2466 }
2467 }
2468
2469 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002470}
2471
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002472void
2473window_show_menu(struct display *display,
2474 struct input *input, uint32_t time, struct window *parent,
2475 int32_t x, int32_t y,
2476 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002477{
2478 struct window *window;
2479 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002480 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002481
2482 menu = malloc(sizeof *menu);
2483 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002484 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002485
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002486 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002487 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002488 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002489
2490 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002491 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002492 menu->entries = entries;
2493 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002494 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002495 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002496 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002497 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002498 window->type = TYPE_MENU;
2499 window->x = x;
2500 window->y = y;
2501
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002502 wl_shell_surface_set_popup(window->shell_surface,
2503 input->input_device, time,
2504 window->parent->shell_surface,
2505 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002506
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002507 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002508 widget_set_enter_handler(menu->widget, menu_enter_handler);
2509 widget_set_leave_handler(menu->widget, menu_leave_handler);
2510 widget_set_motion_handler(menu->widget, menu_motion_handler);
2511 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002512
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002513 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002514 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002515}
2516
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002517void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002518window_set_buffer_type(struct window *window, enum window_buffer_type type)
2519{
2520 window->buffer_type = type;
2521}
2522
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002523
2524static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002525display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002526 struct wl_output *wl_output,
2527 int x, int y,
2528 int physical_width,
2529 int physical_height,
2530 int subpixel,
2531 const char *make,
2532 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002533{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002535
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002536 output->allocation.x = x;
2537 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002538}
2539
2540static void
2541display_handle_mode(void *data,
2542 struct wl_output *wl_output,
2543 uint32_t flags,
2544 int width,
2545 int height,
2546 int refresh)
2547{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002548 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002549 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002550
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002551 if (flags & WL_OUTPUT_MODE_CURRENT) {
2552 output->allocation.width = width;
2553 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002554 if (display->output_configure_handler)
2555 (*display->output_configure_handler)(
2556 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002557 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002558}
2559
2560static const struct wl_output_listener output_listener = {
2561 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002562 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002563};
2564
2565static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002566display_add_output(struct display *d, uint32_t id)
2567{
2568 struct output *output;
2569
2570 output = malloc(sizeof *output);
2571 if (output == NULL)
2572 return;
2573
2574 memset(output, 0, sizeof *output);
2575 output->display = d;
2576 output->output =
2577 wl_display_bind(d->display, id, &wl_output_interface);
2578 wl_list_insert(d->output_list.prev, &output->link);
2579
2580 wl_output_add_listener(output->output, &output_listener, output);
2581}
2582
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002583static void
2584output_destroy(struct output *output)
2585{
2586 if (output->destroy_handler)
2587 (*output->destroy_handler)(output, output->user_data);
2588
2589 wl_output_destroy(output->output);
2590 wl_list_remove(&output->link);
2591 free(output);
2592}
2593
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002594void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002595display_set_output_configure_handler(struct display *display,
2596 display_output_handler_t handler)
2597{
2598 struct output *output;
2599
2600 display->output_configure_handler = handler;
2601 if (!handler)
2602 return;
2603
2604 wl_list_for_each(output, &display->output_list, link)
2605 (*display->output_configure_handler)(output,
2606 display->user_data);
2607}
2608
2609void
2610output_set_user_data(struct output *output, void *data)
2611{
2612 output->user_data = data;
2613}
2614
2615void *
2616output_get_user_data(struct output *output)
2617{
2618 return output->user_data;
2619}
2620
2621void
2622output_set_destroy_handler(struct output *output,
2623 display_output_handler_t handler)
2624{
2625 output->destroy_handler = handler;
2626 /* FIXME: implement this, once we have way to remove outputs */
2627}
2628
2629void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002630output_get_allocation(struct output *output, struct rectangle *allocation)
2631{
2632 *allocation = output->allocation;
2633}
2634
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002635struct wl_output *
2636output_get_wl_output(struct output *output)
2637{
2638 return output->output;
2639}
2640
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002641static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002642display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002643{
2644 struct input *input;
2645
2646 input = malloc(sizeof *input);
2647 if (input == NULL)
2648 return;
2649
2650 memset(input, 0, sizeof *input);
2651 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002652 input->input_device =
2653 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002654 input->pointer_focus = NULL;
2655 input->keyboard_focus = NULL;
2656 wl_list_insert(d->input_list.prev, &input->link);
2657
2658 wl_input_device_add_listener(input->input_device,
2659 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002660 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002661
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002662 input->data_device =
2663 wl_data_device_manager_get_data_device(d->data_device_manager,
2664 input->input_device);
2665 wl_data_device_add_listener(input->data_device,
2666 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002667}
2668
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002669static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002670input_destroy(struct input *input)
2671{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002672 input_remove_keyboard_focus(input);
2673 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002674
2675 if (input->drag_offer)
2676 data_offer_destroy(input->drag_offer);
2677
2678 if (input->selection_offer)
2679 data_offer_destroy(input->selection_offer);
2680
2681 wl_data_device_destroy(input->data_device);
2682 wl_list_remove(&input->link);
2683 wl_input_device_destroy(input->input_device);
2684 free(input);
2685}
2686
2687static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002688display_handle_global(struct wl_display *display, uint32_t id,
2689 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002690{
2691 struct display *d = data;
2692
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002693 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002694 d->compositor =
2695 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002696 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002697 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002698 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002699 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002700 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002701 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002702 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002703 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002704 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2705 d->data_device_manager =
2706 wl_display_bind(display, id,
2707 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002708 }
2709}
2710
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002711static void
2712display_render_frame(struct display *d)
2713{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002714 int radius = 8;
2715 cairo_t *cr;
2716
2717 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2718 cr = cairo_create(d->shadow);
2719 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2720 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2721 rounded_rect(cr, 16, 16, 112, 112, radius);
2722 cairo_fill(cr);
2723 cairo_destroy(cr);
2724 blur_surface(d->shadow, 64);
2725
2726 d->active_frame =
2727 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2728 cr = cairo_create(d->active_frame);
2729 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2730 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2731 rounded_rect(cr, 16, 16, 112, 112, radius);
2732 cairo_fill(cr);
2733 cairo_destroy(cr);
2734
2735 d->inactive_frame =
2736 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2737 cr = cairo_create(d->inactive_frame);
2738 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2739 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2740 rounded_rect(cr, 16, 16, 112, 112, radius);
2741 cairo_fill(cr);
2742 cairo_destroy(cr);
2743}
2744
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002745static void
2746init_xkb(struct display *d)
2747{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002748 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002749
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002750 names.rules = "evdev";
2751 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002752 names.layout = option_xkb_layout;
2753 names.variant = option_xkb_variant;
2754 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002755
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002756 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002757 if (!d->xkb) {
2758 fprintf(stderr, "Failed to compile keymap\n");
2759 exit(1);
2760 }
2761}
2762
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002763static void
2764fini_xkb(struct display *display)
2765{
2766 xkb_free_keymap(display->xkb);
2767}
2768
Yuval Fledel45568f62010-12-06 09:18:12 -05002769static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002770init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002771{
2772 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002773 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002774
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002775 static const EGLint argb_cfg_attribs[] = {
2776 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002777 EGL_RED_SIZE, 1,
2778 EGL_GREEN_SIZE, 1,
2779 EGL_BLUE_SIZE, 1,
2780 EGL_ALPHA_SIZE, 1,
2781 EGL_DEPTH_SIZE, 1,
2782 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2783 EGL_NONE
2784 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002785
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002786 static const EGLint rgb_cfg_attribs[] = {
2787 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2788 EGL_RED_SIZE, 1,
2789 EGL_GREEN_SIZE, 1,
2790 EGL_BLUE_SIZE, 1,
2791 EGL_ALPHA_SIZE, 0,
2792 EGL_DEPTH_SIZE, 1,
2793 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2794 EGL_NONE
2795 };
2796
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002797#ifdef USE_CAIRO_GLESV2
2798 static const EGLint context_attribs[] = {
2799 EGL_CONTEXT_CLIENT_VERSION, 2,
2800 EGL_NONE
2801 };
2802 EGLint api = EGL_OPENGL_ES_API;
2803#else
2804 EGLint *context_attribs = NULL;
2805 EGLint api = EGL_OPENGL_API;
2806#endif
2807
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002808 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002809 if (!eglInitialize(d->dpy, &major, &minor)) {
2810 fprintf(stderr, "failed to initialize display\n");
2811 return -1;
2812 }
2813
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002814 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002815 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2816 return -1;
2817 }
2818
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002819 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2820 &d->argb_config, 1, &n) || n != 1) {
2821 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002822 return -1;
2823 }
2824
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002825 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2826 &d->rgb_config, 1, &n) || n != 1) {
2827 fprintf(stderr, "failed to choose rgb config\n");
2828 return -1;
2829 }
2830
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002831 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2832 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002833 if (d->rgb_ctx == NULL) {
2834 fprintf(stderr, "failed to create context\n");
2835 return -1;
2836 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002837 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002838 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002839 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002840 fprintf(stderr, "failed to create context\n");
2841 return -1;
2842 }
2843
Benjamin Franzke0c991632011-09-27 21:57:31 +02002844 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002845 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002846 return -1;
2847 }
2848
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002849#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002850 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2851 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002852 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002853 return -1;
2854 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002855 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2856 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2857 fprintf(stderr, "failed to get cairo egl argb device\n");
2858 return -1;
2859 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002860#endif
2861
2862 return 0;
2863}
2864
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002865static void
2866fini_egl(struct display *display)
2867{
2868#ifdef HAVE_CAIRO_EGL
2869 cairo_device_destroy(display->argb_device);
2870 cairo_device_destroy(display->rgb_device);
2871#endif
2872
2873 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2874 EGL_NO_CONTEXT);
2875
2876 eglTerminate(display->dpy);
2877 eglReleaseThread();
2878}
2879
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002880static int
2881event_mask_update(uint32_t mask, void *data)
2882{
2883 struct display *d = data;
2884
2885 d->mask = mask;
2886
2887 return 0;
2888}
2889
2890static void
2891handle_display_data(struct task *task, uint32_t events)
2892{
2893 struct display *display =
2894 container_of(task, struct display, display_task);
2895
2896 wl_display_iterate(display->display, display->mask);
2897}
2898
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002899struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002900display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002901{
2902 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002903 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002904 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002905 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002906
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002907 g_type_init();
2908
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002909 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002910 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002911 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002912
2913 xkb_option_group = g_option_group_new("xkb",
2914 "Keyboard options",
2915 "Show all XKB options",
2916 NULL, NULL);
2917 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2918 g_option_context_add_group (context, xkb_option_group);
2919
2920 if (!g_option_context_parse(context, argc, argv, &error)) {
2921 fprintf(stderr, "option parsing failed: %s\n", error->message);
2922 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002923 }
2924
Tim Wiederhake748f6722011-01-23 23:25:25 +01002925 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002926
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002927 d = malloc(sizeof *d);
2928 if (d == NULL)
2929 return NULL;
2930
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002931 memset(d, 0, sizeof *d);
2932
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002933 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002934 if (d->display == NULL) {
2935 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002936 return NULL;
2937 }
2938
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002939 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2940 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2941 d->display_task.run = handle_display_data;
2942 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2943
2944 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002945 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002946 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002947
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002948 /* Set up listener so we'll catch all events. */
2949 wl_display_add_global_listener(d->display,
2950 display_handle_global, d);
2951
2952 /* Process connection events. */
2953 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002954 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002955 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002956
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002957 d->image_target_texture_2d =
2958 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2959 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2960 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2961
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002962 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002963
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002964 display_render_frame(d);
2965
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002966 wl_list_init(&d->window_list);
2967
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002968 init_xkb(d);
2969
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002970 return d;
2971}
2972
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002973static void
2974display_destroy_outputs(struct display *display)
2975{
2976 struct output *tmp;
2977 struct output *output;
2978
2979 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2980 output_destroy(output);
2981}
2982
Pekka Paalanene1207c72011-12-16 12:02:09 +02002983static void
2984display_destroy_inputs(struct display *display)
2985{
2986 struct input *tmp;
2987 struct input *input;
2988
2989 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2990 input_destroy(input);
2991}
2992
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002993void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002994display_destroy(struct display *display)
2995{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002996 if (!wl_list_empty(&display->window_list))
2997 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2998
2999 if (!wl_list_empty(&display->deferred_list))
3000 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3001
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003002 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003003 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003004
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003005 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003006
3007 cairo_surface_destroy(display->active_frame);
3008 cairo_surface_destroy(display->inactive_frame);
3009 cairo_surface_destroy(display->shadow);
3010 destroy_pointer_surfaces(display);
3011
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003012 fini_egl(display);
3013
Pekka Paalanenc2052982011-12-16 11:41:32 +02003014 if (display->shell)
3015 wl_shell_destroy(display->shell);
3016
3017 if (display->shm)
3018 wl_shm_destroy(display->shm);
3019
3020 if (display->data_device_manager)
3021 wl_data_device_manager_destroy(display->data_device_manager);
3022
3023 wl_compositor_destroy(display->compositor);
3024
3025 close(display->epoll_fd);
3026
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003027 wl_display_flush(display->display);
3028 wl_display_destroy(display->display);
3029 free(display);
3030}
3031
3032void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003033display_set_user_data(struct display *display, void *data)
3034{
3035 display->user_data = data;
3036}
3037
3038void *
3039display_get_user_data(struct display *display)
3040{
3041 return display->user_data;
3042}
3043
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003044struct wl_display *
3045display_get_display(struct display *display)
3046{
3047 return display->display;
3048}
3049
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003050struct output *
3051display_get_output(struct display *display)
3052{
3053 return container_of(display->output_list.next, struct output, link);
3054}
3055
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003056struct wl_compositor *
3057display_get_compositor(struct display *display)
3058{
3059 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003060}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003061
3062EGLDisplay
3063display_get_egl_display(struct display *d)
3064{
3065 return d->dpy;
3066}
3067
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003068struct wl_data_source *
3069display_create_data_source(struct display *display)
3070{
3071 return wl_data_device_manager_create_data_source(display->data_device_manager);
3072}
3073
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003074EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003075display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003077 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003078}
3079
Benjamin Franzke0c991632011-09-27 21:57:31 +02003080EGLConfig
3081display_get_argb_egl_config(struct display *d)
3082{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003083 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003084}
3085
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003086struct wl_shell *
3087display_get_shell(struct display *display)
3088{
3089 return display->shell;
3090}
3091
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003092int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003093display_acquire_window_surface(struct display *display,
3094 struct window *window,
3095 EGLContext ctx)
3096{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003097#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003098 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003099 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003100
3101 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003102 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003103 device = cairo_surface_get_device(window->cairo_surface);
3104 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003105 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003106
Benjamin Franzke0c991632011-09-27 21:57:31 +02003107 if (!ctx) {
3108 if (device == display->rgb_device)
3109 ctx = display->rgb_ctx;
3110 else if (device == display->argb_device)
3111 ctx = display->argb_ctx;
3112 else
3113 assert(0);
3114 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003115
3116 data = cairo_surface_get_user_data(window->cairo_surface,
3117 &surface_data_key);
3118
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003119 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003120 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003121 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3122 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003123
3124 return 0;
3125#else
3126 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003127#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003128}
3129
3130void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003131display_release_window_surface(struct display *display,
3132 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003133{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003134#ifdef HAVE_CAIRO_EGL
3135 cairo_device_t *device;
3136
3137 device = cairo_surface_get_device(window->cairo_surface);
3138 if (!device)
3139 return;
3140
3141 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003142 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003143 cairo_device_release(device);
3144#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003145}
3146
3147void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003148display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003149{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003150 wl_list_insert(&display->deferred_list, &task->link);
3151}
3152
3153void
3154display_watch_fd(struct display *display,
3155 int fd, uint32_t events, struct task *task)
3156{
3157 struct epoll_event ep;
3158
3159 ep.events = events;
3160 ep.data.ptr = task;
3161 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3162}
3163
3164void
3165display_run(struct display *display)
3166{
3167 struct task *task;
3168 struct epoll_event ep[16];
3169 int i, count;
3170
Pekka Paalanen826d7952011-12-15 10:14:07 +02003171 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003172 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003173 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003174
3175 while (!wl_list_empty(&display->deferred_list)) {
3176 task = container_of(display->deferred_list.next,
3177 struct task, link);
3178 wl_list_remove(&task->link);
3179 task->run(task, 0);
3180 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003181
3182 if (!display->running)
3183 break;
3184
3185 wl_display_flush(display->display);
3186
3187 count = epoll_wait(display->epoll_fd,
3188 ep, ARRAY_LENGTH(ep), -1);
3189 for (i = 0; i < count; i++) {
3190 task = ep[i].data.ptr;
3191 task->run(task, ep[i].events);
3192 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003193 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003194}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003195
3196void
3197display_exit(struct display *display)
3198{
3199 display->running = 0;
3200}