blob: ac26f52afff18996fae07aafd81541a73963f21d [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øgsberg6a1b2012009-12-16 14:43:37 -0500786window_attach_surface(struct window *window)
787{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400788 struct display *display = window->display;
789 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400790 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000791#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100792 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000793#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500794 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795
796 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100798 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
799 data = cairo_surface_get_user_data(window->cairo_surface,
800 &surface_data_key);
801
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 cairo_gl_surface_swapbuffers(window->cairo_surface);
803 wl_egl_window_get_attached_size(data->window,
804 &window->server_allocation.width,
805 &window->server_allocation.height);
806 break;
807 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000808#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100809 case WINDOW_BUFFER_TYPE_SHM:
810 if (window->pending_surface != NULL)
811 return;
812
813 window->pending_surface = window->cairo_surface;
814 window->cairo_surface = NULL;
815
816 buffer =
817 display_get_buffer_for_surface(display,
818 window->pending_surface);
819
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200820 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 wl_surface_attach(window->surface, buffer, x, y);
822 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400823 cb = wl_display_sync(display->display);
824 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826 default:
827 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500829
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500830 wl_surface_damage(window->surface, 0, 0,
831 window->allocation.width,
832 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500833}
834
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500835void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400836window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500837{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100838 if (window->cairo_surface) {
839 switch (window->buffer_type) {
840 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
841 case WINDOW_BUFFER_TYPE_SHM:
842 display_surface_damage(window->display,
843 window->cairo_surface,
844 0, 0,
845 window->allocation.width,
846 window->allocation.height);
847 break;
848 default:
849 break;
850 }
851 window_attach_surface(window);
852 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500853}
854
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855void
856window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500858 cairo_surface_reference(surface);
859
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400860 if (window->cairo_surface != NULL)
861 cairo_surface_destroy(window->cairo_surface);
862
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500863 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400864}
865
Benjamin Franzke22d54812011-07-16 19:50:32 +0000866#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100867static void
868window_resize_cairo_window_surface(struct window *window)
869{
870 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200871 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100872
873 data = cairo_surface_get_user_data(window->cairo_surface,
874 &surface_data_key);
875
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200876 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877 wl_egl_window_resize(data->window,
878 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200879 window->allocation.height,
880 x,y);
881
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100882 cairo_gl_surface_set_size(window->cairo_surface,
883 window->allocation.width,
884 window->allocation.height);
885}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000886#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100887
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400888struct display *
889window_get_display(struct window *window)
890{
891 return window->display;
892}
893
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894void
895window_create_surface(struct window *window)
896{
897 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400898 uint32_t flags = 0;
899
900 if (!window->transparent)
901 flags = SURFACE_OPAQUE;
902
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400903 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500904#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
906 if (window->cairo_surface) {
907 window_resize_cairo_window_surface(window);
908 return;
909 }
910 surface = display_create_surface(window->display,
911 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500914 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400915 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400917 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400918 break;
919#endif
920 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400922 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800924 default:
925 surface = NULL;
926 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400927 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928
929 window_set_surface(window, surface);
930 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931}
932
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200933static void frame_destroy(struct frame *frame);
934
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400935void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500936window_destroy(struct window *window)
937{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200938 struct display *display = window->display;
939 struct input *input;
940
941 if (window->redraw_scheduled)
942 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500943 if (window->resize_scheduled)
944 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200945
946 wl_list_for_each(input, &display->input_list, link) {
947 if (input->pointer_focus == window)
948 input->pointer_focus = NULL;
949 if (input->keyboard_focus == window)
950 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500951 if (input->focus_widget &&
952 input->focus_widget->window == window)
953 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200954 }
955
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200956 if (window->frame)
957 frame_destroy(window->frame);
958
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200959 if (window->shell_surface)
960 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500961 wl_surface_destroy(window->surface);
962 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200963
964 if (window->cairo_surface != NULL)
965 cairo_surface_destroy(window->cairo_surface);
966 if (window->pending_surface != NULL)
967 cairo_surface_destroy(window->pending_surface);
968
969 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500970 free(window);
971}
972
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500973static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500974widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400975{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500976 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400977
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500978 wl_list_for_each(child, &widget->child_list, link) {
979 target = widget_find_widget(child, x, y);
980 if (target)
981 return target;
982 }
983
984 if (widget->allocation.x <= x &&
985 x < widget->allocation.x + widget->allocation.width &&
986 widget->allocation.y <= y &&
987 y < widget->allocation.y + widget->allocation.height) {
988 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989 }
990
991 return NULL;
992}
993
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500994static struct widget *
995widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500997 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999 widget = malloc(sizeof *widget);
1000 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001001 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001002 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001003 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004 wl_list_init(&widget->child_list);
1005
1006 return widget;
1007}
1008
1009struct widget *
1010window_add_widget(struct window *window, void *data)
1011{
1012 window->widget = widget_create(window, data);
1013 wl_list_init(&window->widget->link);
1014
1015 return window->widget;
1016}
1017
1018struct widget *
1019widget_add_widget(struct widget *parent, void *data)
1020{
1021 struct widget *widget;
1022
1023 widget = widget_create(parent->window, data);
1024 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001027}
1028
1029void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001030widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001031{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001032 struct display *display = widget->window->display;
1033 struct input *input;
1034
1035 wl_list_for_each(input, &display->input_list, link) {
1036 if (input->focus_widget == widget)
1037 input->focus_widget = NULL;
1038 }
1039
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001040 wl_list_remove(&widget->link);
1041 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042}
1043
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001045widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001046{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001047 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048}
1049
1050void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001051widget_set_size(struct widget *widget, int32_t width, int32_t height)
1052{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001053 widget->allocation.width = width;
1054 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001055}
1056
1057void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058widget_set_allocation(struct widget *widget,
1059 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001061 widget->allocation.x = x;
1062 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001063 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064}
1065
1066void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001067widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070}
1071
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001072void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001073widget_set_resize_handler(struct widget *widget,
1074 widget_resize_handler_t handler)
1075{
1076 widget->resize_handler = handler;
1077}
1078
1079void
1080widget_set_redraw_handler(struct widget *widget,
1081 widget_redraw_handler_t handler)
1082{
1083 widget->redraw_handler = handler;
1084}
1085
1086void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001087widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001088{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001089 widget->enter_handler = handler;
1090}
1091
1092void
1093widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1094{
1095 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001096}
1097
1098void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001099widget_set_motion_handler(struct widget *widget,
1100 widget_motion_handler_t handler)
1101{
1102 widget->motion_handler = handler;
1103}
1104
1105void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001106widget_set_button_handler(struct widget *widget,
1107 widget_button_handler_t handler)
1108{
1109 widget->button_handler = handler;
1110}
1111
1112void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001113widget_schedule_redraw(struct widget *widget)
1114{
1115 window_schedule_redraw(widget->window);
1116}
1117
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001118cairo_surface_t *
1119window_get_surface(struct window *window)
1120{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001121 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001122}
1123
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001124struct wl_surface *
1125window_get_wl_surface(struct window *window)
1126{
1127 return window->surface;
1128}
1129
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001130struct wl_shell_surface *
1131window_get_wl_shell_surface(struct window *window)
1132{
1133 return window->shell_surface;
1134}
1135
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001136static void
1137frame_resize_handler(struct widget *widget,
1138 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001139{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001140 struct frame *frame = data;
1141 struct widget *child = frame->child;
1142 struct rectangle allocation;
1143 int decoration_width, decoration_height;
1144
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001145 if (widget->window->type == TYPE_TOPLEVEL) {
1146 decoration_width = 20 + frame->margin * 2;
1147 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001148
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001149 allocation.x = 10 + frame->margin;
1150 allocation.y = 50 + frame->margin;
1151 allocation.width = width - decoration_width;
1152 allocation.height = height - decoration_height;
1153 } else {
1154 decoration_width = 0;
1155 decoration_height = 0;
1156
1157 allocation.x = 0;
1158 allocation.y = 0;
1159 allocation.width = width;
1160 allocation.height = height;
1161 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001162
1163 widget_set_allocation(child, allocation.x, allocation.y,
1164 allocation.width, allocation.height);
1165
1166 if (child->resize_handler)
1167 child->resize_handler(child,
1168 allocation.width,
1169 allocation.height,
1170 child->user_data);
1171
1172 widget_set_allocation(widget, 0, 0,
1173 child->allocation.width + decoration_width,
1174 child->allocation.height + decoration_height);
1175}
1176
1177static void
1178frame_redraw_handler(struct widget *widget, void *data)
1179{
1180 struct frame *frame = data;
1181 cairo_t *cr;
1182 cairo_text_extents_t extents;
1183 cairo_surface_t *source;
1184 int width, height, shadow_dx = 3, shadow_dy = 3;
1185 struct window *window = widget->window;
1186
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001187 if (window->type == TYPE_FULLSCREEN)
1188 return;
1189
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001190 width = widget->allocation.width;
1191 height = widget->allocation.height;
1192
1193 cr = cairo_create(window->cairo_surface);
1194
1195 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1196 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1197 cairo_paint(cr);
1198
1199 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1200 tile_mask(cr, window->display->shadow,
1201 shadow_dx, shadow_dy, width, height,
1202 frame->margin + 10 - shadow_dx,
1203 frame->margin + 10 - shadow_dy);
1204
1205 if (window->keyboard_device)
1206 source = window->display->active_frame;
1207 else
1208 source = window->display->inactive_frame;
1209
1210 tile_source(cr, source, 0, 0, width, height,
1211 frame->margin + 10, frame->margin + 50);
1212
1213 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1214 cairo_set_font_size(cr, 14);
1215 cairo_text_extents(cr, window->title, &extents);
1216 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1217 if (window->keyboard_device)
1218 cairo_set_source_rgb(cr, 0, 0, 0);
1219 else
1220 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1221 cairo_show_text(cr, window->title);
1222
1223 cairo_destroy(cr);
1224}
1225
1226static int
1227frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1228{
1229 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001230 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231 const int grip_size = 8;
1232
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001233 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001235 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001236 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001237 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001238 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240 hlocation = WINDOW_RESIZING_RIGHT;
1241 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001244 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001245 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001246 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001248 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001249 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251 vlocation = WINDOW_RESIZING_BOTTOM;
1252 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255 location = vlocation | hlocation;
1256 if (location & WINDOW_EXTERIOR)
1257 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 location = WINDOW_TITLEBAR;
1260 else if (location == WINDOW_INTERIOR)
1261 location = WINDOW_CLIENT_AREA;
1262
1263 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264}
1265
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001271 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272 switch (location) {
1273 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001274 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001275 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001276 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001277 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001278 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001280 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001281 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001288 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001289 case WINDOW_EXTERIOR:
1290 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001292 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294}
1295
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001296static void
1297frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001299 switch (index) {
1300 case 0: /* close */
1301 if (window->close_handler)
1302 window->close_handler(window->parent,
1303 window->user_data);
1304 else
1305 display_exit(window->display);
1306 break;
1307 case 1: /* fullscreen */
1308 /* we don't have a way to get out of fullscreen for now */
1309 window_set_fullscreen(window, 1);
1310 break;
1311 case 2: /* rotate */
1312 case 3: /* scale */
1313 break;
1314 }
1315}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001316
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001317void
1318window_show_frame_menu(struct window *window,
1319 struct input *input, uint32_t time)
1320{
1321 int32_t x, y;
1322
1323 static const char *entries[] = {
1324 "Close", "Fullscreen", "Rotate", "Scale"
1325 };
1326
1327 input_get_position(input, &x, &y);
1328 window_show_menu(window->display, input, time, window,
1329 x - 10, y - 10, frame_menu_func, entries, 4);
1330}
1331
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001332static int
1333frame_enter_handler(struct widget *widget,
1334 struct input *input, uint32_t time,
1335 int32_t x, int32_t y, void *data)
1336{
1337 return frame_get_pointer_image_for_location(data, input);
1338}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001339
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001340static int
1341frame_motion_handler(struct widget *widget,
1342 struct input *input, uint32_t time,
1343 int32_t x, int32_t y, void *data)
1344{
1345 return frame_get_pointer_image_for_location(data, input);
1346}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001347
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001349frame_button_handler(struct widget *widget,
1350 struct input *input, uint32_t time,
1351 int button, int state, void *data)
1352
1353{
1354 struct frame *frame = data;
1355 struct window *window = widget->window;
1356 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001357 location = frame_get_pointer_location(frame, input->sx, input->sy);
1358
1359 if (window->display->shell && button == BTN_LEFT && state == 1) {
1360 switch (location) {
1361 case WINDOW_TITLEBAR:
1362 if (!window->shell_surface)
1363 break;
1364 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001365 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001366 wl_shell_surface_move(window->shell_surface,
1367 input_get_input_device(input),
1368 time);
1369 break;
1370 case WINDOW_RESIZING_TOP:
1371 case WINDOW_RESIZING_BOTTOM:
1372 case WINDOW_RESIZING_LEFT:
1373 case WINDOW_RESIZING_RIGHT:
1374 case WINDOW_RESIZING_TOP_LEFT:
1375 case WINDOW_RESIZING_TOP_RIGHT:
1376 case WINDOW_RESIZING_BOTTOM_LEFT:
1377 case WINDOW_RESIZING_BOTTOM_RIGHT:
1378 if (!window->shell_surface)
1379 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001380 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001381 wl_shell_surface_resize(window->shell_surface,
1382 input_get_input_device(input),
1383 time, location);
1384 break;
1385 }
1386 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001387 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001388 }
1389}
1390
1391struct widget *
1392frame_create(struct window *window, void *data)
1393{
1394 struct frame *frame;
1395
1396 frame = malloc(sizeof *frame);
1397 memset(frame, 0, sizeof *frame);
1398
1399 frame->widget = window_add_widget(window, frame);
1400 frame->child = widget_add_widget(frame->widget, data);
1401 frame->margin = 16;
1402
1403 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1404 widget_set_resize_handler(frame->widget, frame_resize_handler);
1405 widget_set_enter_handler(frame->widget, frame_enter_handler);
1406 widget_set_motion_handler(frame->widget, frame_motion_handler);
1407 widget_set_button_handler(frame->widget, frame_button_handler);
1408
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001409 window->frame = frame;
1410
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001411 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001412}
1413
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001414static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001415frame_destroy(struct frame *frame)
1416{
1417 /* frame->child must be destroyed by the application */
1418 widget_destroy(frame->widget);
1419 free(frame);
1420}
1421
1422static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001423input_set_focus_widget(struct input *input, struct widget *focus,
1424 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001426 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001427 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001428
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001429 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001430 return;
1431
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001432 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001433 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001434 widget = old;
1435 if (input->grab)
1436 widget = input->grab;
1437 if (widget->leave_handler)
1438 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001439 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001440 }
1441
1442 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001443 widget = focus;
1444 if (input->grab)
1445 widget = input->grab;
1446 if (widget->enter_handler)
1447 pointer = widget->enter_handler(focus, input, time,
1448 x, y,
1449 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001450 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001451
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001452 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001453 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001454}
1455
1456static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001457input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001458 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001459{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001460 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001461 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001462 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001463 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001464
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001465 input->sx = sx;
1466 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001467
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001468 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001469 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001470 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001471 }
1472
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001473 if (input->grab)
1474 widget = input->grab;
1475 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001476 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001477 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001478 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001479 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001480 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001481
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001482 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001483}
1484
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001485void
1486input_grab(struct input *input, struct widget *widget, uint32_t button)
1487{
1488 input->grab = widget;
1489 input->grab_button = button;
1490}
1491
1492void
1493input_ungrab(struct input *input, uint32_t time)
1494{
1495 struct widget *widget;
1496
1497 input->grab = NULL;
1498 if (input->pointer_focus) {
1499 widget = widget_find_widget(input->pointer_focus->widget,
1500 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001501 input_set_focus_widget(input, widget,
1502 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001503 }
1504}
1505
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001506static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001507input_handle_button(void *data,
1508 struct wl_input_device *input_device,
1509 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001510{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001511 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001512 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001513
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001514 if (input->focus_widget && input->grab == NULL && state)
1515 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001516
Neil Roberts6b28aad2012-01-23 19:11:18 +00001517 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001518 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001519 (*widget->button_handler)(widget,
1520 input, time,
1521 button, state,
1522 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001523
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001524 if (input->grab && input->grab_button == button && !state)
1525 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001526}
1527
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001528static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001529input_handle_key(void *data, struct wl_input_device *input_device,
1530 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001531{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001532 struct input *input = data;
1533 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001534 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001535 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001536
Daniel Stone0d5a5092012-02-16 12:48:00 +00001537 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001538 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001539 return;
1540
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001541 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001542 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001543 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1544 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001545
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001546 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1547
1548 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001549 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001550 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001552
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001553 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1554 if (state)
1555 window_set_maximized(window,
1556 window->type != TYPE_MAXIMIZED);
1557 } else if (window->key_handler) {
1558 (*window->key_handler)(window, input, time, key,
1559 sym, state, window->user_data);
1560 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001561}
1562
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001563static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001564input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001565{
1566 struct window *window = input->pointer_focus;
1567
1568 if (!window)
1569 return;
1570
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001571 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001572
Pekka Paalanene1207c72011-12-16 12:02:09 +02001573 input->pointer_focus = NULL;
1574 input->current_pointer_image = POINTER_UNSET;
1575}
1576
1577static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001578input_handle_pointer_focus(void *data,
1579 struct wl_input_device *input_device,
1580 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001581 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001582{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001583 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001584 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001585 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001586
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001587 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001588 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001589 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001590
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001591 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001592 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593 window = input->pointer_focus;
1594
Kristian Høgsberg59826582011-01-20 11:56:57 -05001595 input->sx = sx;
1596 input->sy = sy;
1597
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001598 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001599 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001600 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001601}
1602
1603static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001604input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001605{
1606 struct window *window = input->keyboard_focus;
1607
1608 if (!window)
1609 return;
1610
1611 window->keyboard_device = NULL;
1612 if (window->keyboard_focus_handler)
1613 (*window->keyboard_focus_handler)(window, NULL,
1614 window->user_data);
1615
1616 input->keyboard_focus = NULL;
1617}
1618
1619static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001620input_handle_keyboard_focus(void *data,
1621 struct wl_input_device *input_device,
1622 uint32_t time,
1623 struct wl_surface *surface,
1624 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001625{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001626 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001627 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001628 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001629 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001630
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001631 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001632
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001633 if (surface)
1634 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001635
1636 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001637 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001638 for (k = keys->data; k < end; k++)
1639 input->modifiers |= d->xkb->map->modmap[*k];
1640
1641 window = input->keyboard_focus;
1642 if (window) {
1643 window->keyboard_device = input;
1644 if (window->keyboard_focus_handler)
1645 (*window->keyboard_focus_handler)(window,
1646 window->keyboard_device,
1647 window->user_data);
1648 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001649}
1650
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001651static void
1652input_handle_touch_down(void *data,
1653 struct wl_input_device *wl_input_device,
1654 uint32_t time, struct wl_surface *surface,
1655 int32_t id, int32_t x, int32_t y)
1656{
1657}
1658
1659static void
1660input_handle_touch_up(void *data,
1661 struct wl_input_device *wl_input_device,
1662 uint32_t time, int32_t id)
1663{
1664}
1665
1666static void
1667input_handle_touch_motion(void *data,
1668 struct wl_input_device *wl_input_device,
1669 uint32_t time, int32_t id, int32_t x, int32_t y)
1670{
1671}
1672
1673static void
1674input_handle_touch_frame(void *data,
1675 struct wl_input_device *wl_input_device)
1676{
1677}
1678
1679static void
1680input_handle_touch_cancel(void *data,
1681 struct wl_input_device *wl_input_device)
1682{
1683}
1684
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001685static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001686 input_handle_motion,
1687 input_handle_button,
1688 input_handle_key,
1689 input_handle_pointer_focus,
1690 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001691 input_handle_touch_down,
1692 input_handle_touch_up,
1693 input_handle_touch_motion,
1694 input_handle_touch_frame,
1695 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001696};
1697
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001698void
1699input_get_position(struct input *input, int32_t *x, int32_t *y)
1700{
1701 *x = input->sx;
1702 *y = input->sy;
1703}
1704
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001705struct wl_input_device *
1706input_get_input_device(struct input *input)
1707{
1708 return input->input_device;
1709}
1710
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001711uint32_t
1712input_get_modifiers(struct input *input)
1713{
1714 return input->modifiers;
1715}
1716
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001717struct widget *
1718input_get_focus_widget(struct input *input)
1719{
1720 return input->focus_widget;
1721}
1722
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001723struct data_offer {
1724 struct wl_data_offer *offer;
1725 struct input *input;
1726 struct wl_array types;
1727 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001728
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001729 struct task io_task;
1730 int fd;
1731 data_func_t func;
1732 int32_t x, y;
1733 void *user_data;
1734};
1735
1736static void
1737data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1738{
1739 struct data_offer *offer = data;
1740 char **p;
1741
1742 p = wl_array_add(&offer->types, sizeof *p);
1743 *p = strdup(type);
1744}
1745
1746static const struct wl_data_offer_listener data_offer_listener = {
1747 data_offer_offer,
1748};
1749
1750static void
1751data_offer_destroy(struct data_offer *offer)
1752{
1753 char **p;
1754
1755 offer->refcount--;
1756 if (offer->refcount == 0) {
1757 wl_data_offer_destroy(offer->offer);
1758 for (p = offer->types.data; *p; p++)
1759 free(*p);
1760 wl_array_release(&offer->types);
1761 free(offer);
1762 }
1763}
1764
1765static void
1766data_device_data_offer(void *data,
1767 struct wl_data_device *data_device, uint32_t id)
1768{
1769 struct data_offer *offer;
1770
1771 offer = malloc(sizeof *offer);
1772
1773 wl_array_init(&offer->types);
1774 offer->refcount = 1;
1775 offer->input = data;
1776
1777 /* FIXME: Generate typesafe wrappers for this */
1778 offer->offer = (struct wl_data_offer *)
1779 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1780 id, &wl_data_offer_interface);
1781
1782 wl_data_offer_add_listener(offer->offer,
1783 &data_offer_listener, offer);
1784}
1785
1786static void
1787data_device_enter(void *data, struct wl_data_device *data_device,
1788 uint32_t time, struct wl_surface *surface,
1789 int32_t x, int32_t y, struct wl_data_offer *offer)
1790{
1791 struct input *input = data;
1792 struct window *window;
1793 char **p;
1794
1795 input->drag_offer = wl_data_offer_get_user_data(offer);
1796 window = wl_surface_get_user_data(surface);
1797 input->pointer_focus = window;
1798
1799 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1800 *p = NULL;
1801
1802 window = input->pointer_focus;
1803 if (window->data_handler)
1804 window->data_handler(window, input, time, x, y,
1805 input->drag_offer->types.data,
1806 window->user_data);
1807}
1808
1809static void
1810data_device_leave(void *data, struct wl_data_device *data_device)
1811{
1812 struct input *input = data;
1813
1814 data_offer_destroy(input->drag_offer);
1815 input->drag_offer = NULL;
1816}
1817
1818static void
1819data_device_motion(void *data, struct wl_data_device *data_device,
1820 uint32_t time, int32_t x, int32_t y)
1821{
1822 struct input *input = data;
1823 struct window *window = input->pointer_focus;
1824
1825 input->sx = x;
1826 input->sy = y;
1827
1828 if (window->data_handler)
1829 window->data_handler(window, input, time, x, y,
1830 input->drag_offer->types.data,
1831 window->user_data);
1832}
1833
1834static void
1835data_device_drop(void *data, struct wl_data_device *data_device)
1836{
1837 struct input *input = data;
1838 struct window *window = input->pointer_focus;
1839
1840 if (window->drop_handler)
1841 window->drop_handler(window, input,
1842 input->sx, input->sy, window->user_data);
1843}
1844
1845static void
1846data_device_selection(void *data,
1847 struct wl_data_device *wl_data_device,
1848 struct wl_data_offer *offer)
1849{
1850 struct input *input = data;
1851 char **p;
1852
1853 if (input->selection_offer)
1854 data_offer_destroy(input->selection_offer);
1855
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001856 if (offer) {
1857 input->selection_offer = wl_data_offer_get_user_data(offer);
1858 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1859 *p = NULL;
1860 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001861}
1862
1863static const struct wl_data_device_listener data_device_listener = {
1864 data_device_data_offer,
1865 data_device_enter,
1866 data_device_leave,
1867 data_device_motion,
1868 data_device_drop,
1869 data_device_selection
1870};
1871
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001872void
1873input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1874{
1875 struct display *display = input->display;
1876 struct wl_buffer *buffer;
1877 cairo_surface_t *surface;
1878
1879 if (pointer == input->current_pointer_image)
1880 return;
1881
1882 input->current_pointer_image = pointer;
1883 surface = display->pointer_surfaces[pointer];
1884
1885 if (!surface)
1886 return;
1887
1888 buffer = display_get_buffer_for_surface(display, surface);
1889 wl_input_device_attach(input->input_device, time, buffer,
1890 pointer_images[pointer].hotspot_x,
1891 pointer_images[pointer].hotspot_y);
1892}
1893
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001894struct wl_data_device *
1895input_get_data_device(struct input *input)
1896{
1897 return input->data_device;
1898}
1899
1900void
1901input_set_selection(struct input *input,
1902 struct wl_data_source *source, uint32_t time)
1903{
1904 wl_data_device_set_selection(input->data_device, source, time);
1905}
1906
1907void
1908input_accept(struct input *input, uint32_t time, const char *type)
1909{
1910 wl_data_offer_accept(input->drag_offer->offer, time, type);
1911}
1912
1913static void
1914offer_io_func(struct task *task, uint32_t events)
1915{
1916 struct data_offer *offer =
1917 container_of(task, struct data_offer, io_task);
1918 unsigned int len;
1919 char buffer[4096];
1920
1921 len = read(offer->fd, buffer, sizeof buffer);
1922 offer->func(buffer, len,
1923 offer->x, offer->y, offer->user_data);
1924
1925 if (len == 0) {
1926 close(offer->fd);
1927 data_offer_destroy(offer);
1928 }
1929}
1930
1931static void
1932data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1933 data_func_t func, void *user_data)
1934{
1935 int p[2];
1936
1937 pipe2(p, O_CLOEXEC);
1938 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1939 close(p[1]);
1940
1941 offer->io_task.run = offer_io_func;
1942 offer->fd = p[0];
1943 offer->func = func;
1944 offer->refcount++;
1945 offer->user_data = user_data;
1946
1947 display_watch_fd(offer->input->display,
1948 offer->fd, EPOLLIN, &offer->io_task);
1949}
1950
1951void
1952input_receive_drag_data(struct input *input, const char *mime_type,
1953 data_func_t func, void *data)
1954{
1955 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1956 input->drag_offer->x = input->sx;
1957 input->drag_offer->y = input->sy;
1958}
1959
1960int
1961input_receive_selection_data(struct input *input, const char *mime_type,
1962 data_func_t func, void *data)
1963{
1964 char **p;
1965
1966 if (input->selection_offer == NULL)
1967 return -1;
1968
1969 for (p = input->selection_offer->types.data; *p; p++)
1970 if (strcmp(mime_type, *p) == 0)
1971 break;
1972
1973 if (*p == NULL)
1974 return -1;
1975
1976 data_offer_receive_data(input->selection_offer,
1977 mime_type, func, data);
1978 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001979}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001980
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001981int
1982input_receive_selection_data_to_fd(struct input *input,
1983 const char *mime_type, int fd)
1984{
1985 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1986
1987 return 0;
1988}
1989
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001990void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001991window_move(struct window *window, struct input *input, uint32_t time)
1992{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001993 if (!window->shell_surface)
1994 return;
1995
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001996 wl_shell_surface_move(window->shell_surface,
1997 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001998}
1999
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002000static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002001idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002002{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002003 struct window *window =
2004 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002005 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002006
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002007 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002008 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002009 widget_set_allocation(widget,
2010 window->pending_allocation.x,
2011 window->pending_allocation.y,
2012 window->pending_allocation.width,
2013 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002014
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002015 if (widget->resize_handler)
2016 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002017 widget->allocation.width,
2018 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002019 widget->user_data);
2020
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002021 if (window->allocation.width != widget->allocation.width ||
2022 window->allocation.height != widget->allocation.height) {
2023 window->allocation = widget->allocation;
2024 window_schedule_redraw(window);
2025 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002026}
2027
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002028void
2029window_schedule_resize(struct window *window, int width, int height)
2030{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002031 window->pending_allocation.x = 0;
2032 window->pending_allocation.y = 0;
2033 window->pending_allocation.width = width;
2034 window->pending_allocation.height = height;
2035
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002036 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002037 window->resize_task.run = idle_resize;
2038 display_defer(window->display, &window->resize_task);
2039 window->resize_scheduled = 1;
2040 }
2041}
2042
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002043void
2044widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2045{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002046 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002047}
2048
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002049static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002050handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002051 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002052 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002053{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002054 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002055
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002056 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002057 return;
2058
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002059 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002060 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002061}
2062
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002064menu_destroy(struct menu *menu)
2065{
2066 widget_destroy(menu->widget);
2067 window_destroy(menu->window);
2068 free(menu);
2069}
2070
2071static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2073{
2074 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002075 struct menu *menu = window->widget->user_data;
2076
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002077 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002078 * device. Or just use wl_callback. And this really needs to
2079 * be a window vfunc that the menu can set. And we need the
2080 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002082 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002083 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002084 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002085}
2086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002088 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002090};
2091
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002092void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002093window_get_allocation(struct window *window,
2094 struct rectangle *allocation)
2095{
2096 *allocation = window->allocation;
2097}
2098
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002099static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002100widget_redraw(struct widget *widget)
2101{
2102 struct widget *child;
2103
2104 if (widget->redraw_handler)
2105 widget->redraw_handler(widget, widget->user_data);
2106 wl_list_for_each(child, &widget->child_list, link)
2107 widget_redraw(child);
2108}
2109
2110static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002111idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002112{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002113 struct window *window =
2114 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002115
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002116 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002117 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002118 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002119 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120}
2121
2122void
2123window_schedule_redraw(struct window *window)
2124{
2125 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002126 window->redraw_task.run = idle_redraw;
2127 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002128 window->redraw_scheduled = 1;
2129 }
2130}
2131
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002132void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002133window_set_custom(struct window *window)
2134{
2135 window->type = TYPE_CUSTOM;
2136}
2137
2138void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002139window_set_fullscreen(struct window *window, int fullscreen)
2140{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002141 if (!window->display->shell)
2142 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002143
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002144 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002145 return;
2146
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002147 if (fullscreen) {
2148 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002149 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002150 wl_shell_surface_set_fullscreen(window->shell_surface,
2151 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2152 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002153 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002154 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002155 wl_shell_surface_set_toplevel(window->shell_surface);
2156 window_schedule_resize(window,
2157 window->saved_allocation.width,
2158 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002159 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002160}
2161
2162void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002163window_set_maximized(struct window *window, int maximized)
2164{
2165 if (!window->display->shell)
2166 return;
2167
2168 if ((window->type == TYPE_MAXIMIZED) == maximized)
2169 return;
2170
2171 if (window->type == TYPE_TOPLEVEL) {
2172 window->saved_allocation = window->allocation;
2173 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2174 window->type = TYPE_MAXIMIZED;
2175 } else {
2176 wl_shell_surface_set_toplevel(window->shell_surface);
2177 window->type = TYPE_TOPLEVEL;
2178 window_schedule_resize(window,
2179 window->saved_allocation.width,
2180 window->saved_allocation.height);
2181 }
2182}
2183
2184void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002185window_set_user_data(struct window *window, void *data)
2186{
2187 window->user_data = data;
2188}
2189
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002190void *
2191window_get_user_data(struct window *window)
2192{
2193 return window->user_data;
2194}
2195
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002196void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002197window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002198 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002199{
2200 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002201}
2202
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002203void
2204window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002205 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002206{
2207 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002208}
2209
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002210void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002211window_set_data_handler(struct window *window, window_data_handler_t handler)
2212{
2213 window->data_handler = handler;
2214}
2215
2216void
2217window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2218{
2219 window->drop_handler = handler;
2220}
2221
2222void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002223window_set_close_handler(struct window *window,
2224 window_close_handler_t handler)
2225{
2226 window->close_handler = handler;
2227}
2228
2229void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002230window_set_transparent(struct window *window, int transparent)
2231{
2232 window->transparent = transparent;
2233}
2234
2235void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002236window_set_title(struct window *window, const char *title)
2237{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002238 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002239 window->title = strdup(title);
2240}
2241
2242const char *
2243window_get_title(struct window *window)
2244{
2245 return window->title;
2246}
2247
2248void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002249display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2250 int32_t x, int32_t y, int32_t width, int32_t height)
2251{
2252 struct wl_buffer *buffer;
2253
2254 buffer = display_get_buffer_for_surface(display, cairo_surface);
2255
2256 wl_buffer_damage(buffer, x, y, width, height);
2257}
2258
2259void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002260window_damage(struct window *window, int32_t x, int32_t y,
2261 int32_t width, int32_t height)
2262{
2263 wl_surface_damage(window->surface, x, y, width, height);
2264}
2265
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002266static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002267window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002268{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002269 struct window *window;
2270
2271 window = malloc(sizeof *window);
2272 if (window == NULL)
2273 return NULL;
2274
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002275 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002276 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002277 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002278 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002279 if (display->shell) {
2280 window->shell_surface =
2281 wl_shell_get_shell_surface(display->shell,
2282 window->surface);
2283 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002284 window->allocation.x = 0;
2285 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002286 window->allocation.width = 0;
2287 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002288 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002289 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002290 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002291
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002292 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002293#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002294 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2295 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002296#else
2297 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2298#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002299 else
2300 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002301
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002302 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002303 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002304
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002305 if (window->shell_surface) {
2306 wl_shell_surface_set_user_data(window->shell_surface, window);
2307 wl_shell_surface_add_listener(window->shell_surface,
2308 &shell_surface_listener, window);
2309 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002310
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002311 return window;
2312}
2313
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002314struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002315window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002316{
2317 struct window *window;
2318
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002319 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002320 if (!window)
2321 return NULL;
2322
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002323 window->type = TYPE_TOPLEVEL;
2324 if (display->shell)
2325 wl_shell_surface_set_toplevel(window->shell_surface);
2326
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002327 return window;
2328}
2329
2330struct window *
2331window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002332 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002333{
2334 struct window *window;
2335
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002336 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002337 if (!window)
2338 return NULL;
2339
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002340 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002341 window->x = x;
2342 window->y = y;
2343
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002344 if (display->shell)
2345 wl_shell_surface_set_transient(window->shell_surface,
2346 window->parent->shell_surface,
2347 window->x, window->y, 0);
2348
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002349 return window;
2350}
2351
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002352static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002353menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002354{
2355 int next;
2356
2357 next = (sy - 8) / 20;
2358 if (menu->current != next) {
2359 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002360 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002361 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002362}
2363
2364static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002365menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002366 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002367 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002368{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002369 struct menu *menu = data;
2370
2371 if (widget == menu->widget)
2372 menu_set_item(data, y);
2373
2374 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002375}
2376
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002377static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002378menu_enter_handler(struct widget *widget,
2379 struct input *input, uint32_t time,
2380 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002381{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002382 struct menu *menu = data;
2383
2384 if (widget == menu->widget)
2385 menu_set_item(data, y);
2386
2387 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002388}
2389
2390static void
2391menu_leave_handler(struct widget *widget, struct input *input, void *data)
2392{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002393 struct menu *menu = data;
2394
2395 if (widget == menu->widget)
2396 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397}
2398
2399static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002400menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002401 struct input *input, uint32_t time,
2402 int button, int state, void *data)
2403
2404{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002405 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002406
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002407 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002408 /* Either relase after press-drag-release or
2409 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002410 menu->func(menu->window->parent,
2411 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002412 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002413 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002414 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002415}
2416
2417static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002418menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002419{
2420 cairo_t *cr;
2421 const int32_t r = 3, margin = 3;
2422 struct menu *menu = data;
2423 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002424 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002425
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002426 cr = cairo_create(window->cairo_surface);
2427 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2428 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2429 cairo_paint(cr);
2430
2431 width = window->allocation.width;
2432 height = window->allocation.height;
2433 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002434
2435 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002436 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2437 cairo_fill(cr);
2438
2439 for (i = 0; i < menu->count; i++) {
2440 if (i == menu->current) {
2441 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2442 cairo_rectangle(cr, margin, i * 20 + margin,
2443 width - 2 * margin, 20);
2444 cairo_fill(cr);
2445 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2446 cairo_move_to(cr, 10, i * 20 + 16);
2447 cairo_show_text(cr, menu->entries[i]);
2448 } else {
2449 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2450 cairo_move_to(cr, 10, i * 20 + 16);
2451 cairo_show_text(cr, menu->entries[i]);
2452 }
2453 }
2454
2455 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002456}
2457
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002458void
2459window_show_menu(struct display *display,
2460 struct input *input, uint32_t time, struct window *parent,
2461 int32_t x, int32_t y,
2462 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463{
2464 struct window *window;
2465 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002466 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002467
2468 menu = malloc(sizeof *menu);
2469 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002470 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002471
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002472 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002473 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002474 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002475
2476 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002477 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002478 menu->entries = entries;
2479 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002480 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002481 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002482 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002483 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002484 window->type = TYPE_MENU;
2485 window->x = x;
2486 window->y = y;
2487
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002488 wl_shell_surface_set_popup(window->shell_surface,
2489 input->input_device, time,
2490 window->parent->shell_surface,
2491 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002492
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002493 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002494 widget_set_enter_handler(menu->widget, menu_enter_handler);
2495 widget_set_leave_handler(menu->widget, menu_leave_handler);
2496 widget_set_motion_handler(menu->widget, menu_motion_handler);
2497 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002498
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002499 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002500 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002501}
2502
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002503void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002504window_set_buffer_type(struct window *window, enum window_buffer_type type)
2505{
2506 window->buffer_type = type;
2507}
2508
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002509
2510static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002511display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002512 struct wl_output *wl_output,
2513 int x, int y,
2514 int physical_width,
2515 int physical_height,
2516 int subpixel,
2517 const char *make,
2518 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002519{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002520 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002521
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002522 output->allocation.x = x;
2523 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002524}
2525
2526static void
2527display_handle_mode(void *data,
2528 struct wl_output *wl_output,
2529 uint32_t flags,
2530 int width,
2531 int height,
2532 int refresh)
2533{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002534 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002535 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002536
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002537 if (flags & WL_OUTPUT_MODE_CURRENT) {
2538 output->allocation.width = width;
2539 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002540 if (display->output_configure_handler)
2541 (*display->output_configure_handler)(
2542 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002543 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002544}
2545
2546static const struct wl_output_listener output_listener = {
2547 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002548 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002549};
2550
2551static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002552display_add_output(struct display *d, uint32_t id)
2553{
2554 struct output *output;
2555
2556 output = malloc(sizeof *output);
2557 if (output == NULL)
2558 return;
2559
2560 memset(output, 0, sizeof *output);
2561 output->display = d;
2562 output->output =
2563 wl_display_bind(d->display, id, &wl_output_interface);
2564 wl_list_insert(d->output_list.prev, &output->link);
2565
2566 wl_output_add_listener(output->output, &output_listener, output);
2567}
2568
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002569static void
2570output_destroy(struct output *output)
2571{
2572 if (output->destroy_handler)
2573 (*output->destroy_handler)(output, output->user_data);
2574
2575 wl_output_destroy(output->output);
2576 wl_list_remove(&output->link);
2577 free(output);
2578}
2579
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002580void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002581display_set_output_configure_handler(struct display *display,
2582 display_output_handler_t handler)
2583{
2584 struct output *output;
2585
2586 display->output_configure_handler = handler;
2587 if (!handler)
2588 return;
2589
2590 wl_list_for_each(output, &display->output_list, link)
2591 (*display->output_configure_handler)(output,
2592 display->user_data);
2593}
2594
2595void
2596output_set_user_data(struct output *output, void *data)
2597{
2598 output->user_data = data;
2599}
2600
2601void *
2602output_get_user_data(struct output *output)
2603{
2604 return output->user_data;
2605}
2606
2607void
2608output_set_destroy_handler(struct output *output,
2609 display_output_handler_t handler)
2610{
2611 output->destroy_handler = handler;
2612 /* FIXME: implement this, once we have way to remove outputs */
2613}
2614
2615void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002616output_get_allocation(struct output *output, struct rectangle *allocation)
2617{
2618 *allocation = output->allocation;
2619}
2620
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002621struct wl_output *
2622output_get_wl_output(struct output *output)
2623{
2624 return output->output;
2625}
2626
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002627static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002628display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002629{
2630 struct input *input;
2631
2632 input = malloc(sizeof *input);
2633 if (input == NULL)
2634 return;
2635
2636 memset(input, 0, sizeof *input);
2637 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002638 input->input_device =
2639 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002640 input->pointer_focus = NULL;
2641 input->keyboard_focus = NULL;
2642 wl_list_insert(d->input_list.prev, &input->link);
2643
2644 wl_input_device_add_listener(input->input_device,
2645 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002646 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002647
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002648 input->data_device =
2649 wl_data_device_manager_get_data_device(d->data_device_manager,
2650 input->input_device);
2651 wl_data_device_add_listener(input->data_device,
2652 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002653}
2654
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002655static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002656input_destroy(struct input *input)
2657{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002658 input_remove_keyboard_focus(input);
2659 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002660
2661 if (input->drag_offer)
2662 data_offer_destroy(input->drag_offer);
2663
2664 if (input->selection_offer)
2665 data_offer_destroy(input->selection_offer);
2666
2667 wl_data_device_destroy(input->data_device);
2668 wl_list_remove(&input->link);
2669 wl_input_device_destroy(input->input_device);
2670 free(input);
2671}
2672
2673static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002674display_handle_global(struct wl_display *display, uint32_t id,
2675 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002676{
2677 struct display *d = data;
2678
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002679 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002680 d->compositor =
2681 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002682 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002683 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002684 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002685 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002686 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002687 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002688 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002689 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002690 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2691 d->data_device_manager =
2692 wl_display_bind(display, id,
2693 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002694 }
2695}
2696
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002697static void
2698display_render_frame(struct display *d)
2699{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002700 int radius = 8;
2701 cairo_t *cr;
2702
2703 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2704 cr = cairo_create(d->shadow);
2705 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2706 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2707 rounded_rect(cr, 16, 16, 112, 112, radius);
2708 cairo_fill(cr);
2709 cairo_destroy(cr);
2710 blur_surface(d->shadow, 64);
2711
2712 d->active_frame =
2713 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2714 cr = cairo_create(d->active_frame);
2715 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2716 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2717 rounded_rect(cr, 16, 16, 112, 112, radius);
2718 cairo_fill(cr);
2719 cairo_destroy(cr);
2720
2721 d->inactive_frame =
2722 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2723 cr = cairo_create(d->inactive_frame);
2724 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2725 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2726 rounded_rect(cr, 16, 16, 112, 112, radius);
2727 cairo_fill(cr);
2728 cairo_destroy(cr);
2729}
2730
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002731static void
2732init_xkb(struct display *d)
2733{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002734 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002735
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002736 names.rules = "evdev";
2737 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002738 names.layout = option_xkb_layout;
2739 names.variant = option_xkb_variant;
2740 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002741
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002742 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002743 if (!d->xkb) {
2744 fprintf(stderr, "Failed to compile keymap\n");
2745 exit(1);
2746 }
2747}
2748
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002749static void
2750fini_xkb(struct display *display)
2751{
2752 xkb_free_keymap(display->xkb);
2753}
2754
Yuval Fledel45568f62010-12-06 09:18:12 -05002755static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002756init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002757{
2758 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002759 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002760
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002761 static const EGLint argb_cfg_attribs[] = {
2762 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002763 EGL_RED_SIZE, 1,
2764 EGL_GREEN_SIZE, 1,
2765 EGL_BLUE_SIZE, 1,
2766 EGL_ALPHA_SIZE, 1,
2767 EGL_DEPTH_SIZE, 1,
2768 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2769 EGL_NONE
2770 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002771
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002772 static const EGLint rgb_cfg_attribs[] = {
2773 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2774 EGL_RED_SIZE, 1,
2775 EGL_GREEN_SIZE, 1,
2776 EGL_BLUE_SIZE, 1,
2777 EGL_ALPHA_SIZE, 0,
2778 EGL_DEPTH_SIZE, 1,
2779 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2780 EGL_NONE
2781 };
2782
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002783#ifdef USE_CAIRO_GLESV2
2784 static const EGLint context_attribs[] = {
2785 EGL_CONTEXT_CLIENT_VERSION, 2,
2786 EGL_NONE
2787 };
2788 EGLint api = EGL_OPENGL_ES_API;
2789#else
2790 EGLint *context_attribs = NULL;
2791 EGLint api = EGL_OPENGL_API;
2792#endif
2793
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002794 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002795 if (!eglInitialize(d->dpy, &major, &minor)) {
2796 fprintf(stderr, "failed to initialize display\n");
2797 return -1;
2798 }
2799
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002800 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002801 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2802 return -1;
2803 }
2804
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002805 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2806 &d->argb_config, 1, &n) || n != 1) {
2807 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002808 return -1;
2809 }
2810
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002811 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2812 &d->rgb_config, 1, &n) || n != 1) {
2813 fprintf(stderr, "failed to choose rgb config\n");
2814 return -1;
2815 }
2816
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002817 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2818 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002819 if (d->rgb_ctx == NULL) {
2820 fprintf(stderr, "failed to create context\n");
2821 return -1;
2822 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002823 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002824 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002825 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002826 fprintf(stderr, "failed to create context\n");
2827 return -1;
2828 }
2829
Benjamin Franzke0c991632011-09-27 21:57:31 +02002830 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002831 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002832 return -1;
2833 }
2834
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002835#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002836 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2837 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002838 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002839 return -1;
2840 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002841 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2842 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2843 fprintf(stderr, "failed to get cairo egl argb device\n");
2844 return -1;
2845 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002846#endif
2847
2848 return 0;
2849}
2850
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002851static void
2852fini_egl(struct display *display)
2853{
2854#ifdef HAVE_CAIRO_EGL
2855 cairo_device_destroy(display->argb_device);
2856 cairo_device_destroy(display->rgb_device);
2857#endif
2858
2859 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2860 EGL_NO_CONTEXT);
2861
2862 eglTerminate(display->dpy);
2863 eglReleaseThread();
2864}
2865
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002866static int
2867event_mask_update(uint32_t mask, void *data)
2868{
2869 struct display *d = data;
2870
2871 d->mask = mask;
2872
2873 return 0;
2874}
2875
2876static void
2877handle_display_data(struct task *task, uint32_t events)
2878{
2879 struct display *display =
2880 container_of(task, struct display, display_task);
2881
2882 wl_display_iterate(display->display, display->mask);
2883}
2884
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002885struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002886display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002887{
2888 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002889 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002890 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002891 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002892
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002893 g_type_init();
2894
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002895 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002896 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002897 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002898
2899 xkb_option_group = g_option_group_new("xkb",
2900 "Keyboard options",
2901 "Show all XKB options",
2902 NULL, NULL);
2903 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2904 g_option_context_add_group (context, xkb_option_group);
2905
2906 if (!g_option_context_parse(context, argc, argv, &error)) {
2907 fprintf(stderr, "option parsing failed: %s\n", error->message);
2908 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002909 }
2910
Tim Wiederhake748f6722011-01-23 23:25:25 +01002911 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002912
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002913 d = malloc(sizeof *d);
2914 if (d == NULL)
2915 return NULL;
2916
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002917 memset(d, 0, sizeof *d);
2918
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002919 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002920 if (d->display == NULL) {
2921 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002922 return NULL;
2923 }
2924
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002925 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2926 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2927 d->display_task.run = handle_display_data;
2928 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2929
2930 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002931 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002932 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002933
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002934 /* Set up listener so we'll catch all events. */
2935 wl_display_add_global_listener(d->display,
2936 display_handle_global, d);
2937
2938 /* Process connection events. */
2939 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002940 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002941 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002942
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002943 d->image_target_texture_2d =
2944 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2945 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2946 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2947
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002948 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002949
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002950 display_render_frame(d);
2951
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002952 wl_list_init(&d->window_list);
2953
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002954 init_xkb(d);
2955
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002956 return d;
2957}
2958
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002959static void
2960display_destroy_outputs(struct display *display)
2961{
2962 struct output *tmp;
2963 struct output *output;
2964
2965 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2966 output_destroy(output);
2967}
2968
Pekka Paalanene1207c72011-12-16 12:02:09 +02002969static void
2970display_destroy_inputs(struct display *display)
2971{
2972 struct input *tmp;
2973 struct input *input;
2974
2975 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2976 input_destroy(input);
2977}
2978
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002979void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002980display_destroy(struct display *display)
2981{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002982 if (!wl_list_empty(&display->window_list))
2983 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2984
2985 if (!wl_list_empty(&display->deferred_list))
2986 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2987
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002988 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002989 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002990
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002991 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002992
2993 cairo_surface_destroy(display->active_frame);
2994 cairo_surface_destroy(display->inactive_frame);
2995 cairo_surface_destroy(display->shadow);
2996 destroy_pointer_surfaces(display);
2997
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002998 fini_egl(display);
2999
Pekka Paalanenc2052982011-12-16 11:41:32 +02003000 if (display->shell)
3001 wl_shell_destroy(display->shell);
3002
3003 if (display->shm)
3004 wl_shm_destroy(display->shm);
3005
3006 if (display->data_device_manager)
3007 wl_data_device_manager_destroy(display->data_device_manager);
3008
3009 wl_compositor_destroy(display->compositor);
3010
3011 close(display->epoll_fd);
3012
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003013 wl_display_flush(display->display);
3014 wl_display_destroy(display->display);
3015 free(display);
3016}
3017
3018void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003019display_set_user_data(struct display *display, void *data)
3020{
3021 display->user_data = data;
3022}
3023
3024void *
3025display_get_user_data(struct display *display)
3026{
3027 return display->user_data;
3028}
3029
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003030struct wl_display *
3031display_get_display(struct display *display)
3032{
3033 return display->display;
3034}
3035
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003036struct output *
3037display_get_output(struct display *display)
3038{
3039 return container_of(display->output_list.next, struct output, link);
3040}
3041
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003042struct wl_compositor *
3043display_get_compositor(struct display *display)
3044{
3045 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003046}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003047
3048EGLDisplay
3049display_get_egl_display(struct display *d)
3050{
3051 return d->dpy;
3052}
3053
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003054struct wl_data_source *
3055display_create_data_source(struct display *display)
3056{
3057 return wl_data_device_manager_create_data_source(display->data_device_manager);
3058}
3059
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003060EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003061display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003062{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003063 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003064}
3065
Benjamin Franzke0c991632011-09-27 21:57:31 +02003066EGLConfig
3067display_get_argb_egl_config(struct display *d)
3068{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003069 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003070}
3071
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003072struct wl_shell *
3073display_get_shell(struct display *display)
3074{
3075 return display->shell;
3076}
3077
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003078int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003079display_acquire_window_surface(struct display *display,
3080 struct window *window,
3081 EGLContext ctx)
3082{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003083#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003084 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003085 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003086
3087 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003089 device = cairo_surface_get_device(window->cairo_surface);
3090 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003091 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003092
Benjamin Franzke0c991632011-09-27 21:57:31 +02003093 if (!ctx) {
3094 if (device == display->rgb_device)
3095 ctx = display->rgb_ctx;
3096 else if (device == display->argb_device)
3097 ctx = display->argb_ctx;
3098 else
3099 assert(0);
3100 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003101
3102 data = cairo_surface_get_user_data(window->cairo_surface,
3103 &surface_data_key);
3104
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003105 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003106 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003107 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3108 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003109
3110 return 0;
3111#else
3112 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003113#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003114}
3115
3116void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003117display_release_window_surface(struct display *display,
3118 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003119{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003120#ifdef HAVE_CAIRO_EGL
3121 cairo_device_t *device;
3122
3123 device = cairo_surface_get_device(window->cairo_surface);
3124 if (!device)
3125 return;
3126
3127 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003128 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003129 cairo_device_release(device);
3130#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003131}
3132
3133void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003134display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003135{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003136 wl_list_insert(&display->deferred_list, &task->link);
3137}
3138
3139void
3140display_watch_fd(struct display *display,
3141 int fd, uint32_t events, struct task *task)
3142{
3143 struct epoll_event ep;
3144
3145 ep.events = events;
3146 ep.data.ptr = task;
3147 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3148}
3149
3150void
3151display_run(struct display *display)
3152{
3153 struct task *task;
3154 struct epoll_event ep[16];
3155 int i, count;
3156
Pekka Paalanen826d7952011-12-15 10:14:07 +02003157 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003158 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003159 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003160
3161 while (!wl_list_empty(&display->deferred_list)) {
3162 task = container_of(display->deferred_list.next,
3163 struct task, link);
3164 wl_list_remove(&task->link);
3165 task->run(task, 0);
3166 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003167
3168 if (!display->running)
3169 break;
3170
3171 wl_display_flush(display->display);
3172
3173 count = epoll_wait(display->epoll_fd,
3174 ep, ARRAY_LENGTH(ep), -1);
3175 for (i = 0; i < count; i++) {
3176 task = ep[i].data.ptr;
3177 task->run(task, ep[i].events);
3178 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003179 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003180}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003181
3182void
3183display_exit(struct display *display)
3184{
3185 display->running = 0;
3186}