blob: 7fc35d8342338b1ee7555d42827a219c652ec479 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Daniel Stone9d4f0302012-02-15 16:33:21 +000052#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050053#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040054
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020056#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040057#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050060
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050062 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050063 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040064 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040065 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Hø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;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200164 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 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:
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500793 wl_shell_surface_set_fullscreen(window->shell_surface,
794 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 0, NULL);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500796 case TYPE_MAXIMIZED:
797 wl_shell_surface_set_maximized(window->shell_surface, NULL);
798 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200800 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400801 break;
802 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200803 wl_shell_surface_set_transient(window->shell_surface,
804 window->parent->shell_surface,
805 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400806 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500807 case TYPE_MENU:
808 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400809 case TYPE_CUSTOM:
810 break;
811 }
812}
813
814static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500815window_attach_surface(struct window *window)
816{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400817 struct display *display = window->display;
818 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400819 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500823 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400825 if (display->shell)
826 window_set_type(window);
827
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000829#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
831 data = cairo_surface_get_user_data(window->cairo_surface,
832 &surface_data_key);
833
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 cairo_gl_surface_swapbuffers(window->cairo_surface);
835 wl_egl_window_get_attached_size(data->window,
836 &window->server_allocation.width,
837 &window->server_allocation.height);
838 break;
839 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000840#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 case WINDOW_BUFFER_TYPE_SHM:
842 if (window->pending_surface != NULL)
843 return;
844
845 window->pending_surface = window->cairo_surface;
846 window->cairo_surface = NULL;
847
848 buffer =
849 display_get_buffer_for_surface(display,
850 window->pending_surface);
851
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200852 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 wl_surface_attach(window->surface, buffer, x, y);
854 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400855 cb = wl_display_sync(display->display);
856 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000858 default:
859 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100860 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500861
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500862 wl_surface_damage(window->surface, 0, 0,
863 window->allocation.width,
864 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500865}
866
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500867void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400868window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500869{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100870 if (window->cairo_surface) {
871 switch (window->buffer_type) {
872 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
873 case WINDOW_BUFFER_TYPE_SHM:
874 display_surface_damage(window->display,
875 window->cairo_surface,
876 0, 0,
877 window->allocation.width,
878 window->allocation.height);
879 break;
880 default:
881 break;
882 }
883 window_attach_surface(window);
884 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500885}
886
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400887void
888window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500890 cairo_surface_reference(surface);
891
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892 if (window->cairo_surface != NULL)
893 cairo_surface_destroy(window->cairo_surface);
894
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500895 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896}
897
Benjamin Franzke22d54812011-07-16 19:50:32 +0000898#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899static void
900window_resize_cairo_window_surface(struct window *window)
901{
902 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904
905 data = cairo_surface_get_user_data(window->cairo_surface,
906 &surface_data_key);
907
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909 wl_egl_window_resize(data->window,
910 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200911 window->allocation.height,
912 x,y);
913
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914 cairo_gl_surface_set_size(window->cairo_surface,
915 window->allocation.width,
916 window->allocation.height);
917}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000918#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100919
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400920struct display *
921window_get_display(struct window *window)
922{
923 return window->display;
924}
925
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400926void
927window_create_surface(struct window *window)
928{
929 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400930 uint32_t flags = 0;
931
932 if (!window->transparent)
933 flags = SURFACE_OPAQUE;
934
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400935 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500936#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100937 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
938 if (window->cairo_surface) {
939 window_resize_cairo_window_surface(window);
940 return;
941 }
942 surface = display_create_surface(window->display,
943 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400944 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100945 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500946 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400947 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100948 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400950 break;
951#endif
952 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400953 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400954 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400955 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800956 default:
957 surface = NULL;
958 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400959 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400960
961 window_set_surface(window, surface);
962 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400963}
964
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200965static void frame_destroy(struct frame *frame);
966
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400967void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500968window_destroy(struct window *window)
969{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200970 struct display *display = window->display;
971 struct input *input;
972
973 if (window->redraw_scheduled)
974 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500975 if (window->resize_scheduled)
976 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200977
978 wl_list_for_each(input, &display->input_list, link) {
979 if (input->pointer_focus == window)
980 input->pointer_focus = NULL;
981 if (input->keyboard_focus == window)
982 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500983 if (input->focus_widget &&
984 input->focus_widget->window == window)
985 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200986 }
987
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200988 if (window->frame)
989 frame_destroy(window->frame);
990
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200991 if (window->shell_surface)
992 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500993 wl_surface_destroy(window->surface);
994 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200995
996 if (window->cairo_surface != NULL)
997 cairo_surface_destroy(window->cairo_surface);
998 if (window->pending_surface != NULL)
999 cairo_surface_destroy(window->pending_surface);
1000
1001 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001002 free(window);
1003}
1004
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001005static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001009
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001010 wl_list_for_each(child, &widget->child_list, link) {
1011 target = widget_find_widget(child, x, y);
1012 if (target)
1013 return target;
1014 }
1015
1016 if (widget->allocation.x <= x &&
1017 x < widget->allocation.x + widget->allocation.width &&
1018 widget->allocation.y <= y &&
1019 y < widget->allocation.y + widget->allocation.height) {
1020 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001021 }
1022
1023 return NULL;
1024}
1025
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001026static struct widget *
1027widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001030
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001031 widget = malloc(sizeof *widget);
1032 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001033 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001034 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001035 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001036 wl_list_init(&widget->child_list);
1037
1038 return widget;
1039}
1040
1041struct widget *
1042window_add_widget(struct window *window, void *data)
1043{
1044 window->widget = widget_create(window, data);
1045 wl_list_init(&window->widget->link);
1046
1047 return window->widget;
1048}
1049
1050struct widget *
1051widget_add_widget(struct widget *parent, void *data)
1052{
1053 struct widget *widget;
1054
1055 widget = widget_create(parent->window, data);
1056 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059}
1060
1061void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001062widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001063{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001064 struct display *display = widget->window->display;
1065 struct input *input;
1066
1067 wl_list_for_each(input, &display->input_list, link) {
1068 if (input->focus_widget == widget)
1069 input->focus_widget = NULL;
1070 }
1071
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001072 wl_list_remove(&widget->link);
1073 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074}
1075
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001078{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001079 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080}
1081
1082void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001083widget_set_size(struct widget *widget, int32_t width, int32_t height)
1084{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001085 widget->allocation.width = width;
1086 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001087}
1088
1089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_set_allocation(struct widget *widget,
1091 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001093 widget->allocation.x = x;
1094 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001095 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001096}
1097
1098void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001104void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001105widget_set_resize_handler(struct widget *widget,
1106 widget_resize_handler_t handler)
1107{
1108 widget->resize_handler = handler;
1109}
1110
1111void
1112widget_set_redraw_handler(struct widget *widget,
1113 widget_redraw_handler_t handler)
1114{
1115 widget->redraw_handler = handler;
1116}
1117
1118void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001119widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001120{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001121 widget->enter_handler = handler;
1122}
1123
1124void
1125widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1126{
1127 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001128}
1129
1130void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001131widget_set_motion_handler(struct widget *widget,
1132 widget_motion_handler_t handler)
1133{
1134 widget->motion_handler = handler;
1135}
1136
1137void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001138widget_set_button_handler(struct widget *widget,
1139 widget_button_handler_t handler)
1140{
1141 widget->button_handler = handler;
1142}
1143
1144void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145widget_schedule_redraw(struct widget *widget)
1146{
1147 window_schedule_redraw(widget->window);
1148}
1149
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001150cairo_surface_t *
1151window_get_surface(struct window *window)
1152{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001153 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001154}
1155
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001156struct wl_surface *
1157window_get_wl_surface(struct window *window)
1158{
1159 return window->surface;
1160}
1161
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001162struct wl_shell_surface *
1163window_get_wl_shell_surface(struct window *window)
1164{
1165 return window->shell_surface;
1166}
1167
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168static void
1169frame_resize_handler(struct widget *widget,
1170 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172 struct frame *frame = data;
1173 struct widget *child = frame->child;
1174 struct rectangle allocation;
1175 int decoration_width, decoration_height;
1176
1177 decoration_width = 20 + frame->margin * 2;
1178 decoration_height = 60 + frame->margin * 2;
1179
1180 allocation.x = 10 + frame->margin;
1181 allocation.y = 50 + frame->margin;
1182 allocation.width = width - decoration_width;
1183 allocation.height = height - decoration_height;
1184
1185 widget_set_allocation(child, allocation.x, allocation.y,
1186 allocation.width, allocation.height);
1187
1188 if (child->resize_handler)
1189 child->resize_handler(child,
1190 allocation.width,
1191 allocation.height,
1192 child->user_data);
1193
1194 widget_set_allocation(widget, 0, 0,
1195 child->allocation.width + decoration_width,
1196 child->allocation.height + decoration_height);
1197}
1198
1199static void
1200frame_redraw_handler(struct widget *widget, void *data)
1201{
1202 struct frame *frame = data;
1203 cairo_t *cr;
1204 cairo_text_extents_t extents;
1205 cairo_surface_t *source;
1206 int width, height, shadow_dx = 3, shadow_dy = 3;
1207 struct window *window = widget->window;
1208
1209 width = widget->allocation.width;
1210 height = widget->allocation.height;
1211
1212 cr = cairo_create(window->cairo_surface);
1213
1214 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1215 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1216 cairo_paint(cr);
1217
1218 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1219 tile_mask(cr, window->display->shadow,
1220 shadow_dx, shadow_dy, width, height,
1221 frame->margin + 10 - shadow_dx,
1222 frame->margin + 10 - shadow_dy);
1223
1224 if (window->keyboard_device)
1225 source = window->display->active_frame;
1226 else
1227 source = window->display->inactive_frame;
1228
1229 tile_source(cr, source, 0, 0, width, height,
1230 frame->margin + 10, frame->margin + 50);
1231
1232 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1233 cairo_set_font_size(cr, 14);
1234 cairo_text_extents(cr, window->title, &extents);
1235 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1236 if (window->keyboard_device)
1237 cairo_set_source_rgb(cr, 0, 0, 0);
1238 else
1239 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1240 cairo_show_text(cr, window->title);
1241
1242 cairo_destroy(cr);
1243}
1244
1245static int
1246frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1247{
1248 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001249 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 const int grip_size = 8;
1251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001257 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 hlocation = WINDOW_RESIZING_RIGHT;
1260 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001264 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001268 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001269 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 vlocation = WINDOW_RESIZING_BOTTOM;
1271 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001272 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001274 location = vlocation | hlocation;
1275 if (location & WINDOW_EXTERIOR)
1276 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278 location = WINDOW_TITLEBAR;
1279 else if (location == WINDOW_INTERIOR)
1280 location = WINDOW_CLIENT_AREA;
1281
1282 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283}
1284
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001285static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 switch (location) {
1292 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001293 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001295 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001296 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001297 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001298 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001299 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001300 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001301 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001302 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001303 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001304 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001305 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001306 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001307 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001308 case WINDOW_EXTERIOR:
1309 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001311 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001312 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001313}
1314
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001315static void
1316frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001317{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001318 switch (index) {
1319 case 0: /* close */
1320 if (window->close_handler)
1321 window->close_handler(window->parent,
1322 window->user_data);
1323 else
1324 display_exit(window->display);
1325 break;
1326 case 1: /* fullscreen */
1327 /* we don't have a way to get out of fullscreen for now */
1328 window_set_fullscreen(window, 1);
1329 break;
1330 case 2: /* rotate */
1331 case 3: /* scale */
1332 break;
1333 }
1334}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001336void
1337window_show_frame_menu(struct window *window,
1338 struct input *input, uint32_t time)
1339{
1340 int32_t x, y;
1341
1342 static const char *entries[] = {
1343 "Close", "Fullscreen", "Rotate", "Scale"
1344 };
1345
1346 input_get_position(input, &x, &y);
1347 window_show_menu(window->display, input, time, window,
1348 x - 10, y - 10, frame_menu_func, entries, 4);
1349}
1350
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001351static int
1352frame_enter_handler(struct widget *widget,
1353 struct input *input, uint32_t time,
1354 int32_t x, int32_t y, void *data)
1355{
1356 return frame_get_pointer_image_for_location(data, input);
1357}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001358
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001359static int
1360frame_motion_handler(struct widget *widget,
1361 struct input *input, uint32_t time,
1362 int32_t x, int32_t y, void *data)
1363{
1364 return frame_get_pointer_image_for_location(data, input);
1365}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001367static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001368frame_button_handler(struct widget *widget,
1369 struct input *input, uint32_t time,
1370 int button, int state, void *data)
1371
1372{
1373 struct frame *frame = data;
1374 struct window *window = widget->window;
1375 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001376 location = frame_get_pointer_location(frame, input->sx, input->sy);
1377
1378 if (window->display->shell && button == BTN_LEFT && state == 1) {
1379 switch (location) {
1380 case WINDOW_TITLEBAR:
1381 if (!window->shell_surface)
1382 break;
1383 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001384 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385 wl_shell_surface_move(window->shell_surface,
1386 input_get_input_device(input),
1387 time);
1388 break;
1389 case WINDOW_RESIZING_TOP:
1390 case WINDOW_RESIZING_BOTTOM:
1391 case WINDOW_RESIZING_LEFT:
1392 case WINDOW_RESIZING_RIGHT:
1393 case WINDOW_RESIZING_TOP_LEFT:
1394 case WINDOW_RESIZING_TOP_RIGHT:
1395 case WINDOW_RESIZING_BOTTOM_LEFT:
1396 case WINDOW_RESIZING_BOTTOM_RIGHT:
1397 if (!window->shell_surface)
1398 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001399 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001400 wl_shell_surface_resize(window->shell_surface,
1401 input_get_input_device(input),
1402 time, location);
1403 break;
1404 }
1405 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001406 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001407 }
1408}
1409
1410struct widget *
1411frame_create(struct window *window, void *data)
1412{
1413 struct frame *frame;
1414
1415 frame = malloc(sizeof *frame);
1416 memset(frame, 0, sizeof *frame);
1417
1418 frame->widget = window_add_widget(window, frame);
1419 frame->child = widget_add_widget(frame->widget, data);
1420 frame->margin = 16;
1421
1422 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1423 widget_set_resize_handler(frame->widget, frame_resize_handler);
1424 widget_set_enter_handler(frame->widget, frame_enter_handler);
1425 widget_set_motion_handler(frame->widget, frame_motion_handler);
1426 widget_set_button_handler(frame->widget, frame_button_handler);
1427
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001428 window->frame = frame;
1429
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001430 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001431}
1432
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001433static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001434frame_destroy(struct frame *frame)
1435{
1436 /* frame->child must be destroyed by the application */
1437 widget_destroy(frame->widget);
1438 free(frame);
1439}
1440
1441static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001442input_set_focus_widget(struct input *input, struct widget *focus,
1443 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001444{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001445 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001446 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001447
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001448 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001449 return;
1450
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001451 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001452 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001453 widget = old;
1454 if (input->grab)
1455 widget = input->grab;
1456 if (widget->leave_handler)
1457 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001458 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001459 }
1460
1461 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001462 widget = focus;
1463 if (input->grab)
1464 widget = input->grab;
1465 if (widget->enter_handler)
1466 pointer = widget->enter_handler(focus, input, time,
1467 x, y,
1468 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001469 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001470
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001471 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001472 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001473}
1474
1475static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001476input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001477 uint32_t time, 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
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001484 input->sx = sx;
1485 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001486
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001487 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001488 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001489 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001490 }
1491
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001492 if (input->grab)
1493 widget = input->grab;
1494 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001495 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001496 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001497 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001498 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001499 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001500
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001501 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001502}
1503
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001504void
1505input_grab(struct input *input, struct widget *widget, uint32_t button)
1506{
1507 input->grab = widget;
1508 input->grab_button = button;
1509}
1510
1511void
1512input_ungrab(struct input *input, uint32_t time)
1513{
1514 struct widget *widget;
1515
1516 input->grab = NULL;
1517 if (input->pointer_focus) {
1518 widget = widget_find_widget(input->pointer_focus->widget,
1519 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001520 input_set_focus_widget(input, widget,
1521 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001522 }
1523}
1524
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001525static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001526input_handle_button(void *data,
1527 struct wl_input_device *input_device,
1528 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001529{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001531 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001532
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001533 if (input->focus_widget && input->grab == NULL && state)
1534 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001535
Neil Roberts6b28aad2012-01-23 19:11:18 +00001536 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001537 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001538 (*widget->button_handler)(widget,
1539 input, time,
1540 button, state,
1541 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001542
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001543 if (input->grab && input->grab_button == button && !state)
1544 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001545}
1546
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001547static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001548input_handle_key(void *data, struct wl_input_device *input_device,
1549 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001550{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 struct input *input = data;
1552 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001553 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001554 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001555
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001556 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001557 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001558 return;
1559
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001560 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001561 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001562 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1563 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001564
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001565 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1566
1567 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001568 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001569 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001571
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001572 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1573 if (state)
1574 window_set_maximized(window,
1575 window->type != TYPE_MAXIMIZED);
1576 } else if (window->key_handler) {
1577 (*window->key_handler)(window, input, time, key,
1578 sym, state, window->user_data);
1579 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001580}
1581
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001582static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001583input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001584{
1585 struct window *window = input->pointer_focus;
1586
1587 if (!window)
1588 return;
1589
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001590 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001591
Pekka Paalanene1207c72011-12-16 12:02:09 +02001592 input->pointer_focus = NULL;
1593 input->current_pointer_image = POINTER_UNSET;
1594}
1595
1596static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001597input_handle_pointer_focus(void *data,
1598 struct wl_input_device *input_device,
1599 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001600 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001601{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001602 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001603 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001604 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001605
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001606 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001607 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001608 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001609
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001610 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001611 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001612 window = input->pointer_focus;
1613
Kristian Høgsberg59826582011-01-20 11:56:57 -05001614 input->sx = sx;
1615 input->sy = sy;
1616
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001618 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001619 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001620}
1621
1622static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001623input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001624{
1625 struct window *window = input->keyboard_focus;
1626
1627 if (!window)
1628 return;
1629
1630 window->keyboard_device = NULL;
1631 if (window->keyboard_focus_handler)
1632 (*window->keyboard_focus_handler)(window, NULL,
1633 window->user_data);
1634
1635 input->keyboard_focus = NULL;
1636}
1637
1638static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001639input_handle_keyboard_focus(void *data,
1640 struct wl_input_device *input_device,
1641 uint32_t time,
1642 struct wl_surface *surface,
1643 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001644{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001645 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001646 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001647 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001648 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001649
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001650 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001651
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001652 if (surface)
1653 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001654
1655 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001656 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001657 for (k = keys->data; k < end; k++)
1658 input->modifiers |= d->xkb->map->modmap[*k];
1659
1660 window = input->keyboard_focus;
1661 if (window) {
1662 window->keyboard_device = input;
1663 if (window->keyboard_focus_handler)
1664 (*window->keyboard_focus_handler)(window,
1665 window->keyboard_device,
1666 window->user_data);
1667 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001668}
1669
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001670static void
1671input_handle_touch_down(void *data,
1672 struct wl_input_device *wl_input_device,
1673 uint32_t time, struct wl_surface *surface,
1674 int32_t id, int32_t x, int32_t y)
1675{
1676}
1677
1678static void
1679input_handle_touch_up(void *data,
1680 struct wl_input_device *wl_input_device,
1681 uint32_t time, int32_t id)
1682{
1683}
1684
1685static void
1686input_handle_touch_motion(void *data,
1687 struct wl_input_device *wl_input_device,
1688 uint32_t time, int32_t id, int32_t x, int32_t y)
1689{
1690}
1691
1692static void
1693input_handle_touch_frame(void *data,
1694 struct wl_input_device *wl_input_device)
1695{
1696}
1697
1698static void
1699input_handle_touch_cancel(void *data,
1700 struct wl_input_device *wl_input_device)
1701{
1702}
1703
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001704static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001705 input_handle_motion,
1706 input_handle_button,
1707 input_handle_key,
1708 input_handle_pointer_focus,
1709 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001710 input_handle_touch_down,
1711 input_handle_touch_up,
1712 input_handle_touch_motion,
1713 input_handle_touch_frame,
1714 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001715};
1716
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001717void
1718input_get_position(struct input *input, int32_t *x, int32_t *y)
1719{
1720 *x = input->sx;
1721 *y = input->sy;
1722}
1723
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001724struct wl_input_device *
1725input_get_input_device(struct input *input)
1726{
1727 return input->input_device;
1728}
1729
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001730uint32_t
1731input_get_modifiers(struct input *input)
1732{
1733 return input->modifiers;
1734}
1735
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001736struct widget *
1737input_get_focus_widget(struct input *input)
1738{
1739 return input->focus_widget;
1740}
1741
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001742struct data_offer {
1743 struct wl_data_offer *offer;
1744 struct input *input;
1745 struct wl_array types;
1746 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001747
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001748 struct task io_task;
1749 int fd;
1750 data_func_t func;
1751 int32_t x, y;
1752 void *user_data;
1753};
1754
1755static void
1756data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1757{
1758 struct data_offer *offer = data;
1759 char **p;
1760
1761 p = wl_array_add(&offer->types, sizeof *p);
1762 *p = strdup(type);
1763}
1764
1765static const struct wl_data_offer_listener data_offer_listener = {
1766 data_offer_offer,
1767};
1768
1769static void
1770data_offer_destroy(struct data_offer *offer)
1771{
1772 char **p;
1773
1774 offer->refcount--;
1775 if (offer->refcount == 0) {
1776 wl_data_offer_destroy(offer->offer);
1777 for (p = offer->types.data; *p; p++)
1778 free(*p);
1779 wl_array_release(&offer->types);
1780 free(offer);
1781 }
1782}
1783
1784static void
1785data_device_data_offer(void *data,
1786 struct wl_data_device *data_device, uint32_t id)
1787{
1788 struct data_offer *offer;
1789
1790 offer = malloc(sizeof *offer);
1791
1792 wl_array_init(&offer->types);
1793 offer->refcount = 1;
1794 offer->input = data;
1795
1796 /* FIXME: Generate typesafe wrappers for this */
1797 offer->offer = (struct wl_data_offer *)
1798 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1799 id, &wl_data_offer_interface);
1800
1801 wl_data_offer_add_listener(offer->offer,
1802 &data_offer_listener, offer);
1803}
1804
1805static void
1806data_device_enter(void *data, struct wl_data_device *data_device,
1807 uint32_t time, struct wl_surface *surface,
1808 int32_t x, int32_t y, struct wl_data_offer *offer)
1809{
1810 struct input *input = data;
1811 struct window *window;
1812 char **p;
1813
1814 input->drag_offer = wl_data_offer_get_user_data(offer);
1815 window = wl_surface_get_user_data(surface);
1816 input->pointer_focus = window;
1817
1818 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1819 *p = NULL;
1820
1821 window = input->pointer_focus;
1822 if (window->data_handler)
1823 window->data_handler(window, input, time, x, y,
1824 input->drag_offer->types.data,
1825 window->user_data);
1826}
1827
1828static void
1829data_device_leave(void *data, struct wl_data_device *data_device)
1830{
1831 struct input *input = data;
1832
1833 data_offer_destroy(input->drag_offer);
1834 input->drag_offer = NULL;
1835}
1836
1837static void
1838data_device_motion(void *data, struct wl_data_device *data_device,
1839 uint32_t time, int32_t x, int32_t y)
1840{
1841 struct input *input = data;
1842 struct window *window = input->pointer_focus;
1843
1844 input->sx = x;
1845 input->sy = y;
1846
1847 if (window->data_handler)
1848 window->data_handler(window, input, time, x, y,
1849 input->drag_offer->types.data,
1850 window->user_data);
1851}
1852
1853static void
1854data_device_drop(void *data, struct wl_data_device *data_device)
1855{
1856 struct input *input = data;
1857 struct window *window = input->pointer_focus;
1858
1859 if (window->drop_handler)
1860 window->drop_handler(window, input,
1861 input->sx, input->sy, window->user_data);
1862}
1863
1864static void
1865data_device_selection(void *data,
1866 struct wl_data_device *wl_data_device,
1867 struct wl_data_offer *offer)
1868{
1869 struct input *input = data;
1870 char **p;
1871
1872 if (input->selection_offer)
1873 data_offer_destroy(input->selection_offer);
1874
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001875 if (offer) {
1876 input->selection_offer = wl_data_offer_get_user_data(offer);
1877 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1878 *p = NULL;
1879 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001880}
1881
1882static const struct wl_data_device_listener data_device_listener = {
1883 data_device_data_offer,
1884 data_device_enter,
1885 data_device_leave,
1886 data_device_motion,
1887 data_device_drop,
1888 data_device_selection
1889};
1890
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001891void
1892input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1893{
1894 struct display *display = input->display;
1895 struct wl_buffer *buffer;
1896 cairo_surface_t *surface;
1897
1898 if (pointer == input->current_pointer_image)
1899 return;
1900
1901 input->current_pointer_image = pointer;
1902 surface = display->pointer_surfaces[pointer];
1903
1904 if (!surface)
1905 return;
1906
1907 buffer = display_get_buffer_for_surface(display, surface);
1908 wl_input_device_attach(input->input_device, time, buffer,
1909 pointer_images[pointer].hotspot_x,
1910 pointer_images[pointer].hotspot_y);
1911}
1912
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001913struct wl_data_device *
1914input_get_data_device(struct input *input)
1915{
1916 return input->data_device;
1917}
1918
1919void
1920input_set_selection(struct input *input,
1921 struct wl_data_source *source, uint32_t time)
1922{
1923 wl_data_device_set_selection(input->data_device, source, time);
1924}
1925
1926void
1927input_accept(struct input *input, uint32_t time, const char *type)
1928{
1929 wl_data_offer_accept(input->drag_offer->offer, time, type);
1930}
1931
1932static void
1933offer_io_func(struct task *task, uint32_t events)
1934{
1935 struct data_offer *offer =
1936 container_of(task, struct data_offer, io_task);
1937 unsigned int len;
1938 char buffer[4096];
1939
1940 len = read(offer->fd, buffer, sizeof buffer);
1941 offer->func(buffer, len,
1942 offer->x, offer->y, offer->user_data);
1943
1944 if (len == 0) {
1945 close(offer->fd);
1946 data_offer_destroy(offer);
1947 }
1948}
1949
1950static void
1951data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1952 data_func_t func, void *user_data)
1953{
1954 int p[2];
1955
1956 pipe2(p, O_CLOEXEC);
1957 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1958 close(p[1]);
1959
1960 offer->io_task.run = offer_io_func;
1961 offer->fd = p[0];
1962 offer->func = func;
1963 offer->refcount++;
1964 offer->user_data = user_data;
1965
1966 display_watch_fd(offer->input->display,
1967 offer->fd, EPOLLIN, &offer->io_task);
1968}
1969
1970void
1971input_receive_drag_data(struct input *input, const char *mime_type,
1972 data_func_t func, void *data)
1973{
1974 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1975 input->drag_offer->x = input->sx;
1976 input->drag_offer->y = input->sy;
1977}
1978
1979int
1980input_receive_selection_data(struct input *input, const char *mime_type,
1981 data_func_t func, void *data)
1982{
1983 char **p;
1984
1985 if (input->selection_offer == NULL)
1986 return -1;
1987
1988 for (p = input->selection_offer->types.data; *p; p++)
1989 if (strcmp(mime_type, *p) == 0)
1990 break;
1991
1992 if (*p == NULL)
1993 return -1;
1994
1995 data_offer_receive_data(input->selection_offer,
1996 mime_type, func, data);
1997 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001998}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001999
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002000int
2001input_receive_selection_data_to_fd(struct input *input,
2002 const char *mime_type, int fd)
2003{
2004 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2005
2006 return 0;
2007}
2008
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002009void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002010window_move(struct window *window, struct input *input, uint32_t time)
2011{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002012 if (!window->shell_surface)
2013 return;
2014
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002015 wl_shell_surface_move(window->shell_surface,
2016 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002017}
2018
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002019static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002020idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002021{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002022 struct window *window =
2023 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002024 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002025
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002026 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002027 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002028 widget_set_allocation(widget,
2029 window->pending_allocation.x,
2030 window->pending_allocation.y,
2031 window->pending_allocation.width,
2032 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002033
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002034 if (widget->resize_handler)
2035 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002036 widget->allocation.width,
2037 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002038 widget->user_data);
2039
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002040 if (window->allocation.width != widget->allocation.width ||
2041 window->allocation.height != widget->allocation.height) {
2042 window->allocation = widget->allocation;
2043 window_schedule_redraw(window);
2044 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002045}
2046
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002047void
2048window_schedule_resize(struct window *window, int width, int height)
2049{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002050 window->pending_allocation.x = 0;
2051 window->pending_allocation.y = 0;
2052 window->pending_allocation.width = width;
2053 window->pending_allocation.height = height;
2054
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002055 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002056 window->resize_task.run = idle_resize;
2057 display_defer(window->display, &window->resize_task);
2058 window->resize_scheduled = 1;
2059 }
2060}
2061
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002062void
2063widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2064{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002065 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002066}
2067
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002068static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002070 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002072{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002074
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002075 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002076 return;
2077
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002078 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002079 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002080}
2081
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002083menu_destroy(struct menu *menu)
2084{
2085 widget_destroy(menu->widget);
2086 window_destroy(menu->window);
2087 free(menu);
2088}
2089
2090static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002091handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2092{
2093 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 struct menu *menu = window->widget->user_data;
2095
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002096 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002097 * device. Or just use wl_callback. And this really needs to
2098 * be a window vfunc that the menu can set. And we need the
2099 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002100
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002101 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002102 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002103 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002104}
2105
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002107 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002108 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002109};
2110
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002111void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002112window_get_allocation(struct window *window,
2113 struct rectangle *allocation)
2114{
2115 *allocation = window->allocation;
2116}
2117
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002118static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002119widget_redraw(struct widget *widget)
2120{
2121 struct widget *child;
2122
2123 if (widget->redraw_handler)
2124 widget->redraw_handler(widget, widget->user_data);
2125 wl_list_for_each(child, &widget->child_list, link)
2126 widget_redraw(child);
2127}
2128
2129static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002130idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002131{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002132 struct window *window =
2133 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002134
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002135 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002136 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002137 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002138 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002139}
2140
2141void
2142window_schedule_redraw(struct window *window)
2143{
2144 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002145 window->redraw_task.run = idle_redraw;
2146 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002147 window->redraw_scheduled = 1;
2148 }
2149}
2150
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002151void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002152window_set_custom(struct window *window)
2153{
2154 window->type = TYPE_CUSTOM;
2155}
2156
2157void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002158window_set_fullscreen(struct window *window, int fullscreen)
2159{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002160 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002161 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002162
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002163 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002164 return;
2165
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002166 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002167 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002168 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002169 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002170 width = output->allocation.width;
2171 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002172 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002173 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 width = window->saved_allocation.width;
2175 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002176 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002177
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002179}
2180
2181void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002182window_set_maximized(struct window *window, int maximized)
2183{
2184 if (!window->display->shell)
2185 return;
2186
2187 if ((window->type == TYPE_MAXIMIZED) == maximized)
2188 return;
2189
2190 if (window->type == TYPE_TOPLEVEL) {
2191 window->saved_allocation = window->allocation;
2192 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2193 window->type = TYPE_MAXIMIZED;
2194 } else {
2195 wl_shell_surface_set_toplevel(window->shell_surface);
2196 window->type = TYPE_TOPLEVEL;
2197 window_schedule_resize(window,
2198 window->saved_allocation.width,
2199 window->saved_allocation.height);
2200 }
2201}
2202
2203void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002204window_set_user_data(struct window *window, void *data)
2205{
2206 window->user_data = data;
2207}
2208
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002209void *
2210window_get_user_data(struct window *window)
2211{
2212 return window->user_data;
2213}
2214
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002215void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002216window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002217 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002218{
2219 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002220}
2221
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002222void
2223window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002224 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002225{
2226 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002227}
2228
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002229void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002230window_set_data_handler(struct window *window, window_data_handler_t handler)
2231{
2232 window->data_handler = handler;
2233}
2234
2235void
2236window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2237{
2238 window->drop_handler = handler;
2239}
2240
2241void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002242window_set_close_handler(struct window *window,
2243 window_close_handler_t handler)
2244{
2245 window->close_handler = handler;
2246}
2247
2248void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002249window_set_transparent(struct window *window, int transparent)
2250{
2251 window->transparent = transparent;
2252}
2253
2254void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002255window_set_title(struct window *window, const char *title)
2256{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002257 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002258 window->title = strdup(title);
2259}
2260
2261const char *
2262window_get_title(struct window *window)
2263{
2264 return window->title;
2265}
2266
2267void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002268display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2269 int32_t x, int32_t y, int32_t width, int32_t height)
2270{
2271 struct wl_buffer *buffer;
2272
2273 buffer = display_get_buffer_for_surface(display, cairo_surface);
2274
2275 wl_buffer_damage(buffer, x, y, width, height);
2276}
2277
2278void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002279window_damage(struct window *window, int32_t x, int32_t y,
2280 int32_t width, int32_t height)
2281{
2282 wl_surface_damage(window->surface, x, y, width, height);
2283}
2284
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002286window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002287{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002288 struct window *window;
2289
2290 window = malloc(sizeof *window);
2291 if (window == NULL)
2292 return NULL;
2293
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002294 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002295 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002296 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002297 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002298 if (display->shell) {
2299 window->shell_surface =
2300 wl_shell_get_shell_surface(display->shell,
2301 window->surface);
2302 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002303 window->allocation.x = 0;
2304 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002305 window->allocation.width = 0;
2306 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002307 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002308 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002309 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002310
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002311 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002312#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002313 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2314 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002315#else
2316 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2317#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002318 else
2319 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002320
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002321 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002322 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002323
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002324 if (window->shell_surface) {
2325 wl_shell_surface_set_user_data(window->shell_surface, window);
2326 wl_shell_surface_add_listener(window->shell_surface,
2327 &shell_surface_listener, window);
2328 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002329
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002330 return window;
2331}
2332
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002333struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002334window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002335{
2336 struct window *window;
2337
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002338 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002339 if (!window)
2340 return NULL;
2341
2342 return window;
2343}
2344
2345struct window *
2346window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002347 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002348{
2349 struct window *window;
2350
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002351 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002352 if (!window)
2353 return NULL;
2354
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002355 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002356 window->x = x;
2357 window->y = y;
2358
2359 return window;
2360}
2361
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002362static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002363menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002364{
2365 int next;
2366
2367 next = (sy - 8) / 20;
2368 if (menu->current != next) {
2369 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002370 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002371 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002372}
2373
2374static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002375menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002376 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002377 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002378{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002379 struct menu *menu = data;
2380
2381 if (widget == menu->widget)
2382 menu_set_item(data, y);
2383
2384 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002385}
2386
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002387static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002388menu_enter_handler(struct widget *widget,
2389 struct input *input, uint32_t time,
2390 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002391{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002392 struct menu *menu = data;
2393
2394 if (widget == menu->widget)
2395 menu_set_item(data, y);
2396
2397 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002398}
2399
2400static void
2401menu_leave_handler(struct widget *widget, struct input *input, void *data)
2402{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002403 struct menu *menu = data;
2404
2405 if (widget == menu->widget)
2406 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002407}
2408
2409static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002410menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411 struct input *input, uint32_t time,
2412 int button, int state, void *data)
2413
2414{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002415 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002416
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002417 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002418 /* Either relase after press-drag-release or
2419 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002420 menu->func(menu->window->parent,
2421 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002422 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002423 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002424 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002425}
2426
2427static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002428menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002429{
2430 cairo_t *cr;
2431 const int32_t r = 3, margin = 3;
2432 struct menu *menu = data;
2433 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002434 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002435
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436 cr = cairo_create(window->cairo_surface);
2437 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2438 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2439 cairo_paint(cr);
2440
2441 width = window->allocation.width;
2442 height = window->allocation.height;
2443 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002444
2445 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002446 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2447 cairo_fill(cr);
2448
2449 for (i = 0; i < menu->count; i++) {
2450 if (i == menu->current) {
2451 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2452 cairo_rectangle(cr, margin, i * 20 + margin,
2453 width - 2 * margin, 20);
2454 cairo_fill(cr);
2455 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2456 cairo_move_to(cr, 10, i * 20 + 16);
2457 cairo_show_text(cr, menu->entries[i]);
2458 } else {
2459 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2460 cairo_move_to(cr, 10, i * 20 + 16);
2461 cairo_show_text(cr, menu->entries[i]);
2462 }
2463 }
2464
2465 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002466}
2467
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002468void
2469window_show_menu(struct display *display,
2470 struct input *input, uint32_t time, struct window *parent,
2471 int32_t x, int32_t y,
2472 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002473{
2474 struct window *window;
2475 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002476 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002477
2478 menu = malloc(sizeof *menu);
2479 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002480 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002481
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002482 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002483 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002484 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002485
2486 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002487 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002488 menu->entries = entries;
2489 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002490 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002491 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002492 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002493 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002494 window->type = TYPE_MENU;
2495 window->x = x;
2496 window->y = y;
2497
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002498 wl_shell_surface_set_popup(window->shell_surface,
2499 input->input_device, time,
2500 window->parent->shell_surface,
2501 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002502
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002503 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002504 widget_set_enter_handler(menu->widget, menu_enter_handler);
2505 widget_set_leave_handler(menu->widget, menu_leave_handler);
2506 widget_set_motion_handler(menu->widget, menu_motion_handler);
2507 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002508
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002509 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002510 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002511}
2512
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002513void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002514window_set_buffer_type(struct window *window, enum window_buffer_type type)
2515{
2516 window->buffer_type = type;
2517}
2518
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002519
2520static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002521display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002522 struct wl_output *wl_output,
2523 int x, int y,
2524 int physical_width,
2525 int physical_height,
2526 int subpixel,
2527 const char *make,
2528 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002529{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002530 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002531
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002532 output->allocation.x = x;
2533 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002534}
2535
2536static void
2537display_handle_mode(void *data,
2538 struct wl_output *wl_output,
2539 uint32_t flags,
2540 int width,
2541 int height,
2542 int refresh)
2543{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002544 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002545 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002546
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002547 if (flags & WL_OUTPUT_MODE_CURRENT) {
2548 output->allocation.width = width;
2549 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002550 if (display->output_configure_handler)
2551 (*display->output_configure_handler)(
2552 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002553 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002554}
2555
2556static const struct wl_output_listener output_listener = {
2557 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002558 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002559};
2560
2561static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002562display_add_output(struct display *d, uint32_t id)
2563{
2564 struct output *output;
2565
2566 output = malloc(sizeof *output);
2567 if (output == NULL)
2568 return;
2569
2570 memset(output, 0, sizeof *output);
2571 output->display = d;
2572 output->output =
2573 wl_display_bind(d->display, id, &wl_output_interface);
2574 wl_list_insert(d->output_list.prev, &output->link);
2575
2576 wl_output_add_listener(output->output, &output_listener, output);
2577}
2578
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002579static void
2580output_destroy(struct output *output)
2581{
2582 if (output->destroy_handler)
2583 (*output->destroy_handler)(output, output->user_data);
2584
2585 wl_output_destroy(output->output);
2586 wl_list_remove(&output->link);
2587 free(output);
2588}
2589
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002590void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002591display_set_output_configure_handler(struct display *display,
2592 display_output_handler_t handler)
2593{
2594 struct output *output;
2595
2596 display->output_configure_handler = handler;
2597 if (!handler)
2598 return;
2599
2600 wl_list_for_each(output, &display->output_list, link)
2601 (*display->output_configure_handler)(output,
2602 display->user_data);
2603}
2604
2605void
2606output_set_user_data(struct output *output, void *data)
2607{
2608 output->user_data = data;
2609}
2610
2611void *
2612output_get_user_data(struct output *output)
2613{
2614 return output->user_data;
2615}
2616
2617void
2618output_set_destroy_handler(struct output *output,
2619 display_output_handler_t handler)
2620{
2621 output->destroy_handler = handler;
2622 /* FIXME: implement this, once we have way to remove outputs */
2623}
2624
2625void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002626output_get_allocation(struct output *output, struct rectangle *allocation)
2627{
2628 *allocation = output->allocation;
2629}
2630
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002631struct wl_output *
2632output_get_wl_output(struct output *output)
2633{
2634 return output->output;
2635}
2636
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002637static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002638display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002639{
2640 struct input *input;
2641
2642 input = malloc(sizeof *input);
2643 if (input == NULL)
2644 return;
2645
2646 memset(input, 0, sizeof *input);
2647 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002648 input->input_device =
2649 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002650 input->pointer_focus = NULL;
2651 input->keyboard_focus = NULL;
2652 wl_list_insert(d->input_list.prev, &input->link);
2653
2654 wl_input_device_add_listener(input->input_device,
2655 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002656 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002657
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002658 input->data_device =
2659 wl_data_device_manager_get_data_device(d->data_device_manager,
2660 input->input_device);
2661 wl_data_device_add_listener(input->data_device,
2662 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002663}
2664
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002665static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002666input_destroy(struct input *input)
2667{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002668 input_remove_keyboard_focus(input);
2669 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002670
2671 if (input->drag_offer)
2672 data_offer_destroy(input->drag_offer);
2673
2674 if (input->selection_offer)
2675 data_offer_destroy(input->selection_offer);
2676
2677 wl_data_device_destroy(input->data_device);
2678 wl_list_remove(&input->link);
2679 wl_input_device_destroy(input->input_device);
2680 free(input);
2681}
2682
2683static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002684display_handle_global(struct wl_display *display, uint32_t id,
2685 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002686{
2687 struct display *d = data;
2688
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002689 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002690 d->compositor =
2691 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002692 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002693 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002694 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002695 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002696 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002697 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002698 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002699 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002700 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2701 d->data_device_manager =
2702 wl_display_bind(display, id,
2703 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002704 }
2705}
2706
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002707static void
2708display_render_frame(struct display *d)
2709{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002710 int radius = 8;
2711 cairo_t *cr;
2712
2713 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2714 cr = cairo_create(d->shadow);
2715 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2716 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2717 rounded_rect(cr, 16, 16, 112, 112, radius);
2718 cairo_fill(cr);
2719 cairo_destroy(cr);
2720 blur_surface(d->shadow, 64);
2721
2722 d->active_frame =
2723 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2724 cr = cairo_create(d->active_frame);
2725 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2726 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2727 rounded_rect(cr, 16, 16, 112, 112, radius);
2728 cairo_fill(cr);
2729 cairo_destroy(cr);
2730
2731 d->inactive_frame =
2732 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2733 cr = cairo_create(d->inactive_frame);
2734 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2735 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2736 rounded_rect(cr, 16, 16, 112, 112, radius);
2737 cairo_fill(cr);
2738 cairo_destroy(cr);
2739}
2740
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002741static void
2742init_xkb(struct display *d)
2743{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002744 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002745
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002746 names.rules = "evdev";
2747 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002748 names.layout = option_xkb_layout;
2749 names.variant = option_xkb_variant;
2750 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002751
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002752 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002753 if (!d->xkb) {
2754 fprintf(stderr, "Failed to compile keymap\n");
2755 exit(1);
2756 }
2757}
2758
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002759static void
2760fini_xkb(struct display *display)
2761{
2762 xkb_free_keymap(display->xkb);
2763}
2764
Yuval Fledel45568f62010-12-06 09:18:12 -05002765static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002766init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002767{
2768 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002769 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002770
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002771 static const EGLint argb_cfg_attribs[] = {
2772 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002773 EGL_RED_SIZE, 1,
2774 EGL_GREEN_SIZE, 1,
2775 EGL_BLUE_SIZE, 1,
2776 EGL_ALPHA_SIZE, 1,
2777 EGL_DEPTH_SIZE, 1,
2778 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2779 EGL_NONE
2780 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002781
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002782 static const EGLint rgb_cfg_attribs[] = {
2783 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2784 EGL_RED_SIZE, 1,
2785 EGL_GREEN_SIZE, 1,
2786 EGL_BLUE_SIZE, 1,
2787 EGL_ALPHA_SIZE, 0,
2788 EGL_DEPTH_SIZE, 1,
2789 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2790 EGL_NONE
2791 };
2792
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002793#ifdef USE_CAIRO_GLESV2
2794 static const EGLint context_attribs[] = {
2795 EGL_CONTEXT_CLIENT_VERSION, 2,
2796 EGL_NONE
2797 };
2798 EGLint api = EGL_OPENGL_ES_API;
2799#else
2800 EGLint *context_attribs = NULL;
2801 EGLint api = EGL_OPENGL_API;
2802#endif
2803
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002804 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002805 if (!eglInitialize(d->dpy, &major, &minor)) {
2806 fprintf(stderr, "failed to initialize display\n");
2807 return -1;
2808 }
2809
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002810 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002811 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2812 return -1;
2813 }
2814
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002815 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2816 &d->argb_config, 1, &n) || n != 1) {
2817 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002818 return -1;
2819 }
2820
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002821 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2822 &d->rgb_config, 1, &n) || n != 1) {
2823 fprintf(stderr, "failed to choose rgb config\n");
2824 return -1;
2825 }
2826
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002827 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2828 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002829 if (d->rgb_ctx == NULL) {
2830 fprintf(stderr, "failed to create context\n");
2831 return -1;
2832 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002833 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002834 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002835 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002836 fprintf(stderr, "failed to create context\n");
2837 return -1;
2838 }
2839
Benjamin Franzke0c991632011-09-27 21:57:31 +02002840 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002841 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002842 return -1;
2843 }
2844
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002845#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002846 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2847 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002848 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002849 return -1;
2850 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002851 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2852 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2853 fprintf(stderr, "failed to get cairo egl argb device\n");
2854 return -1;
2855 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002856#endif
2857
2858 return 0;
2859}
2860
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002861static void
2862fini_egl(struct display *display)
2863{
2864#ifdef HAVE_CAIRO_EGL
2865 cairo_device_destroy(display->argb_device);
2866 cairo_device_destroy(display->rgb_device);
2867#endif
2868
2869 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2870 EGL_NO_CONTEXT);
2871
2872 eglTerminate(display->dpy);
2873 eglReleaseThread();
2874}
2875
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002876static int
2877event_mask_update(uint32_t mask, void *data)
2878{
2879 struct display *d = data;
2880
2881 d->mask = mask;
2882
2883 return 0;
2884}
2885
2886static void
2887handle_display_data(struct task *task, uint32_t events)
2888{
2889 struct display *display =
2890 container_of(task, struct display, display_task);
2891
2892 wl_display_iterate(display->display, display->mask);
2893}
2894
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002895struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002896display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002897{
2898 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002899 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002900 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002901 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002902
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002903 g_type_init();
2904
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002905 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002906 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002907 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002908
2909 xkb_option_group = g_option_group_new("xkb",
2910 "Keyboard options",
2911 "Show all XKB options",
2912 NULL, NULL);
2913 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2914 g_option_context_add_group (context, xkb_option_group);
2915
2916 if (!g_option_context_parse(context, argc, argv, &error)) {
2917 fprintf(stderr, "option parsing failed: %s\n", error->message);
2918 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002919 }
2920
Tim Wiederhake748f6722011-01-23 23:25:25 +01002921 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002922
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002923 d = malloc(sizeof *d);
2924 if (d == NULL)
2925 return NULL;
2926
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002927 memset(d, 0, sizeof *d);
2928
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002929 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002930 if (d->display == NULL) {
2931 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002932 return NULL;
2933 }
2934
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002935 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2936 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2937 d->display_task.run = handle_display_data;
2938 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2939
2940 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002941 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002942 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002943
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002944 /* Set up listener so we'll catch all events. */
2945 wl_display_add_global_listener(d->display,
2946 display_handle_global, d);
2947
2948 /* Process connection events. */
2949 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002950 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002951 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002952
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002953 d->image_target_texture_2d =
2954 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2955 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2956 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2957
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002958 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002959
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002960 display_render_frame(d);
2961
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002962 wl_list_init(&d->window_list);
2963
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002964 init_xkb(d);
2965
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002966 return d;
2967}
2968
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002969static void
2970display_destroy_outputs(struct display *display)
2971{
2972 struct output *tmp;
2973 struct output *output;
2974
2975 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2976 output_destroy(output);
2977}
2978
Pekka Paalanene1207c72011-12-16 12:02:09 +02002979static void
2980display_destroy_inputs(struct display *display)
2981{
2982 struct input *tmp;
2983 struct input *input;
2984
2985 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2986 input_destroy(input);
2987}
2988
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002989void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002990display_destroy(struct display *display)
2991{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002992 if (!wl_list_empty(&display->window_list))
2993 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2994
2995 if (!wl_list_empty(&display->deferred_list))
2996 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2997
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002998 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002999 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003000
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003001 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003002
3003 cairo_surface_destroy(display->active_frame);
3004 cairo_surface_destroy(display->inactive_frame);
3005 cairo_surface_destroy(display->shadow);
3006 destroy_pointer_surfaces(display);
3007
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003008 fini_egl(display);
3009
Pekka Paalanenc2052982011-12-16 11:41:32 +02003010 if (display->shell)
3011 wl_shell_destroy(display->shell);
3012
3013 if (display->shm)
3014 wl_shm_destroy(display->shm);
3015
3016 if (display->data_device_manager)
3017 wl_data_device_manager_destroy(display->data_device_manager);
3018
3019 wl_compositor_destroy(display->compositor);
3020
3021 close(display->epoll_fd);
3022
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003023 wl_display_flush(display->display);
3024 wl_display_destroy(display->display);
3025 free(display);
3026}
3027
3028void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003029display_set_user_data(struct display *display, void *data)
3030{
3031 display->user_data = data;
3032}
3033
3034void *
3035display_get_user_data(struct display *display)
3036{
3037 return display->user_data;
3038}
3039
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003040struct wl_display *
3041display_get_display(struct display *display)
3042{
3043 return display->display;
3044}
3045
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003046struct output *
3047display_get_output(struct display *display)
3048{
3049 return container_of(display->output_list.next, struct output, link);
3050}
3051
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003052struct wl_compositor *
3053display_get_compositor(struct display *display)
3054{
3055 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003056}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003057
3058EGLDisplay
3059display_get_egl_display(struct display *d)
3060{
3061 return d->dpy;
3062}
3063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003064struct wl_data_source *
3065display_create_data_source(struct display *display)
3066{
3067 return wl_data_device_manager_create_data_source(display->data_device_manager);
3068}
3069
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003070EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003071display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003072{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003073 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003074}
3075
Benjamin Franzke0c991632011-09-27 21:57:31 +02003076EGLConfig
3077display_get_argb_egl_config(struct display *d)
3078{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003079 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003080}
3081
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003082struct wl_shell *
3083display_get_shell(struct display *display)
3084{
3085 return display->shell;
3086}
3087
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003089display_acquire_window_surface(struct display *display,
3090 struct window *window,
3091 EGLContext ctx)
3092{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003093#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003094 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003095 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003096
3097 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003098 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003099 device = cairo_surface_get_device(window->cairo_surface);
3100 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003101 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003102
Benjamin Franzke0c991632011-09-27 21:57:31 +02003103 if (!ctx) {
3104 if (device == display->rgb_device)
3105 ctx = display->rgb_ctx;
3106 else if (device == display->argb_device)
3107 ctx = display->argb_ctx;
3108 else
3109 assert(0);
3110 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003111
3112 data = cairo_surface_get_user_data(window->cairo_surface,
3113 &surface_data_key);
3114
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003115 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003116 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003117 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3118 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003119
3120 return 0;
3121#else
3122 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003123#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003124}
3125
3126void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003127display_release_window_surface(struct display *display,
3128 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003129{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003130#ifdef HAVE_CAIRO_EGL
3131 cairo_device_t *device;
3132
3133 device = cairo_surface_get_device(window->cairo_surface);
3134 if (!device)
3135 return;
3136
3137 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003138 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003139 cairo_device_release(device);
3140#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003141}
3142
3143void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003144display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003145{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003146 wl_list_insert(&display->deferred_list, &task->link);
3147}
3148
3149void
3150display_watch_fd(struct display *display,
3151 int fd, uint32_t events, struct task *task)
3152{
3153 struct epoll_event ep;
3154
3155 ep.events = events;
3156 ep.data.ptr = task;
3157 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3158}
3159
3160void
3161display_run(struct display *display)
3162{
3163 struct task *task;
3164 struct epoll_event ep[16];
3165 int i, count;
3166
Pekka Paalanen826d7952011-12-15 10:14:07 +02003167 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003168 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003169 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003170
3171 while (!wl_list_empty(&display->deferred_list)) {
3172 task = container_of(display->deferred_list.next,
3173 struct task, link);
3174 wl_list_remove(&task->link);
3175 task->run(task, 0);
3176 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003177
3178 if (!display->running)
3179 break;
3180
3181 wl_display_flush(display->display);
3182
3183 count = epoll_wait(display->epoll_fd,
3184 ep, ARRAY_LENGTH(ep), -1);
3185 for (i = 0; i < count; i++) {
3186 task = ep[i].data.ptr;
3187 task->run(task, ep[i].events);
3188 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003189 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003190}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003191
3192void
3193display_exit(struct display *display)
3194{
3195 display->running = 0;
3196}