blob: c9dec18514213401de88b5f1ef7cbd5ad8cd7886 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500119 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500127 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400130 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400132 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500133 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 window_motion_handler_t motion_handler;
Kristian Høgsberg900b2262011-09-06 14:33:52 -0400135 window_enter_handler_t enter_handler;
136 window_leave_handler_t leave_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400137 window_data_handler_t data_handler;
138 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500139 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400140
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500141 struct wl_list widget_list;
142 struct widget *focus_widget;
143 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400144
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500145 struct window *menu;
146
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500147 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400148 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500149};
150
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500151struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500152 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400153 struct wl_list link;
154 struct rectangle allocation;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500155 widget_enter_handler_t enter_handler;
156 widget_leave_handler_t leave_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400157 void *user_data;
158};
159
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160struct input {
161 struct display *display;
162 struct wl_input_device *input_device;
163 struct window *pointer_focus;
164 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400165 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400166 uint32_t modifiers;
167 int32_t x, y, sx, sy;
168 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400169
170 struct wl_data_device *data_device;
171 struct data_offer *drag_offer;
172 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173};
174
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500175struct output {
176 struct display *display;
177 struct wl_output *output;
178 struct rectangle allocation;
179 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200180
181 display_output_handler_t destroy_handler;
182 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183};
184
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500185struct menu {
186 struct window *window;
Kristian Høgsberg391649b2012-01-09 09:22:30 -0500187 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500188 const char **entries;
189 uint32_t time;
190 int current;
191 int count;
192 menu_func_t func;
193};
194
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400195enum {
196 POINTER_DEFAULT = 100,
197 POINTER_UNSET
198};
199
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500200enum window_location {
201 WINDOW_INTERIOR = 0,
202 WINDOW_RESIZING_TOP = 1,
203 WINDOW_RESIZING_BOTTOM = 2,
204 WINDOW_RESIZING_LEFT = 4,
205 WINDOW_RESIZING_TOP_LEFT = 5,
206 WINDOW_RESIZING_BOTTOM_LEFT = 6,
207 WINDOW_RESIZING_RIGHT = 8,
208 WINDOW_RESIZING_TOP_RIGHT = 9,
209 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
210 WINDOW_RESIZING_MASK = 15,
211 WINDOW_EXTERIOR = 16,
212 WINDOW_TITLEBAR = 17,
213 WINDOW_CLIENT_AREA = 18,
214};
215
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400216const char *option_xkb_layout = "us";
217const char *option_xkb_variant = "";
218const char *option_xkb_options = "";
219
220static const GOptionEntry xkb_option_entries[] = {
221 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
222 &option_xkb_layout, "XKB Layout" },
223 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
224 &option_xkb_variant, "XKB Variant" },
225 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
226 &option_xkb_options, "XKB Options" },
227 { NULL }
228};
229
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400230static const cairo_user_data_key_t surface_data_key;
231struct surface_data {
232 struct wl_buffer *buffer;
233};
234
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500235#define MULT(_d,c,a,t) \
236 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
237
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500238#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400239
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100240struct egl_window_surface_data {
241 struct display *display;
242 struct wl_surface *surface;
243 struct wl_egl_window *window;
244 EGLSurface surf;
245};
246
247static void
248egl_window_surface_data_destroy(void *p)
249{
250 struct egl_window_surface_data *data = p;
251 struct display *d = data->display;
252
253 eglDestroySurface(d->dpy, data->surf);
254 wl_egl_window_destroy(data->window);
255 data->surface = NULL;
256
257 free(p);
258}
259
260static cairo_surface_t *
261display_create_egl_window_surface(struct display *display,
262 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400263 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100264 struct rectangle *rectangle)
265{
266 cairo_surface_t *cairo_surface;
267 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 EGLConfig config;
269 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200270 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100271
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400272 static const EGLint premul_attribs[] = {
273 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
274 EGL_NONE
275 };
276
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100277 data = malloc(sizeof *data);
278 if (data == NULL)
279 return NULL;
280
281 data->display = display;
282 data->surface = surface;
283
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400284 if (flags & SURFACE_OPAQUE) {
285 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200287 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400288 } else {
289 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200290 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400291 attribs = premul_attribs;
292 }
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,
299 data->window, attribs);
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øgsbergf389cac2011-08-31 16:21:38 -0400547 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400549 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400551 data->data.buffer = wl_shm_create_buffer(display->shm,
552 fd,
553 rectangle->width,
554 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400555 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556
557 close(fd);
558
559 return surface;
560}
561
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500562static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563display_create_shm_surface_from_file(struct display *display,
564 const char *filename,
565 struct rectangle *rect)
566{
567 cairo_surface_t *surface;
568 GdkPixbuf *pixbuf;
569 GError *error = NULL;
570 int stride, i;
571 unsigned char *pixels, *p, *end, *dest_data;
572 int dest_stride;
573 uint32_t *d;
574
575 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
576 rect->width, rect->height,
577 FALSE, &error);
578 if (error != NULL)
579 return NULL;
580
581 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
582 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500583 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400584 return NULL;
585 }
586
587 stride = gdk_pixbuf_get_rowstride(pixbuf);
588 pixels = gdk_pixbuf_get_pixels(pixbuf);
589
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400590 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000591 if (surface == NULL) {
592 g_object_unref(pixbuf);
593 return NULL;
594 }
595
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400596 dest_data = cairo_image_surface_get_data (surface);
597 dest_stride = cairo_image_surface_get_stride (surface);
598
599 for (i = 0; i < rect->height; i++) {
600 d = (uint32_t *) (dest_data + i * dest_stride);
601 p = pixels + i * stride;
602 end = p + rect->width * 4;
603 while (p < end) {
604 unsigned int t;
605 unsigned char a, r, g, b;
606
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400607 a = p[3];
608 MULT(r, p[0], a, t);
609 MULT(g, p[1], a, t);
610 MULT(b, p[2], a, t);
611 p += 4;
612 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
613 }
614 }
615
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500616 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617
618 return surface;
619}
620
nobled7b87cb02011-02-01 18:51:47 +0000621static int
622check_size(struct rectangle *rect)
623{
624 if (rect->width && rect->height)
625 return 0;
626
627 fprintf(stderr, "tried to create surface of "
628 "width: %d, height: %d\n", rect->width, rect->height);
629 return -1;
630}
631
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400632cairo_surface_t *
633display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400635 struct rectangle *rectangle,
636 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637{
nobled7b87cb02011-02-01 18:51:47 +0000638 if (check_size(rectangle) < 0)
639 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500640#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500641 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642 if (surface)
643 return display_create_egl_window_surface(display,
644 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 rectangle);
647 else
648 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400649 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500651 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400653 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654}
655
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500656static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657display_create_surface_from_file(struct display *display,
658 const char *filename,
659 struct rectangle *rectangle)
660{
nobled7b87cb02011-02-01 18:51:47 +0000661 if (check_size(rectangle) < 0)
662 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500663#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500664 if (display->dpy) {
665 return display_create_egl_image_surface_from_file(display,
666 filename,
667 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671}
Yuval Fledel45568f62010-12-06 09:18:12 -0500672 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400673 const char *filename;
674 int hotspot_x, hotspot_y;
675} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500676 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
677 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
678 { DATADIR "/weston/bottom_side.png", 16, 20 },
679 { DATADIR "/weston/grabbing.png", 20, 17 },
680 { DATADIR "/weston/left_ptr.png", 10, 5 },
681 { DATADIR "/weston/left_side.png", 10, 20 },
682 { DATADIR "/weston/right_side.png", 30, 19 },
683 { DATADIR "/weston/top_left_corner.png", 8, 8 },
684 { DATADIR "/weston/top_right_corner.png", 26, 8 },
685 { DATADIR "/weston/top_side.png", 18, 8 },
686 { DATADIR "/weston/xterm.png", 15, 15 },
687 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400688};
689
690static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400691create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692{
693 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400698 display->pointer_surfaces =
699 malloc(count * sizeof *display->pointer_surfaces);
700 rect.width = width;
701 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704 display_create_surface_from_file(display,
705 pointer_images[i].filename,
706 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100707 if (!display->pointer_surfaces[i]) {
708 fprintf(stderr, "Error loading pointer image: %s\n",
709 pointer_images[i].filename);
710 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400711 }
712
713}
714
Pekka Paalanen325bb602011-12-19 10:31:45 +0200715static void
716destroy_pointer_surfaces(struct display *display)
717{
718 int i, count;
719
720 count = ARRAY_LENGTH(pointer_images);
721 for (i = 0; i < count; ++i) {
722 if (display->pointer_surfaces[i])
723 cairo_surface_destroy(display->pointer_surfaces[i]);
724 }
725 free(display->pointer_surfaces);
726}
727
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400728cairo_surface_t *
729display_get_pointer_surface(struct display *display, int pointer,
730 int *width, int *height,
731 int *hotspot_x, int *hotspot_y)
732{
733 cairo_surface_t *surface;
734
735 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500736#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400737 *width = cairo_gl_surface_get_width(surface);
738 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000739#else
740 *width = cairo_image_surface_get_width(surface);
741 *height = cairo_image_surface_get_height(surface);
742#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400743 *hotspot_x = pointer_images[pointer].hotspot_x;
744 *hotspot_y = pointer_images[pointer].hotspot_y;
745
746 return cairo_surface_reference(surface);
747}
748
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400749static void
750window_attach_surface(struct window *window);
751
752static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400753free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754{
755 struct window *window = data;
756
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758 cairo_surface_destroy(window->pending_surface);
759 window->pending_surface = NULL;
760 if (window->cairo_surface)
761 window_attach_surface(window);
762}
763
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400764static const struct wl_callback_listener free_surface_listener = {
765 free_surface
766};
767
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500768static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200769window_get_resize_dx_dy(struct window *window, int *x, int *y)
770{
771 if (window->resize_edges & WINDOW_RESIZING_LEFT)
772 *x = window->server_allocation.width - window->allocation.width;
773 else
774 *x = 0;
775
776 if (window->resize_edges & WINDOW_RESIZING_TOP)
777 *y = window->server_allocation.height -
778 window->allocation.height;
779 else
780 *y = 0;
781
782 window->resize_edges = 0;
783}
784
785static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400786window_set_type(struct window *window)
787{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200788 if (!window->shell_surface)
789 return;
790
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 switch (window->type) {
792 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
795 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400797 break;
798 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200799 wl_shell_surface_set_transient(window->shell_surface,
800 window->parent->shell_surface,
801 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500803 case TYPE_MENU:
804 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400805 case TYPE_CUSTOM:
806 break;
807 }
808}
809
810static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500811window_attach_surface(struct window *window)
812{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400813 struct display *display = window->display;
814 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400815 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000818#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500819 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100820
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400821 if (display->shell)
822 window_set_type(window);
823
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100826 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
827 data = cairo_surface_get_user_data(window->cairo_surface,
828 &surface_data_key);
829
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100830 cairo_gl_surface_swapbuffers(window->cairo_surface);
831 wl_egl_window_get_attached_size(data->window,
832 &window->server_allocation.width,
833 &window->server_allocation.height);
834 break;
835 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000836#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200838 window_get_resize_dx_dy(window, &x, &y);
839
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100840 if (window->pending_surface != NULL)
841 return;
842
843 window->pending_surface = window->cairo_surface;
844 window->cairo_surface = NULL;
845
846 buffer =
847 display_get_buffer_for_surface(display,
848 window->pending_surface);
849
850 wl_surface_attach(window->surface, buffer, x, y);
851 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400852 cb = wl_display_sync(display->display);
853 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000855 default:
856 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100857 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500858
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500859 wl_surface_damage(window->surface, 0, 0,
860 window->allocation.width,
861 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500862}
863
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500864void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400865window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500866{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100867 if (window->cairo_surface) {
868 switch (window->buffer_type) {
869 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
870 case WINDOW_BUFFER_TYPE_SHM:
871 display_surface_damage(window->display,
872 window->cairo_surface,
873 0, 0,
874 window->allocation.width,
875 window->allocation.height);
876 break;
877 default:
878 break;
879 }
880 window_attach_surface(window);
881 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500882}
883
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400884void
885window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400886{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500887 cairo_surface_reference(surface);
888
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889 if (window->cairo_surface != NULL)
890 cairo_surface_destroy(window->cairo_surface);
891
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500892 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400893}
894
Benjamin Franzke22d54812011-07-16 19:50:32 +0000895#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100896static void
897window_resize_cairo_window_surface(struct window *window)
898{
899 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200900 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901
902 data = cairo_surface_get_user_data(window->cairo_surface,
903 &surface_data_key);
904
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906 wl_egl_window_resize(data->window,
907 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200908 window->allocation.height,
909 x,y);
910
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 cairo_gl_surface_set_size(window->cairo_surface,
912 window->allocation.width,
913 window->allocation.height);
914}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000915#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400917struct display *
918window_get_display(struct window *window)
919{
920 return window->display;
921}
922
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400923void
924window_create_surface(struct window *window)
925{
926 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400927 uint32_t flags = 0;
928
929 if (!window->transparent)
930 flags = SURFACE_OPAQUE;
931
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400932 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500933#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100934 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
935 if (window->cairo_surface) {
936 window_resize_cairo_window_surface(window);
937 return;
938 }
939 surface = display_create_surface(window->display,
940 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400941 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500943 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400944 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100945 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400946 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947 break;
948#endif
949 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400950 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800953 default:
954 surface = NULL;
955 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400956 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400957
958 window_set_surface(window, surface);
959 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400960}
961
962static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500963window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500964{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500965 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500966 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400967 cairo_surface_t *frame;
968 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500969
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400970 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400971
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400972 width = window->allocation.width;
973 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500974
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500975 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500976
Kristian Høgsberg09531622010-06-14 23:22:15 -0400977 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400978 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400979 cairo_paint(cr);
980
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400981 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400982 tile_mask(cr, window->display->shadow,
983 shadow_dx, shadow_dy, width, height,
984 window->margin + 10 - shadow_dx,
985 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500986
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400987 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400988 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400989 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400990 frame = window->display->inactive_frame;
991
992 tile_source(cr, frame, 0, 0, width, height,
993 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500994
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500995 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
996 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500997 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400998 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400999 if (window->keyboard_device)
1000 cairo_set_source_rgb(cr, 0, 0, 0);
1001 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001002 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001003 cairo_show_text(cr, window->title);
1004
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001005 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001006}
1007
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001008void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001009window_destroy(struct window *window)
1010{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001011 struct display *display = window->display;
1012 struct input *input;
1013
1014 if (window->redraw_scheduled)
1015 wl_list_remove(&window->redraw_task.link);
1016
1017 wl_list_for_each(input, &display->input_list, link) {
1018 if (input->pointer_focus == window)
1019 input->pointer_focus = NULL;
1020 if (input->keyboard_focus == window)
1021 input->keyboard_focus = NULL;
1022 }
1023
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001024 if (window->shell_surface)
1025 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001026 wl_surface_destroy(window->surface);
1027 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001028
1029 if (window->cairo_surface != NULL)
1030 cairo_surface_destroy(window->cairo_surface);
1031 if (window->pending_surface != NULL)
1032 cairo_surface_destroy(window->pending_surface);
1033
1034 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001035 free(window);
1036}
1037
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001038static struct widget *
1039window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001043 wl_list_for_each(widget, &window->widget_list, link) {
1044 if (widget->allocation.x <= x &&
1045 x < widget->allocation.x + widget->allocation.width &&
1046 widget->allocation.y <= y &&
1047 y < widget->allocation.y + widget->allocation.height) {
1048 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001049 }
1050 }
1051
1052 return NULL;
1053}
1054
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001055struct widget *
1056window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 widget = malloc(sizeof *widget);
1061 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001062 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 widget->user_data = data;
1064 wl_list_insert(window->widget_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001066 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067}
1068
1069void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001070window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001071{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001072 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074 wl_list_for_each(widget, &window->widget_list, link)
1075 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076}
1077
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001078struct widget *
1079window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001081 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001082}
1083
1084void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001085widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088}
1089
1090void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001091widget_set_allocation(struct widget *widget,
1092 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001094 widget->allocation.x = x;
1095 widget->allocation.y = y;
1096 widget->allocation.width = width;
1097 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098}
1099
1100void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001103 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001104}
1105
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001106void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001107widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001108{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001109 widget->enter_handler = handler;
1110}
1111
1112void
1113widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1114{
1115 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001116}
1117
1118void
1119widget_schedule_redraw(struct widget *widget)
1120{
1121 window_schedule_redraw(widget->window);
1122}
1123
1124void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001125window_draw(struct window *window)
1126{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001127 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001128 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001129 else
1130 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001131}
1132
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001133cairo_surface_t *
1134window_get_surface(struct window *window)
1135{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001136 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001137}
1138
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001139struct wl_surface *
1140window_get_wl_surface(struct window *window)
1141{
1142 return window->surface;
1143}
1144
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001145struct wl_shell_surface *
1146window_get_wl_shell_surface(struct window *window)
1147{
1148 return window->shell_surface;
1149}
1150
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001151static int
1152get_pointer_location(struct window *window, int32_t x, int32_t y)
1153{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001154 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001155 const int grip_size = 8;
1156
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001157 if (!window->decoration)
1158 return WINDOW_CLIENT_AREA;
1159
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001161 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001162 else if (window->margin <= x && x < window->margin + grip_size)
1163 hlocation = WINDOW_RESIZING_LEFT;
1164 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001165 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166 else if (x < window->allocation.width - window->margin)
1167 hlocation = WINDOW_RESIZING_RIGHT;
1168 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001169 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001170
1171 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001172 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001173 else if (window->margin <= y && y < window->margin + grip_size)
1174 vlocation = WINDOW_RESIZING_TOP;
1175 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001176 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001177 else if (y < window->allocation.height - window->margin)
1178 vlocation = WINDOW_RESIZING_BOTTOM;
1179 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001180 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001181
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001182 location = vlocation | hlocation;
1183 if (location & WINDOW_EXTERIOR)
1184 location = WINDOW_EXTERIOR;
1185 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1186 location = WINDOW_TITLEBAR;
1187 else if (location == WINDOW_INTERIOR)
1188 location = WINDOW_CLIENT_AREA;
1189
1190 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001191}
1192
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001193static int
1194input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001195{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001196 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197
1198 location = get_pointer_location(input->pointer_focus,
1199 input->sx, input->sy);
1200 switch (location) {
1201 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001202 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001204 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001205 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001206 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001207 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001208 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001210 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001211 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001212 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001214 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001216 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217 case WINDOW_EXTERIOR:
1218 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001219 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001220 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001221 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001222 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001223}
1224
1225void
1226input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1227{
1228 struct display *display = input->display;
1229 struct wl_buffer *buffer;
1230 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001231
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001232 if (pointer == input->current_pointer_image)
1233 return;
1234
1235 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001237
1238 if (!surface)
1239 return;
1240
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001241 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001242 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243 pointer_images[pointer].hotspot_x,
1244 pointer_images[pointer].hotspot_y);
1245}
1246
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001247static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001248window_set_focus_widget(struct window *window, struct widget *focus,
1249 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001250{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001251 struct widget *old;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001252
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001253 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001254 return;
1255
Kristian Høgsbergee143232012-01-09 08:42:24 -05001256 old = window->focus_widget;
1257 if (old) {
1258 if (old->leave_handler)
1259 old->leave_handler(old, input, old->user_data);
1260 window->focus_widget = NULL;
1261 }
1262
1263 if (focus) {
1264 if (focus->enter_handler)
1265 focus->enter_handler(focus, input, time,
1266 x, y, focus->user_data);
1267 window->focus_widget = focus;
1268 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001269}
1270
1271static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001272input_handle_motion(void *data, struct wl_input_device *input_device,
1273 uint32_t time,
1274 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001275{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001276 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001278 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001279 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001280
1281 input->x = x;
1282 input->y = y;
1283 input->sx = sx;
1284 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001286 if (!window->focus_widget || !window->widget_grab_button) {
1287 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001288 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001289 }
1290
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001291 if (window->motion_handler)
1292 pointer = (*window->motion_handler)(window, input, time,
1293 x, y, sx, sy,
1294 window->user_data);
1295
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001297 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001298}
1299
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001300static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001301window_menu_func(struct window *window, int index, void *data)
1302{
1303 switch (index) {
1304 case 0: /* close */
1305 if (window->close_handler)
1306 window->close_handler(window->parent,
1307 window->user_data);
1308 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001309 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001310 break;
1311 case 1: /* fullscreen */
1312 /* we don't have a way to get out of fullscreen for now */
1313 window_set_fullscreen(window, 1);
1314 break;
1315 case 2: /* rotate */
1316 case 3: /* scale */
1317 break;
1318 }
1319}
1320
1321
1322static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001323input_handle_button(void *data,
1324 struct wl_input_device *input_device,
1325 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001326{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327 struct input *input = data;
1328 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001329 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001331 int32_t x, y;
1332 static const char *entries[] = {
1333 "Close", "Fullscreen", "Rotate", "Scale"
1334 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001335
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001336 if (window->focus_widget && window->widget_grab_button == 0 && state)
1337 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001338
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001339 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001340
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001341 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001342 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001343 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001344 if (!window->shell_surface)
1345 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001346 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001347 wl_shell_surface_move(window->shell_surface,
1348 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001349 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001350 case WINDOW_RESIZING_TOP:
1351 case WINDOW_RESIZING_BOTTOM:
1352 case WINDOW_RESIZING_LEFT:
1353 case WINDOW_RESIZING_RIGHT:
1354 case WINDOW_RESIZING_TOP_LEFT:
1355 case WINDOW_RESIZING_TOP_RIGHT:
1356 case WINDOW_RESIZING_BOTTOM_LEFT:
1357 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001358 if (!window->shell_surface)
1359 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001360 wl_shell_surface_resize(window->shell_surface,
1361 input_device, time,
1362 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001363 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001364 case WINDOW_CLIENT_AREA:
1365 if (window->button_handler)
1366 (*window->button_handler)(window,
1367 input, time,
1368 button, state,
1369 window->user_data);
1370 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001371 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001372 } else if (button == BTN_RIGHT && state == 1) {
1373 switch (location) {
1374 default:
1375 input_get_position(input, &x, &y);
1376 window->menu = window_create_menu(window->display,
1377 input, time,
1378 window,
1379 x - 10, y - 10,
1380 window_menu_func,
1381 entries, 4);
1382 window_schedule_redraw(window->menu);
1383 break;
1384 case WINDOW_CLIENT_AREA:
1385 if (window->button_handler)
1386 (*window->button_handler)(window,
1387 input, time,
1388 button, state,
1389 window->user_data);
1390 break;
1391 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001392 } else {
1393 if (window->button_handler)
1394 (*window->button_handler)(window,
1395 input, time,
1396 button, state,
1397 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001398 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001399
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001400 if (window->focus_widget &&
1401 window->widget_grab_button == button && !state) {
1402 window->widget_grab_button = 0;
1403 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001404 window_set_focus_widget(window, widget, input, time,
1405 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001406 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001407}
1408
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001409static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001410input_handle_key(void *data, struct wl_input_device *input_device,
1411 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001412{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001413 struct input *input = data;
1414 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001415 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001416 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001417
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001418 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001419 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001420 return;
1421
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001422 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001423 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001424 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1425 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001426
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001427 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1428
1429 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001430 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001431 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001432 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001433
1434 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001435 (*window->key_handler)(window, input, time, key, sym, state,
1436 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001437}
1438
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001439static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001440input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001441{
1442 struct window *window = input->pointer_focus;
1443
1444 if (!window)
1445 return;
1446
Kristian Høgsbergee143232012-01-09 08:42:24 -05001447 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001448
1449 if (window->leave_handler)
1450 window->leave_handler(window, input, time, window->user_data);
1451 input->pointer_focus = NULL;
1452 input->current_pointer_image = POINTER_UNSET;
1453}
1454
1455static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001456input_handle_pointer_focus(void *data,
1457 struct wl_input_device *input_device,
1458 uint32_t time, struct wl_surface *surface,
1459 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001460{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001461 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001462 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001463 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001464 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001465
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001466 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001467 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001468 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001469
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001470 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001471 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472 window = input->pointer_focus;
1473
Kristian Høgsberg59826582011-01-20 11:56:57 -05001474 input->x = x;
1475 input->y = y;
1476 input->sx = sx;
1477 input->sy = sy;
1478
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001480 if (window->enter_handler)
1481 pointer = window->enter_handler(window, input,
1482 time, sx, sy,
1483 window->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001484
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001485 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001486 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001487
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001488 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001489 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001490 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001491}
1492
1493static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001494input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001495{
1496 struct window *window = input->keyboard_focus;
1497
1498 if (!window)
1499 return;
1500
1501 window->keyboard_device = NULL;
1502 if (window->keyboard_focus_handler)
1503 (*window->keyboard_focus_handler)(window, NULL,
1504 window->user_data);
1505
1506 input->keyboard_focus = NULL;
1507}
1508
1509static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001510input_handle_keyboard_focus(void *data,
1511 struct wl_input_device *input_device,
1512 uint32_t time,
1513 struct wl_surface *surface,
1514 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001515{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001516 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001517 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001518 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001519 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001520
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001521 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001522
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001523 if (surface)
1524 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001525
1526 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001527 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528 for (k = keys->data; k < end; k++)
1529 input->modifiers |= d->xkb->map->modmap[*k];
1530
1531 window = input->keyboard_focus;
1532 if (window) {
1533 window->keyboard_device = input;
1534 if (window->keyboard_focus_handler)
1535 (*window->keyboard_focus_handler)(window,
1536 window->keyboard_device,
1537 window->user_data);
1538 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001539}
1540
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001541static void
1542input_handle_touch_down(void *data,
1543 struct wl_input_device *wl_input_device,
1544 uint32_t time, struct wl_surface *surface,
1545 int32_t id, int32_t x, int32_t y)
1546{
1547}
1548
1549static void
1550input_handle_touch_up(void *data,
1551 struct wl_input_device *wl_input_device,
1552 uint32_t time, int32_t id)
1553{
1554}
1555
1556static void
1557input_handle_touch_motion(void *data,
1558 struct wl_input_device *wl_input_device,
1559 uint32_t time, int32_t id, int32_t x, int32_t y)
1560{
1561}
1562
1563static void
1564input_handle_touch_frame(void *data,
1565 struct wl_input_device *wl_input_device)
1566{
1567}
1568
1569static void
1570input_handle_touch_cancel(void *data,
1571 struct wl_input_device *wl_input_device)
1572{
1573}
1574
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001575static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001576 input_handle_motion,
1577 input_handle_button,
1578 input_handle_key,
1579 input_handle_pointer_focus,
1580 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001581 input_handle_touch_down,
1582 input_handle_touch_up,
1583 input_handle_touch_motion,
1584 input_handle_touch_frame,
1585 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001586};
1587
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001588void
1589input_get_position(struct input *input, int32_t *x, int32_t *y)
1590{
1591 *x = input->sx;
1592 *y = input->sy;
1593}
1594
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001595struct wl_input_device *
1596input_get_input_device(struct input *input)
1597{
1598 return input->input_device;
1599}
1600
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001601uint32_t
1602input_get_modifiers(struct input *input)
1603{
1604 return input->modifiers;
1605}
1606
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001607struct data_offer {
1608 struct wl_data_offer *offer;
1609 struct input *input;
1610 struct wl_array types;
1611 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001612
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001613 struct task io_task;
1614 int fd;
1615 data_func_t func;
1616 int32_t x, y;
1617 void *user_data;
1618};
1619
1620static void
1621data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1622{
1623 struct data_offer *offer = data;
1624 char **p;
1625
1626 p = wl_array_add(&offer->types, sizeof *p);
1627 *p = strdup(type);
1628}
1629
1630static const struct wl_data_offer_listener data_offer_listener = {
1631 data_offer_offer,
1632};
1633
1634static void
1635data_offer_destroy(struct data_offer *offer)
1636{
1637 char **p;
1638
1639 offer->refcount--;
1640 if (offer->refcount == 0) {
1641 wl_data_offer_destroy(offer->offer);
1642 for (p = offer->types.data; *p; p++)
1643 free(*p);
1644 wl_array_release(&offer->types);
1645 free(offer);
1646 }
1647}
1648
1649static void
1650data_device_data_offer(void *data,
1651 struct wl_data_device *data_device, uint32_t id)
1652{
1653 struct data_offer *offer;
1654
1655 offer = malloc(sizeof *offer);
1656
1657 wl_array_init(&offer->types);
1658 offer->refcount = 1;
1659 offer->input = data;
1660
1661 /* FIXME: Generate typesafe wrappers for this */
1662 offer->offer = (struct wl_data_offer *)
1663 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1664 id, &wl_data_offer_interface);
1665
1666 wl_data_offer_add_listener(offer->offer,
1667 &data_offer_listener, offer);
1668}
1669
1670static void
1671data_device_enter(void *data, struct wl_data_device *data_device,
1672 uint32_t time, struct wl_surface *surface,
1673 int32_t x, int32_t y, struct wl_data_offer *offer)
1674{
1675 struct input *input = data;
1676 struct window *window;
1677 char **p;
1678
1679 input->drag_offer = wl_data_offer_get_user_data(offer);
1680 window = wl_surface_get_user_data(surface);
1681 input->pointer_focus = window;
1682
1683 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1684 *p = NULL;
1685
1686 window = input->pointer_focus;
1687 if (window->data_handler)
1688 window->data_handler(window, input, time, x, y,
1689 input->drag_offer->types.data,
1690 window->user_data);
1691}
1692
1693static void
1694data_device_leave(void *data, struct wl_data_device *data_device)
1695{
1696 struct input *input = data;
1697
1698 data_offer_destroy(input->drag_offer);
1699 input->drag_offer = NULL;
1700}
1701
1702static void
1703data_device_motion(void *data, struct wl_data_device *data_device,
1704 uint32_t time, int32_t x, int32_t y)
1705{
1706 struct input *input = data;
1707 struct window *window = input->pointer_focus;
1708
1709 input->sx = x;
1710 input->sy = y;
1711
1712 if (window->data_handler)
1713 window->data_handler(window, input, time, x, y,
1714 input->drag_offer->types.data,
1715 window->user_data);
1716}
1717
1718static void
1719data_device_drop(void *data, struct wl_data_device *data_device)
1720{
1721 struct input *input = data;
1722 struct window *window = input->pointer_focus;
1723
1724 if (window->drop_handler)
1725 window->drop_handler(window, input,
1726 input->sx, input->sy, window->user_data);
1727}
1728
1729static void
1730data_device_selection(void *data,
1731 struct wl_data_device *wl_data_device,
1732 struct wl_data_offer *offer)
1733{
1734 struct input *input = data;
1735 char **p;
1736
1737 if (input->selection_offer)
1738 data_offer_destroy(input->selection_offer);
1739
1740 input->selection_offer = wl_data_offer_get_user_data(offer);
1741 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1742 *p = NULL;
1743}
1744
1745static const struct wl_data_device_listener data_device_listener = {
1746 data_device_data_offer,
1747 data_device_enter,
1748 data_device_leave,
1749 data_device_motion,
1750 data_device_drop,
1751 data_device_selection
1752};
1753
1754struct wl_data_device *
1755input_get_data_device(struct input *input)
1756{
1757 return input->data_device;
1758}
1759
1760void
1761input_set_selection(struct input *input,
1762 struct wl_data_source *source, uint32_t time)
1763{
1764 wl_data_device_set_selection(input->data_device, source, time);
1765}
1766
1767void
1768input_accept(struct input *input, uint32_t time, const char *type)
1769{
1770 wl_data_offer_accept(input->drag_offer->offer, time, type);
1771}
1772
1773static void
1774offer_io_func(struct task *task, uint32_t events)
1775{
1776 struct data_offer *offer =
1777 container_of(task, struct data_offer, io_task);
1778 unsigned int len;
1779 char buffer[4096];
1780
1781 len = read(offer->fd, buffer, sizeof buffer);
1782 offer->func(buffer, len,
1783 offer->x, offer->y, offer->user_data);
1784
1785 if (len == 0) {
1786 close(offer->fd);
1787 data_offer_destroy(offer);
1788 }
1789}
1790
1791static void
1792data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1793 data_func_t func, void *user_data)
1794{
1795 int p[2];
1796
1797 pipe2(p, O_CLOEXEC);
1798 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1799 close(p[1]);
1800
1801 offer->io_task.run = offer_io_func;
1802 offer->fd = p[0];
1803 offer->func = func;
1804 offer->refcount++;
1805 offer->user_data = user_data;
1806
1807 display_watch_fd(offer->input->display,
1808 offer->fd, EPOLLIN, &offer->io_task);
1809}
1810
1811void
1812input_receive_drag_data(struct input *input, const char *mime_type,
1813 data_func_t func, void *data)
1814{
1815 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1816 input->drag_offer->x = input->sx;
1817 input->drag_offer->y = input->sy;
1818}
1819
1820int
1821input_receive_selection_data(struct input *input, const char *mime_type,
1822 data_func_t func, void *data)
1823{
1824 char **p;
1825
1826 if (input->selection_offer == NULL)
1827 return -1;
1828
1829 for (p = input->selection_offer->types.data; *p; p++)
1830 if (strcmp(mime_type, *p) == 0)
1831 break;
1832
1833 if (*p == NULL)
1834 return -1;
1835
1836 data_offer_receive_data(input->selection_offer,
1837 mime_type, func, data);
1838 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001839}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001840
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001841int
1842input_receive_selection_data_to_fd(struct input *input,
1843 const char *mime_type, int fd)
1844{
1845 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1846
1847 return 0;
1848}
1849
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001850void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001851window_move(struct window *window, struct input *input, uint32_t time)
1852{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001853 if (!window->shell_surface)
1854 return;
1855
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001856 wl_shell_surface_move(window->shell_surface,
1857 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001858}
1859
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001860static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001861handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001862 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001863 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001864{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001865 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001866 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001867
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001868 /* FIXME: this is probably the wrong place to check for width
1869 * or height <= 0, but it prevents the compositor from crashing
1870 */
1871 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001872 return;
1873
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001874 window->resize_edges = edges;
1875
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001876 if (window->resize_handler) {
1877 child_width = width - 20 - window->margin * 2;
1878 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001879
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001880 (*window->resize_handler)(window,
1881 child_width, child_height,
1882 window->user_data);
1883 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001884 window->allocation.width = width;
1885 window->allocation.height = height;
1886
1887 if (window->redraw_handler)
1888 window_schedule_redraw(window);
1889 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001890}
1891
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001892static void
1893handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1894{
1895 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001896 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897 /* FIXME: Need more context in this event, at least the input
1898 * device. Or just use wl_callback. */
1899
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001900 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001901 window_destroy(window);
1902}
1903
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001904static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001905 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001906 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001907};
1908
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001909void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001910window_get_allocation(struct window *window,
1911 struct rectangle *allocation)
1912{
1913 *allocation = window->allocation;
1914}
1915
1916void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001917window_get_child_allocation(struct window *window,
1918 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001919{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001920 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001921 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001922 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001923 allocation->x = window->margin + 10;
1924 allocation->y = window->margin + 50;
1925 allocation->width =
1926 window->allocation.width - 20 - window->margin * 2;
1927 allocation->height =
1928 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001929 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001930}
1931
1932void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001933window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001934{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001935 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001936 window->allocation.x = 20 + window->margin;
1937 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001938 window->allocation.width = width + 20 + window->margin * 2;
1939 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001940 } else {
1941 window->allocation.x = 0;
1942 window->allocation.y = 0;
1943 window->allocation.width = width;
1944 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001945 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001946}
1947
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001948static void
1949idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001950{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001951 struct window *window =
1952 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001953
1954 window->redraw_handler(window, window->user_data);
1955 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001956}
1957
1958void
1959window_schedule_redraw(struct window *window)
1960{
1961 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001962 window->redraw_task.run = idle_redraw;
1963 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001964 window->redraw_scheduled = 1;
1965 }
1966}
1967
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001968void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001969window_set_custom(struct window *window)
1970{
1971 window->type = TYPE_CUSTOM;
1972}
1973
1974void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001975window_set_fullscreen(struct window *window, int fullscreen)
1976{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001977 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001978 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001979
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001980 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001981 return;
1982
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001983 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001984 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001985 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001986 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001987 width = output->allocation.width;
1988 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001989 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001990 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001991 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001992 width = window->saved_allocation.width - 20 - window->margin * 2;
1993 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001994 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001995 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001996
1997 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001998}
1999
2000void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002001window_set_decoration(struct window *window, int decoration)
2002{
2003 window->decoration = decoration;
2004}
2005
2006void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002007window_set_user_data(struct window *window, void *data)
2008{
2009 window->user_data = data;
2010}
2011
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002012void *
2013window_get_user_data(struct window *window)
2014{
2015 return window->user_data;
2016}
2017
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002018void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002019window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002020 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002021{
2022 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002023}
2024
2025void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002026window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002027 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002028{
2029 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002030}
2031
2032void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002033window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002034 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002035{
2036 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002037}
2038
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002039void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002040window_set_button_handler(struct window *window,
2041 window_button_handler_t handler)
2042{
2043 window->button_handler = handler;
2044}
2045
2046void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002047window_set_motion_handler(struct window *window,
2048 window_motion_handler_t handler)
2049{
2050 window->motion_handler = handler;
2051}
2052
2053void
Kristian Høgsberg900b2262011-09-06 14:33:52 -04002054window_set_enter_handler(struct window *window,
2055 window_enter_handler_t handler)
2056{
2057 window->enter_handler = handler;
2058}
2059
2060void
2061window_set_leave_handler(struct window *window,
2062 window_leave_handler_t handler)
2063{
2064 window->leave_handler = handler;
2065}
2066
2067void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002068window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002069 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002070{
2071 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002072}
2073
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002074void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002075window_set_data_handler(struct window *window, window_data_handler_t handler)
2076{
2077 window->data_handler = handler;
2078}
2079
2080void
2081window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2082{
2083 window->drop_handler = handler;
2084}
2085
2086void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002087window_set_close_handler(struct window *window,
2088 window_close_handler_t handler)
2089{
2090 window->close_handler = handler;
2091}
2092
2093void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002094window_set_transparent(struct window *window, int transparent)
2095{
2096 window->transparent = transparent;
2097}
2098
2099void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002100window_set_title(struct window *window, const char *title)
2101{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002102 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002103 window->title = strdup(title);
2104}
2105
2106const char *
2107window_get_title(struct window *window)
2108{
2109 return window->title;
2110}
2111
2112void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002113display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2114 int32_t x, int32_t y, int32_t width, int32_t height)
2115{
2116 struct wl_buffer *buffer;
2117
2118 buffer = display_get_buffer_for_surface(display, cairo_surface);
2119
2120 wl_buffer_damage(buffer, x, y, width, height);
2121}
2122
2123void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002124window_damage(struct window *window, int32_t x, int32_t y,
2125 int32_t width, int32_t height)
2126{
2127 wl_surface_damage(window->surface, x, y, width, height);
2128}
2129
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002130static struct window *
2131window_create_internal(struct display *display, struct window *parent,
2132 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002133{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002134 struct window *window;
2135
2136 window = malloc(sizeof *window);
2137 if (window == NULL)
2138 return NULL;
2139
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002140 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002141 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002142 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002143 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002144 if (display->shell) {
2145 window->shell_surface =
2146 wl_shell_get_shell_surface(display->shell,
2147 window->surface);
2148 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002149 window->allocation.x = 0;
2150 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002151 window->allocation.width = width;
2152 window->allocation.height = height;
2153 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002154 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002155 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002156 window->transparent = 1;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002157 wl_list_init(&window->widget_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002158
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002159 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002160#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002161 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2162 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002163#else
2164 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2165#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002166 else
2167 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002168
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002169 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002170 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002171
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002172 if (window->shell_surface) {
2173 wl_shell_surface_set_user_data(window->shell_surface, window);
2174 wl_shell_surface_add_listener(window->shell_surface,
2175 &shell_surface_listener, window);
2176 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002177
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002178 return window;
2179}
2180
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002181struct window *
2182window_create(struct display *display, int32_t width, int32_t height)
2183{
2184 struct window *window;
2185
2186 window = window_create_internal(display, NULL, width, height);
2187 if (!window)
2188 return NULL;
2189
2190 return window;
2191}
2192
2193struct window *
2194window_create_transient(struct display *display, struct window *parent,
2195 int32_t x, int32_t y, int32_t width, int32_t height)
2196{
2197 struct window *window;
2198
2199 window = window_create_internal(parent->display,
2200 parent, width, height);
2201 if (!window)
2202 return NULL;
2203
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002204 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002205 window->x = x;
2206 window->y = y;
2207
2208 return window;
2209}
2210
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002211static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002212menu_set_item(struct widget *widget, struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002213{
2214 int next;
2215
2216 next = (sy - 8) / 20;
2217 if (menu->current != next) {
2218 menu->current = next;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002219 widget_schedule_redraw(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002220 }
2221
2222 return POINTER_LEFT_PTR;
2223}
2224
2225static int
2226menu_motion_handler(struct window *window,
2227 struct input *input, uint32_t time,
2228 int32_t x, int32_t y,
2229 int32_t sx, int32_t sy, void *data)
2230{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002231 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002232
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002233 return menu_set_item(menu->widget, menu, sy);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002234}
2235
2236static void
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002237menu_enter_handler(struct widget *widget,
2238 struct input *input, uint32_t time,
2239 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002240{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002241 menu_set_item(widget, data, y);
2242}
2243
2244static void
2245menu_leave_handler(struct widget *widget, struct input *input, void *data)
2246{
2247 menu_set_item(widget, data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002248}
2249
2250static void
2251menu_button_handler(struct window *window,
2252 struct input *input, uint32_t time,
2253 int button, int state, void *data)
2254
2255{
2256 struct menu *menu = data;
2257
2258 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002259 if (state == 0 && time - menu->time > 500) {
2260 menu->func(window->parent,
2261 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002262 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002263 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002264}
2265
2266static void
2267menu_redraw_handler(struct window *window, void *data)
2268{
2269 cairo_t *cr;
2270 const int32_t r = 3, margin = 3;
2271 struct menu *menu = data;
2272 int32_t width, height, i;
2273
2274 width = 200;
2275 height = menu->count * 20 + margin * 2;
2276 window_set_child_size(window, width, height);
2277 window_create_surface(window);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002278 widget_set_allocation(menu->widget, 0, 0, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002279
2280 cr = cairo_create(window->cairo_surface);
2281 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2282 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2283 cairo_paint(cr);
2284
2285 width = window->allocation.width;
2286 height = window->allocation.height;
2287 rounded_rect(cr, 0, 0, width, height, r);
2288 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2289 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2290 cairo_fill(cr);
2291
2292 for (i = 0; i < menu->count; i++) {
2293 if (i == menu->current) {
2294 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2295 cairo_rectangle(cr, margin, i * 20 + margin,
2296 width - 2 * margin, 20);
2297 cairo_fill(cr);
2298 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2299 cairo_move_to(cr, 10, i * 20 + 16);
2300 cairo_show_text(cr, menu->entries[i]);
2301 } else {
2302 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2303 cairo_move_to(cr, 10, i * 20 + 16);
2304 cairo_show_text(cr, menu->entries[i]);
2305 }
2306 }
2307
2308 cairo_destroy(cr);
2309 window_flush(window);
2310}
2311
2312struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002313window_create_menu(struct display *display,
2314 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002315 int32_t x, int32_t y,
2316 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002317{
2318 struct window *window;
2319 struct menu *menu;
2320
2321 menu = malloc(sizeof *menu);
2322 if (!menu)
2323 return NULL;
2324
2325 window = window_create_internal(parent->display, parent, 0, 0);
2326 if (!window)
2327 return NULL;
2328
2329 menu->window = window;
2330 menu->entries = entries;
2331 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002332 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002333 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002334 window->decoration = 0;
2335 window->type = TYPE_MENU;
2336 window->x = x;
2337 window->y = y;
2338
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002339 wl_shell_surface_set_popup(window->shell_surface,
2340 input->input_device, time,
2341 window->parent->shell_surface,
2342 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002343
2344 window_set_motion_handler(window, menu_motion_handler);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002345 window_set_button_handler(window, menu_button_handler);
2346 window_set_redraw_handler(window, menu_redraw_handler);
2347 window_set_user_data(window, menu);
2348
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002349 menu->widget = window_add_widget(window, menu);
2350 widget_set_enter_handler(menu->widget, menu_enter_handler);
2351 widget_set_leave_handler(menu->widget, menu_leave_handler);
2352
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002353 return window;
2354}
2355
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002356void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002357window_set_buffer_type(struct window *window, enum window_buffer_type type)
2358{
2359 window->buffer_type = type;
2360}
2361
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002362
2363static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002364display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002365 struct wl_output *wl_output,
2366 int x, int y,
2367 int physical_width,
2368 int physical_height,
2369 int subpixel,
2370 const char *make,
2371 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002372{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002373 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002374
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002375 output->allocation.x = x;
2376 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002377}
2378
2379static void
2380display_handle_mode(void *data,
2381 struct wl_output *wl_output,
2382 uint32_t flags,
2383 int width,
2384 int height,
2385 int refresh)
2386{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002387 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002388 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002389
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002390 if (flags & WL_OUTPUT_MODE_CURRENT) {
2391 output->allocation.width = width;
2392 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002393 if (display->output_configure_handler)
2394 (*display->output_configure_handler)(
2395 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002396 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002397}
2398
2399static const struct wl_output_listener output_listener = {
2400 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002401 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002402};
2403
2404static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002405display_add_output(struct display *d, uint32_t id)
2406{
2407 struct output *output;
2408
2409 output = malloc(sizeof *output);
2410 if (output == NULL)
2411 return;
2412
2413 memset(output, 0, sizeof *output);
2414 output->display = d;
2415 output->output =
2416 wl_display_bind(d->display, id, &wl_output_interface);
2417 wl_list_insert(d->output_list.prev, &output->link);
2418
2419 wl_output_add_listener(output->output, &output_listener, output);
2420}
2421
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002422static void
2423output_destroy(struct output *output)
2424{
2425 if (output->destroy_handler)
2426 (*output->destroy_handler)(output, output->user_data);
2427
2428 wl_output_destroy(output->output);
2429 wl_list_remove(&output->link);
2430 free(output);
2431}
2432
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002433void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002434display_set_output_configure_handler(struct display *display,
2435 display_output_handler_t handler)
2436{
2437 struct output *output;
2438
2439 display->output_configure_handler = handler;
2440 if (!handler)
2441 return;
2442
2443 wl_list_for_each(output, &display->output_list, link)
2444 (*display->output_configure_handler)(output,
2445 display->user_data);
2446}
2447
2448void
2449output_set_user_data(struct output *output, void *data)
2450{
2451 output->user_data = data;
2452}
2453
2454void *
2455output_get_user_data(struct output *output)
2456{
2457 return output->user_data;
2458}
2459
2460void
2461output_set_destroy_handler(struct output *output,
2462 display_output_handler_t handler)
2463{
2464 output->destroy_handler = handler;
2465 /* FIXME: implement this, once we have way to remove outputs */
2466}
2467
2468void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002469output_get_allocation(struct output *output, struct rectangle *allocation)
2470{
2471 *allocation = output->allocation;
2472}
2473
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002474struct wl_output *
2475output_get_wl_output(struct output *output)
2476{
2477 return output->output;
2478}
2479
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002480static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002481display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002482{
2483 struct input *input;
2484
2485 input = malloc(sizeof *input);
2486 if (input == NULL)
2487 return;
2488
2489 memset(input, 0, sizeof *input);
2490 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002491 input->input_device =
2492 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002493 input->pointer_focus = NULL;
2494 input->keyboard_focus = NULL;
2495 wl_list_insert(d->input_list.prev, &input->link);
2496
2497 wl_input_device_add_listener(input->input_device,
2498 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002499 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002500
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002501 input->data_device =
2502 wl_data_device_manager_get_data_device(d->data_device_manager,
2503 input->input_device);
2504 wl_data_device_add_listener(input->data_device,
2505 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002506}
2507
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002508static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002509input_destroy(struct input *input)
2510{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002511 input_remove_keyboard_focus(input);
2512 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002513
2514 if (input->drag_offer)
2515 data_offer_destroy(input->drag_offer);
2516
2517 if (input->selection_offer)
2518 data_offer_destroy(input->selection_offer);
2519
2520 wl_data_device_destroy(input->data_device);
2521 wl_list_remove(&input->link);
2522 wl_input_device_destroy(input->input_device);
2523 free(input);
2524}
2525
2526static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002527display_handle_global(struct wl_display *display, uint32_t id,
2528 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002529{
2530 struct display *d = data;
2531
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002532 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002533 d->compositor =
2534 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002535 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002536 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002537 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002538 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002539 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002540 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002541 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002542 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002543 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2544 d->data_device_manager =
2545 wl_display_bind(display, id,
2546 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002547 }
2548}
2549
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002550static void
2551display_render_frame(struct display *d)
2552{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002553 int radius = 8;
2554 cairo_t *cr;
2555
2556 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2557 cr = cairo_create(d->shadow);
2558 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2559 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2560 rounded_rect(cr, 16, 16, 112, 112, radius);
2561 cairo_fill(cr);
2562 cairo_destroy(cr);
2563 blur_surface(d->shadow, 64);
2564
2565 d->active_frame =
2566 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2567 cr = cairo_create(d->active_frame);
2568 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2569 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2570 rounded_rect(cr, 16, 16, 112, 112, radius);
2571 cairo_fill(cr);
2572 cairo_destroy(cr);
2573
2574 d->inactive_frame =
2575 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2576 cr = cairo_create(d->inactive_frame);
2577 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2578 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2579 rounded_rect(cr, 16, 16, 112, 112, radius);
2580 cairo_fill(cr);
2581 cairo_destroy(cr);
2582}
2583
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002584static void
2585init_xkb(struct display *d)
2586{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002587 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002588
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002589 names.rules = "evdev";
2590 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002591 names.layout = option_xkb_layout;
2592 names.variant = option_xkb_variant;
2593 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002594
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002595 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002596 if (!d->xkb) {
2597 fprintf(stderr, "Failed to compile keymap\n");
2598 exit(1);
2599 }
2600}
2601
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002602static void
2603fini_xkb(struct display *display)
2604{
2605 xkb_free_keymap(display->xkb);
2606}
2607
Yuval Fledel45568f62010-12-06 09:18:12 -05002608static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002609init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002610{
2611 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002612 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002613
2614 static const EGLint premul_argb_cfg_attribs[] = {
2615 EGL_SURFACE_TYPE,
2616 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2617 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002618 EGL_RED_SIZE, 1,
2619 EGL_GREEN_SIZE, 1,
2620 EGL_BLUE_SIZE, 1,
2621 EGL_ALPHA_SIZE, 1,
2622 EGL_DEPTH_SIZE, 1,
2623 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2624 EGL_NONE
2625 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002626
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002627 static const EGLint rgb_cfg_attribs[] = {
2628 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2629 EGL_RED_SIZE, 1,
2630 EGL_GREEN_SIZE, 1,
2631 EGL_BLUE_SIZE, 1,
2632 EGL_ALPHA_SIZE, 0,
2633 EGL_DEPTH_SIZE, 1,
2634 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2635 EGL_NONE
2636 };
2637
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002638 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002639 if (!eglInitialize(d->dpy, &major, &minor)) {
2640 fprintf(stderr, "failed to initialize display\n");
2641 return -1;
2642 }
2643
2644 if (!eglBindAPI(EGL_OPENGL_API)) {
2645 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2646 return -1;
2647 }
2648
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002649 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002650 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002651 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002652 return -1;
2653 }
2654
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002655 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2656 &d->rgb_config, 1, &n) || n != 1) {
2657 fprintf(stderr, "failed to choose rgb config\n");
2658 return -1;
2659 }
2660
Benjamin Franzke0c991632011-09-27 21:57:31 +02002661 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2662 if (d->rgb_ctx == NULL) {
2663 fprintf(stderr, "failed to create context\n");
2664 return -1;
2665 }
2666 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2667 EGL_NO_CONTEXT, NULL);
2668 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002669 fprintf(stderr, "failed to create context\n");
2670 return -1;
2671 }
2672
Benjamin Franzke0c991632011-09-27 21:57:31 +02002673 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002674 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002675 return -1;
2676 }
2677
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002678#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002679 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2680 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002681 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002682 return -1;
2683 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002684 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2685 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2686 fprintf(stderr, "failed to get cairo egl argb device\n");
2687 return -1;
2688 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002689#endif
2690
2691 return 0;
2692}
2693
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002694static void
2695fini_egl(struct display *display)
2696{
2697#ifdef HAVE_CAIRO_EGL
2698 cairo_device_destroy(display->argb_device);
2699 cairo_device_destroy(display->rgb_device);
2700#endif
2701
2702 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2703 EGL_NO_CONTEXT);
2704
2705 eglTerminate(display->dpy);
2706 eglReleaseThread();
2707}
2708
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002709static int
2710event_mask_update(uint32_t mask, void *data)
2711{
2712 struct display *d = data;
2713
2714 d->mask = mask;
2715
2716 return 0;
2717}
2718
2719static void
2720handle_display_data(struct task *task, uint32_t events)
2721{
2722 struct display *display =
2723 container_of(task, struct display, display_task);
2724
2725 wl_display_iterate(display->display, display->mask);
2726}
2727
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002728struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002729display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002730{
2731 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002732 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002733 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002734 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002735
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002736 g_type_init();
2737
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002738 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002739 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002740 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002741
2742 xkb_option_group = g_option_group_new("xkb",
2743 "Keyboard options",
2744 "Show all XKB options",
2745 NULL, NULL);
2746 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2747 g_option_context_add_group (context, xkb_option_group);
2748
2749 if (!g_option_context_parse(context, argc, argv, &error)) {
2750 fprintf(stderr, "option parsing failed: %s\n", error->message);
2751 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002752 }
2753
Tim Wiederhake748f6722011-01-23 23:25:25 +01002754 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002755
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002756 d = malloc(sizeof *d);
2757 if (d == NULL)
2758 return NULL;
2759
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002760 memset(d, 0, sizeof *d);
2761
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002762 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002763 if (d->display == NULL) {
2764 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002765 return NULL;
2766 }
2767
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002768 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2769 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2770 d->display_task.run = handle_display_data;
2771 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2772
2773 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002774 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002775 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002776
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002777 /* Set up listener so we'll catch all events. */
2778 wl_display_add_global_listener(d->display,
2779 display_handle_global, d);
2780
2781 /* Process connection events. */
2782 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002783 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002784 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002785
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002786 d->image_target_texture_2d =
2787 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2788 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2789 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2790
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002791 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002792
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002793 display_render_frame(d);
2794
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002795 wl_list_init(&d->window_list);
2796
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002797 init_xkb(d);
2798
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002799 return d;
2800}
2801
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002802static void
2803display_destroy_outputs(struct display *display)
2804{
2805 struct output *tmp;
2806 struct output *output;
2807
2808 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2809 output_destroy(output);
2810}
2811
Pekka Paalanene1207c72011-12-16 12:02:09 +02002812static void
2813display_destroy_inputs(struct display *display)
2814{
2815 struct input *tmp;
2816 struct input *input;
2817
2818 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2819 input_destroy(input);
2820}
2821
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002822void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002823display_destroy(struct display *display)
2824{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002825 if (!wl_list_empty(&display->window_list))
2826 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2827
2828 if (!wl_list_empty(&display->deferred_list))
2829 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2830
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002831 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002832 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002833
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002834 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002835
2836 cairo_surface_destroy(display->active_frame);
2837 cairo_surface_destroy(display->inactive_frame);
2838 cairo_surface_destroy(display->shadow);
2839 destroy_pointer_surfaces(display);
2840
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002841 fini_egl(display);
2842
Pekka Paalanenc2052982011-12-16 11:41:32 +02002843 if (display->shell)
2844 wl_shell_destroy(display->shell);
2845
2846 if (display->shm)
2847 wl_shm_destroy(display->shm);
2848
2849 if (display->data_device_manager)
2850 wl_data_device_manager_destroy(display->data_device_manager);
2851
2852 wl_compositor_destroy(display->compositor);
2853
2854 close(display->epoll_fd);
2855
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002856 wl_display_flush(display->display);
2857 wl_display_destroy(display->display);
2858 free(display);
2859}
2860
2861void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002862display_set_user_data(struct display *display, void *data)
2863{
2864 display->user_data = data;
2865}
2866
2867void *
2868display_get_user_data(struct display *display)
2869{
2870 return display->user_data;
2871}
2872
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002873struct wl_display *
2874display_get_display(struct display *display)
2875{
2876 return display->display;
2877}
2878
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002879struct output *
2880display_get_output(struct display *display)
2881{
2882 return container_of(display->output_list.next, struct output, link);
2883}
2884
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002885struct wl_compositor *
2886display_get_compositor(struct display *display)
2887{
2888 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002889}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002890
2891EGLDisplay
2892display_get_egl_display(struct display *d)
2893{
2894 return d->dpy;
2895}
2896
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002897struct wl_data_source *
2898display_create_data_source(struct display *display)
2899{
2900 return wl_data_device_manager_create_data_source(display->data_device_manager);
2901}
2902
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002903EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002904display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002905{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002906 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002907}
2908
Benjamin Franzke0c991632011-09-27 21:57:31 +02002909EGLConfig
2910display_get_argb_egl_config(struct display *d)
2911{
2912 return d->premultiplied_argb_config;
2913}
2914
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002915struct wl_shell *
2916display_get_shell(struct display *display)
2917{
2918 return display->shell;
2919}
2920
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002921int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002922display_acquire_window_surface(struct display *display,
2923 struct window *window,
2924 EGLContext ctx)
2925{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002926#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002927 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002928 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002929
2930 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002931 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002932 device = cairo_surface_get_device(window->cairo_surface);
2933 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002934 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002935
Benjamin Franzke0c991632011-09-27 21:57:31 +02002936 if (!ctx) {
2937 if (device == display->rgb_device)
2938 ctx = display->rgb_ctx;
2939 else if (device == display->argb_device)
2940 ctx = display->argb_ctx;
2941 else
2942 assert(0);
2943 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002944
2945 data = cairo_surface_get_user_data(window->cairo_surface,
2946 &surface_data_key);
2947
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002948 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002949 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002950 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2951 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002952
2953 return 0;
2954#else
2955 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002956#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002957}
2958
2959void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002960display_release_window_surface(struct display *display,
2961 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002962{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002963#ifdef HAVE_CAIRO_EGL
2964 cairo_device_t *device;
2965
2966 device = cairo_surface_get_device(window->cairo_surface);
2967 if (!device)
2968 return;
2969
2970 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002971 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002972 cairo_device_release(device);
2973#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002974}
2975
2976void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002977display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002978{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002979 wl_list_insert(&display->deferred_list, &task->link);
2980}
2981
2982void
2983display_watch_fd(struct display *display,
2984 int fd, uint32_t events, struct task *task)
2985{
2986 struct epoll_event ep;
2987
2988 ep.events = events;
2989 ep.data.ptr = task;
2990 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2991}
2992
2993void
2994display_run(struct display *display)
2995{
2996 struct task *task;
2997 struct epoll_event ep[16];
2998 int i, count;
2999
Pekka Paalanen826d7952011-12-15 10:14:07 +02003000 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003001 while (1) {
3002 while (display->mask & WL_DISPLAY_WRITABLE)
3003 wl_display_iterate(display->display,
3004 WL_DISPLAY_WRITABLE);
3005
Pekka Paalanen826d7952011-12-15 10:14:07 +02003006 if (!display->running)
3007 break;
3008
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003009 count = epoll_wait(display->epoll_fd,
3010 ep, ARRAY_LENGTH(ep), -1);
3011 for (i = 0; i < count; i++) {
3012 task = ep[i].data.ptr;
3013 task->run(task, ep[i].events);
3014 }
3015
3016 while (!wl_list_empty(&display->deferred_list)) {
3017 task = container_of(display->deferred_list.next,
3018 struct task, link);
3019 wl_list_remove(&task->link);
3020 task->run(task, 0);
3021 }
3022 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003023}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003024
3025void
3026display_exit(struct display *display)
3027{
3028 display->running = 0;
3029}