blob: a8334465216163f36ab2b59bd7653089b18888eb [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øgsberg87a57bb2012-01-09 10:34:35 -0500110 struct widget *widget;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500111 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200112 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500113 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500114 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400116 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400117 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500119 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500120 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400122 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400123 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400124 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500125 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400126 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500127
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500128 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500129
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500130 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400131 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500132 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400133 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500134 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400135 window_data_handler_t data_handler;
136 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500137 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400138
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500139 struct wl_list widget_list;
140 struct widget *focus_widget;
141 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400142
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500143 struct window *menu;
144
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500145 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400146 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500147};
148
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500149struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500150 struct window *window;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 struct wl_list link;
152 struct rectangle allocation;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500153 widget_enter_handler_t enter_handler;
154 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500155 widget_motion_handler_t motion_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 void *user_data;
157};
158
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400159struct input {
160 struct display *display;
161 struct wl_input_device *input_device;
162 struct window *pointer_focus;
163 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400164 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 uint32_t modifiers;
166 int32_t x, y, sx, sy;
167 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168
169 struct wl_data_device *data_device;
170 struct data_offer *drag_offer;
171 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172};
173
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500174struct output {
175 struct display *display;
176 struct wl_output *output;
177 struct rectangle allocation;
178 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200179
180 display_output_handler_t destroy_handler;
181 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182};
183
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500184struct menu {
185 struct window *window;
Kristian Høgsberg391649b2012-01-09 09:22:30 -0500186 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500187 const char **entries;
188 uint32_t time;
189 int current;
190 int count;
191 menu_func_t func;
192};
193
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400194enum {
195 POINTER_DEFAULT = 100,
196 POINTER_UNSET
197};
198
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500199enum window_location {
200 WINDOW_INTERIOR = 0,
201 WINDOW_RESIZING_TOP = 1,
202 WINDOW_RESIZING_BOTTOM = 2,
203 WINDOW_RESIZING_LEFT = 4,
204 WINDOW_RESIZING_TOP_LEFT = 5,
205 WINDOW_RESIZING_BOTTOM_LEFT = 6,
206 WINDOW_RESIZING_RIGHT = 8,
207 WINDOW_RESIZING_TOP_RIGHT = 9,
208 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
209 WINDOW_RESIZING_MASK = 15,
210 WINDOW_EXTERIOR = 16,
211 WINDOW_TITLEBAR = 17,
212 WINDOW_CLIENT_AREA = 18,
213};
214
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400215const char *option_xkb_layout = "us";
216const char *option_xkb_variant = "";
217const char *option_xkb_options = "";
218
219static const GOptionEntry xkb_option_entries[] = {
220 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
221 &option_xkb_layout, "XKB Layout" },
222 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
223 &option_xkb_variant, "XKB Variant" },
224 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
225 &option_xkb_options, "XKB Options" },
226 { NULL }
227};
228
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400229static const cairo_user_data_key_t surface_data_key;
230struct surface_data {
231 struct wl_buffer *buffer;
232};
233
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500234#define MULT(_d,c,a,t) \
235 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
236
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500237#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400238
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100239struct egl_window_surface_data {
240 struct display *display;
241 struct wl_surface *surface;
242 struct wl_egl_window *window;
243 EGLSurface surf;
244};
245
246static void
247egl_window_surface_data_destroy(void *p)
248{
249 struct egl_window_surface_data *data = p;
250 struct display *d = data->display;
251
252 eglDestroySurface(d->dpy, data->surf);
253 wl_egl_window_destroy(data->window);
254 data->surface = NULL;
255
256 free(p);
257}
258
259static cairo_surface_t *
260display_create_egl_window_surface(struct display *display,
261 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400262 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100263 struct rectangle *rectangle)
264{
265 cairo_surface_t *cairo_surface;
266 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400267 EGLConfig config;
268 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200269 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100270
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400271 static const EGLint premul_attribs[] = {
272 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
273 EGL_NONE
274 };
275
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276 data = malloc(sizeof *data);
277 if (data == NULL)
278 return NULL;
279
280 data->display = display;
281 data->surface = surface;
282
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 if (flags & SURFACE_OPAQUE) {
284 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200285 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200286 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400287 } else {
288 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400290 attribs = premul_attribs;
291 }
292
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400293 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 data->surf = eglCreateWindowSurface(display->dpy, config,
298 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299
Benjamin Franzke0c991632011-09-27 21:57:31 +0200300 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301 data->surf,
302 rectangle->width,
303 rectangle->height);
304
305 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
306 data, egl_window_surface_data_destroy);
307
308 return cairo_surface;
309}
310
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500311struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200313 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 EGLImageKHR image;
315 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800316 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500317 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318};
319
320static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400322{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800324 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800329
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500330 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500332 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500333 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400334}
335
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500336EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500337display_get_image_for_egl_image_surface(struct display *display,
338 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500340 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341
342 data = cairo_surface_get_user_data (surface, &surface_data_key);
343
344 return data->image;
345}
346
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500347static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400349 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353 EGLDisplay dpy = display->dpy;
354 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200355 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356
357 data = malloc(sizeof *data);
358 if (data == NULL)
359 return NULL;
360
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800361 data->display = display;
362
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400363 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400364 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500365 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000366 free(data);
367 return NULL;
368 }
369
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 if (flags & SURFACE_OPAQUE) {
371 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 content = CAIRO_CONTENT_COLOR;
373 } else {
374 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 content = CAIRO_CONTENT_COLOR_ALPHA;
376 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500378 data->image = display->create_image(dpy, NULL,
379 EGL_NATIVE_PIXMAP_KHR,
380 (EGLClientBuffer) data->pixmap,
381 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500383 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 free(data);
385 return NULL;
386 }
387
388 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400389 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500394 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 surface = cairo_gl_surface_create_for_texture(data->device,
398 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 data->texture,
400 rectangle->width,
401 rectangle->height);
402
403 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500404 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405
406 return surface;
407}
408
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500409static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410display_create_egl_image_surface_from_file(struct display *display,
411 const char *filename,
412 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413{
414 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415 GdkPixbuf *pixbuf;
416 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400417 int stride, i;
418 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500419 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400420
421 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400423 FALSE, &error);
424 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400425 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400426
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400427 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
428 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500429 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433
434 stride = gdk_pixbuf_get_rowstride(pixbuf);
435 pixels = gdk_pixbuf_get_pixels(pixbuf);
436
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400440 while (p < end) {
441 unsigned int t;
442
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400443 MULT(p[0], p[0], p[3], t);
444 MULT(p[1], p[1], p[3], t);
445 MULT(p[2], p[2], p[3], t);
446 p += 4;
447
448 }
449 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400450
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200451 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000452 if (surface == NULL) {
453 g_object_unref(pixbuf);
454 return NULL;
455 }
456
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800457 data = cairo_surface_get_user_data(surface, &surface_data_key);
458
Benjamin Franzke0c991632011-09-27 21:57:31 +0200459 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800460 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800461 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
462 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200463 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500465 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400466
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400467 return surface;
468}
469
470#endif
471
472struct wl_buffer *
473display_get_buffer_for_surface(struct display *display,
474 cairo_surface_t *surface)
475{
476 struct surface_data *data;
477
478 data = cairo_surface_get_user_data (surface, &surface_data_key);
479
480 return data->buffer;
481}
482
483struct shm_surface_data {
484 struct surface_data data;
485 void *map;
486 size_t length;
487};
488
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500489static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490shm_surface_data_destroy(void *p)
491{
492 struct shm_surface_data *data = p;
493
494 wl_buffer_destroy(data->data.buffer);
495 munmap(data->map, data->length);
496}
497
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500498static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400500 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501{
502 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400503 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400504 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800505 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506 char filename[] = "/tmp/wayland-shm-XXXXXX";
507
508 data = malloc(sizeof *data);
509 if (data == NULL)
510 return NULL;
511
512 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
513 rectangle->width);
514 data->length = stride * rectangle->height;
515 fd = mkstemp(filename);
516 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000517 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518 return NULL;
519 }
520 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000521 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400522 close(fd);
523 return NULL;
524 }
525
526 data->map = mmap(NULL, data->length,
527 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
528 unlink(filename);
529
530 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000531 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400532 close(fd);
533 return NULL;
534 }
535
536 surface = cairo_image_surface_create_for_data (data->map,
537 CAIRO_FORMAT_ARGB32,
538 rectangle->width,
539 rectangle->height,
540 stride);
541
542 cairo_surface_set_user_data (surface, &surface_data_key,
543 data, shm_surface_data_destroy);
544
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400546 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400548 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550 data->data.buffer = wl_shm_create_buffer(display->shm,
551 fd,
552 rectangle->width,
553 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400554 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555
556 close(fd);
557
558 return surface;
559}
560
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500561static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562display_create_shm_surface_from_file(struct display *display,
563 const char *filename,
564 struct rectangle *rect)
565{
566 cairo_surface_t *surface;
567 GdkPixbuf *pixbuf;
568 GError *error = NULL;
569 int stride, i;
570 unsigned char *pixels, *p, *end, *dest_data;
571 int dest_stride;
572 uint32_t *d;
573
574 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
575 rect->width, rect->height,
576 FALSE, &error);
577 if (error != NULL)
578 return NULL;
579
580 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
581 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500582 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400583 return NULL;
584 }
585
586 stride = gdk_pixbuf_get_rowstride(pixbuf);
587 pixels = gdk_pixbuf_get_pixels(pixbuf);
588
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400589 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000590 if (surface == NULL) {
591 g_object_unref(pixbuf);
592 return NULL;
593 }
594
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400595 dest_data = cairo_image_surface_get_data (surface);
596 dest_stride = cairo_image_surface_get_stride (surface);
597
598 for (i = 0; i < rect->height; i++) {
599 d = (uint32_t *) (dest_data + i * dest_stride);
600 p = pixels + i * stride;
601 end = p + rect->width * 4;
602 while (p < end) {
603 unsigned int t;
604 unsigned char a, r, g, b;
605
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400606 a = p[3];
607 MULT(r, p[0], a, t);
608 MULT(g, p[1], a, t);
609 MULT(b, p[2], a, t);
610 p += 4;
611 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
612 }
613 }
614
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500615 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400616
617 return surface;
618}
619
nobled7b87cb02011-02-01 18:51:47 +0000620static int
621check_size(struct rectangle *rect)
622{
623 if (rect->width && rect->height)
624 return 0;
625
626 fprintf(stderr, "tried to create surface of "
627 "width: %d, height: %d\n", rect->width, rect->height);
628 return -1;
629}
630
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400631cairo_surface_t *
632display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400634 struct rectangle *rectangle,
635 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636{
nobled7b87cb02011-02-01 18:51:47 +0000637 if (check_size(rectangle) < 0)
638 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500639#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500640 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641 if (surface)
642 return display_create_egl_window_surface(display,
643 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400644 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645 rectangle);
646 else
647 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400648 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500650 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400652 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653}
654
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500655static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656display_create_surface_from_file(struct display *display,
657 const char *filename,
658 struct rectangle *rectangle)
659{
nobled7b87cb02011-02-01 18:51:47 +0000660 if (check_size(rectangle) < 0)
661 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500662#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500663 if (display->dpy) {
664 return display_create_egl_image_surface_from_file(display,
665 filename,
666 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500667 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670}
Yuval Fledel45568f62010-12-06 09:18:12 -0500671 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672 const char *filename;
673 int hotspot_x, hotspot_y;
674} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500675 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
676 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
677 { DATADIR "/weston/bottom_side.png", 16, 20 },
678 { DATADIR "/weston/grabbing.png", 20, 17 },
679 { DATADIR "/weston/left_ptr.png", 10, 5 },
680 { DATADIR "/weston/left_side.png", 10, 20 },
681 { DATADIR "/weston/right_side.png", 30, 19 },
682 { DATADIR "/weston/top_left_corner.png", 8, 8 },
683 { DATADIR "/weston/top_right_corner.png", 26, 8 },
684 { DATADIR "/weston/top_side.png", 18, 8 },
685 { DATADIR "/weston/xterm.png", 15, 15 },
686 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400687};
688
689static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400690create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691{
692 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400694 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400697 display->pointer_surfaces =
698 malloc(count * sizeof *display->pointer_surfaces);
699 rect.width = width;
700 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400703 display_create_surface_from_file(display,
704 pointer_images[i].filename,
705 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100706 if (!display->pointer_surfaces[i]) {
707 fprintf(stderr, "Error loading pointer image: %s\n",
708 pointer_images[i].filename);
709 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400710 }
711
712}
713
Pekka Paalanen325bb602011-12-19 10:31:45 +0200714static void
715destroy_pointer_surfaces(struct display *display)
716{
717 int i, count;
718
719 count = ARRAY_LENGTH(pointer_images);
720 for (i = 0; i < count; ++i) {
721 if (display->pointer_surfaces[i])
722 cairo_surface_destroy(display->pointer_surfaces[i]);
723 }
724 free(display->pointer_surfaces);
725}
726
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400727cairo_surface_t *
728display_get_pointer_surface(struct display *display, int pointer,
729 int *width, int *height,
730 int *hotspot_x, int *hotspot_y)
731{
732 cairo_surface_t *surface;
733
734 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500735#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400736 *width = cairo_gl_surface_get_width(surface);
737 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000738#else
739 *width = cairo_image_surface_get_width(surface);
740 *height = cairo_image_surface_get_height(surface);
741#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 *hotspot_x = pointer_images[pointer].hotspot_x;
743 *hotspot_y = pointer_images[pointer].hotspot_y;
744
745 return cairo_surface_reference(surface);
746}
747
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400748static void
749window_attach_surface(struct window *window);
750
751static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400752free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400753{
754 struct window *window = data;
755
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400756 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400757 cairo_surface_destroy(window->pending_surface);
758 window->pending_surface = NULL;
759 if (window->cairo_surface)
760 window_attach_surface(window);
761}
762
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400763static const struct wl_callback_listener free_surface_listener = {
764 free_surface
765};
766
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500767static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200768window_get_resize_dx_dy(struct window *window, int *x, int *y)
769{
770 if (window->resize_edges & WINDOW_RESIZING_LEFT)
771 *x = window->server_allocation.width - window->allocation.width;
772 else
773 *x = 0;
774
775 if (window->resize_edges & WINDOW_RESIZING_TOP)
776 *y = window->server_allocation.height -
777 window->allocation.height;
778 else
779 *y = 0;
780
781 window->resize_edges = 0;
782}
783
784static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400785window_set_type(struct window *window)
786{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200787 if (!window->shell_surface)
788 return;
789
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400790 switch (window->type) {
791 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200792 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400793 break;
794 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200795 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400796 break;
797 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200798 wl_shell_surface_set_transient(window->shell_surface,
799 window->parent->shell_surface,
800 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400801 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500802 case TYPE_MENU:
803 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400804 case TYPE_CUSTOM:
805 break;
806 }
807}
808
809static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500810window_attach_surface(struct window *window)
811{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400812 struct display *display = window->display;
813 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400814 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100816 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500818 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100819
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400820 if (display->shell)
821 window_set_type(window);
822
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000824#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
826 data = cairo_surface_get_user_data(window->cairo_surface,
827 &surface_data_key);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 cairo_gl_surface_swapbuffers(window->cairo_surface);
830 wl_egl_window_get_attached_size(data->window,
831 &window->server_allocation.width,
832 &window->server_allocation.height);
833 break;
834 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000835#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100836 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200837 window_get_resize_dx_dy(window, &x, &y);
838
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100839 if (window->pending_surface != NULL)
840 return;
841
842 window->pending_surface = window->cairo_surface;
843 window->cairo_surface = NULL;
844
845 buffer =
846 display_get_buffer_for_surface(display,
847 window->pending_surface);
848
849 wl_surface_attach(window->surface, buffer, x, y);
850 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400851 cb = wl_display_sync(display->display);
852 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100853 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000854 default:
855 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100856 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500857
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500858 wl_surface_damage(window->surface, 0, 0,
859 window->allocation.width,
860 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500861}
862
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400864window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100866 if (window->cairo_surface) {
867 switch (window->buffer_type) {
868 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
869 case WINDOW_BUFFER_TYPE_SHM:
870 display_surface_damage(window->display,
871 window->cairo_surface,
872 0, 0,
873 window->allocation.width,
874 window->allocation.height);
875 break;
876 default:
877 break;
878 }
879 window_attach_surface(window);
880 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500881}
882
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400883void
884window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500886 cairo_surface_reference(surface);
887
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400888 if (window->cairo_surface != NULL)
889 cairo_surface_destroy(window->cairo_surface);
890
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500891 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400892}
893
Benjamin Franzke22d54812011-07-16 19:50:32 +0000894#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895static void
896window_resize_cairo_window_surface(struct window *window)
897{
898 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200899 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100900
901 data = cairo_surface_get_user_data(window->cairo_surface,
902 &surface_data_key);
903
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200904 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100905 wl_egl_window_resize(data->window,
906 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 window->allocation.height,
908 x,y);
909
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 cairo_gl_surface_set_size(window->cairo_surface,
911 window->allocation.width,
912 window->allocation.height);
913}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000914#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100915
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400916struct display *
917window_get_display(struct window *window)
918{
919 return window->display;
920}
921
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500922struct widget *
923window_get_widget(struct window *window)
924{
925 return window->widget;
926}
927
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928void
929window_create_surface(struct window *window)
930{
931 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400932 uint32_t flags = 0;
933
934 if (!window->transparent)
935 flags = SURFACE_OPAQUE;
936
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500938#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
940 if (window->cairo_surface) {
941 window_resize_cairo_window_surface(window);
942 return;
943 }
944 surface = display_create_surface(window->display,
945 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400946 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100947 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500948 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
953#endif
954 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400956 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800958 default:
959 surface = NULL;
960 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400962
963 window_set_surface(window, surface);
964 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400965}
966
967static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500968window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500969{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500970 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500971 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400972 cairo_surface_t *frame;
973 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500974
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400975 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400976
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400977 width = window->allocation.width;
978 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500979
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500980 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500981
Kristian Høgsberg09531622010-06-14 23:22:15 -0400982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400983 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400984 cairo_paint(cr);
985
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 tile_mask(cr, window->display->shadow,
988 shadow_dx, shadow_dy, width, height,
989 window->margin + 10 - shadow_dx,
990 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500991
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400992 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400993 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400994 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400995 frame = window->display->inactive_frame;
996
997 tile_source(cr, frame, 0, 0, width, height,
998 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500999
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001000 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1001 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001002 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001003 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001004 if (window->keyboard_device)
1005 cairo_set_source_rgb(cr, 0, 0, 0);
1006 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001007 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001008 cairo_show_text(cr, window->title);
1009
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001010 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001011}
1012
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001013void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001014window_destroy(struct window *window)
1015{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001016 struct display *display = window->display;
1017 struct input *input;
1018
1019 if (window->redraw_scheduled)
1020 wl_list_remove(&window->redraw_task.link);
1021
1022 wl_list_for_each(input, &display->input_list, link) {
1023 if (input->pointer_focus == window)
1024 input->pointer_focus = NULL;
1025 if (input->keyboard_focus == window)
1026 input->keyboard_focus = NULL;
1027 }
1028
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001029 if (window->shell_surface)
1030 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001031 wl_surface_destroy(window->surface);
1032 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001033
1034 if (window->cairo_surface != NULL)
1035 cairo_surface_destroy(window->cairo_surface);
1036 if (window->pending_surface != NULL)
1037 cairo_surface_destroy(window->pending_surface);
1038
1039 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001040 free(window);
1041}
1042
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001043static struct widget *
1044window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001045{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001046 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001047
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001048 wl_list_for_each(widget, &window->widget_list, link) {
1049 if (widget->allocation.x <= x &&
1050 x < widget->allocation.x + widget->allocation.width &&
1051 widget->allocation.y <= y &&
1052 y < widget->allocation.y + widget->allocation.height) {
1053 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054 }
1055 }
1056
1057 return NULL;
1058}
1059
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060struct widget *
1061window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 widget = malloc(sizeof *widget);
1066 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001067 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 widget->user_data = data;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05001069 wl_list_insert(&window->widget_list, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072}
1073
1074void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001075window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001076{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001078
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001079 wl_list_for_each(widget, &window->widget_list, link)
1080 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081}
1082
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001083struct widget *
1084window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
1089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_set_allocation(struct widget *widget,
1097 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099 widget->allocation.x = x;
1100 widget->allocation.y = y;
1101 widget->allocation.width = width;
1102 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103}
1104
1105void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001106widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001107{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001108 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001109}
1110
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001111void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001112widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001113{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001114 widget->enter_handler = handler;
1115}
1116
1117void
1118widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1119{
1120 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001121}
1122
1123void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001124widget_set_motion_handler(struct widget *widget,
1125 widget_motion_handler_t handler)
1126{
1127 widget->motion_handler = handler;
1128}
1129
1130void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001131widget_schedule_redraw(struct widget *widget)
1132{
1133 window_schedule_redraw(widget->window);
1134}
1135
1136void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001137window_draw(struct window *window)
1138{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001139 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001140 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001141 else
1142 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001143}
1144
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001145cairo_surface_t *
1146window_get_surface(struct window *window)
1147{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001148 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001149}
1150
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001151struct wl_surface *
1152window_get_wl_surface(struct window *window)
1153{
1154 return window->surface;
1155}
1156
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157struct wl_shell_surface *
1158window_get_wl_shell_surface(struct window *window)
1159{
1160 return window->shell_surface;
1161}
1162
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001163static int
1164get_pointer_location(struct window *window, int32_t x, int32_t y)
1165{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001166 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001167 const int grip_size = 8;
1168
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001169 if (!window->decoration)
1170 return WINDOW_CLIENT_AREA;
1171
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001172 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001173 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001174 else if (window->margin <= x && x < window->margin + grip_size)
1175 hlocation = WINDOW_RESIZING_LEFT;
1176 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001177 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001178 else if (x < window->allocation.width - window->margin)
1179 hlocation = WINDOW_RESIZING_RIGHT;
1180 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001181 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001182
1183 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001184 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001185 else if (window->margin <= y && y < window->margin + grip_size)
1186 vlocation = WINDOW_RESIZING_TOP;
1187 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001188 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001189 else if (y < window->allocation.height - window->margin)
1190 vlocation = WINDOW_RESIZING_BOTTOM;
1191 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001192 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001193
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001194 location = vlocation | hlocation;
1195 if (location & WINDOW_EXTERIOR)
1196 location = WINDOW_EXTERIOR;
1197 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1198 location = WINDOW_TITLEBAR;
1199 else if (location == WINDOW_INTERIOR)
1200 location = WINDOW_CLIENT_AREA;
1201
1202 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203}
1204
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001205static int
1206input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001207{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209
1210 location = get_pointer_location(input->pointer_focus,
1211 input->sx, input->sy);
1212 switch (location) {
1213 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001214 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001216 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001218 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001219 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001220 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001222 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001223 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001224 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001225 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001226 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001227 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001228 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001229 case WINDOW_EXTERIOR:
1230 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001231 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001232 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001233 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001235}
1236
1237void
1238input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1239{
1240 struct display *display = input->display;
1241 struct wl_buffer *buffer;
1242 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001243
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001244 if (pointer == input->current_pointer_image)
1245 return;
1246
1247 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001248 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001249
1250 if (!surface)
1251 return;
1252
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001254 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 pointer_images[pointer].hotspot_x,
1256 pointer_images[pointer].hotspot_y);
1257}
1258
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001259static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001260window_set_focus_widget(struct window *window, struct widget *focus,
1261 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001262{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001263 struct widget *old;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001264
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001265 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001266 return;
1267
Kristian Høgsbergee143232012-01-09 08:42:24 -05001268 old = window->focus_widget;
1269 if (old) {
1270 if (old->leave_handler)
1271 old->leave_handler(old, input, old->user_data);
1272 window->focus_widget = NULL;
1273 }
1274
1275 if (focus) {
1276 if (focus->enter_handler)
1277 focus->enter_handler(focus, input, time,
1278 x, y, focus->user_data);
1279 window->focus_widget = focus;
1280 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001281}
1282
1283static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001284input_handle_motion(void *data, struct wl_input_device *input_device,
1285 uint32_t time,
1286 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001287{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001288 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001289 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001290 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001291 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001292
1293 input->x = x;
1294 input->y = y;
1295 input->sx = sx;
1296 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001298 if (!window->focus_widget || !window->widget_grab_button) {
1299 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001300 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001301 }
1302
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001303 widget = window->focus_widget;
1304 if (widget && widget->motion_handler)
1305 pointer = widget->motion_handler(widget, input, time, sx, sy,
1306 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001307
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001308 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001309 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001310}
1311
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001312static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001313window_menu_func(struct window *window, int index, void *data)
1314{
1315 switch (index) {
1316 case 0: /* close */
1317 if (window->close_handler)
1318 window->close_handler(window->parent,
1319 window->user_data);
1320 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001321 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001322 break;
1323 case 1: /* fullscreen */
1324 /* we don't have a way to get out of fullscreen for now */
1325 window_set_fullscreen(window, 1);
1326 break;
1327 case 2: /* rotate */
1328 case 3: /* scale */
1329 break;
1330 }
1331}
1332
1333
1334static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001335input_handle_button(void *data,
1336 struct wl_input_device *input_device,
1337 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001338{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001339 struct input *input = data;
1340 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001341 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001342 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001343 int32_t x, y;
1344 static const char *entries[] = {
1345 "Close", "Fullscreen", "Rotate", "Scale"
1346 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001347
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001348 if (window->focus_widget && window->widget_grab_button == 0 && state)
1349 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001350
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001351 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001352
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001353 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001354 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001355 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001356 if (!window->shell_surface)
1357 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001358 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001359 wl_shell_surface_move(window->shell_surface,
1360 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001361 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001362 case WINDOW_RESIZING_TOP:
1363 case WINDOW_RESIZING_BOTTOM:
1364 case WINDOW_RESIZING_LEFT:
1365 case WINDOW_RESIZING_RIGHT:
1366 case WINDOW_RESIZING_TOP_LEFT:
1367 case WINDOW_RESIZING_TOP_RIGHT:
1368 case WINDOW_RESIZING_BOTTOM_LEFT:
1369 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001370 if (!window->shell_surface)
1371 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001372 wl_shell_surface_resize(window->shell_surface,
1373 input_device, time,
1374 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001375 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001376 case WINDOW_CLIENT_AREA:
1377 if (window->button_handler)
1378 (*window->button_handler)(window,
1379 input, time,
1380 button, state,
1381 window->user_data);
1382 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001383 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001384 } else if (button == BTN_RIGHT && state == 1) {
1385 switch (location) {
1386 default:
1387 input_get_position(input, &x, &y);
1388 window->menu = window_create_menu(window->display,
1389 input, time,
1390 window,
1391 x - 10, y - 10,
1392 window_menu_func,
1393 entries, 4);
1394 window_schedule_redraw(window->menu);
1395 break;
1396 case WINDOW_CLIENT_AREA:
1397 if (window->button_handler)
1398 (*window->button_handler)(window,
1399 input, time,
1400 button, state,
1401 window->user_data);
1402 break;
1403 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001404 } else {
1405 if (window->button_handler)
1406 (*window->button_handler)(window,
1407 input, time,
1408 button, state,
1409 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001410 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001411
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001412 if (window->focus_widget &&
1413 window->widget_grab_button == button && !state) {
1414 window->widget_grab_button = 0;
1415 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001416 window_set_focus_widget(window, widget, input, time,
1417 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001418 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001419}
1420
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001421static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001422input_handle_key(void *data, struct wl_input_device *input_device,
1423 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001424{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001425 struct input *input = data;
1426 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001427 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001428 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001429
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001430 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001431 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001432 return;
1433
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001434 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001435 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001436 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1437 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001438
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001439 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1440
1441 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001442 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001443 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001444 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001445
1446 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001447 (*window->key_handler)(window, input, time, key, sym, state,
1448 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001449}
1450
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001451static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001452input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001453{
1454 struct window *window = input->pointer_focus;
1455
1456 if (!window)
1457 return;
1458
Kristian Høgsbergee143232012-01-09 08:42:24 -05001459 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001460
Pekka Paalanene1207c72011-12-16 12:02:09 +02001461 input->pointer_focus = NULL;
1462 input->current_pointer_image = POINTER_UNSET;
1463}
1464
1465static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001466input_handle_pointer_focus(void *data,
1467 struct wl_input_device *input_device,
1468 uint32_t time, struct wl_surface *surface,
1469 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001470{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001471 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001473 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001474 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001475
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001476 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001477 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001478 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001479
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001480 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001481 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001482 window = input->pointer_focus;
1483
Kristian Høgsberg59826582011-01-20 11:56:57 -05001484 input->x = x;
1485 input->y = y;
1486 input->sx = sx;
1487 input->sy = sy;
1488
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001489 pointer = POINTER_LEFT_PTR;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001490 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001491 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001492
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001493 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001494 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001495 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001496}
1497
1498static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001499input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001500{
1501 struct window *window = input->keyboard_focus;
1502
1503 if (!window)
1504 return;
1505
1506 window->keyboard_device = NULL;
1507 if (window->keyboard_focus_handler)
1508 (*window->keyboard_focus_handler)(window, NULL,
1509 window->user_data);
1510
1511 input->keyboard_focus = NULL;
1512}
1513
1514static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001515input_handle_keyboard_focus(void *data,
1516 struct wl_input_device *input_device,
1517 uint32_t time,
1518 struct wl_surface *surface,
1519 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001520{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001521 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001522 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001523 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001524 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001525
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001526 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001527
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528 if (surface)
1529 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530
1531 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001532 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001533 for (k = keys->data; k < end; k++)
1534 input->modifiers |= d->xkb->map->modmap[*k];
1535
1536 window = input->keyboard_focus;
1537 if (window) {
1538 window->keyboard_device = input;
1539 if (window->keyboard_focus_handler)
1540 (*window->keyboard_focus_handler)(window,
1541 window->keyboard_device,
1542 window->user_data);
1543 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001544}
1545
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001546static void
1547input_handle_touch_down(void *data,
1548 struct wl_input_device *wl_input_device,
1549 uint32_t time, struct wl_surface *surface,
1550 int32_t id, int32_t x, int32_t y)
1551{
1552}
1553
1554static void
1555input_handle_touch_up(void *data,
1556 struct wl_input_device *wl_input_device,
1557 uint32_t time, int32_t id)
1558{
1559}
1560
1561static void
1562input_handle_touch_motion(void *data,
1563 struct wl_input_device *wl_input_device,
1564 uint32_t time, int32_t id, int32_t x, int32_t y)
1565{
1566}
1567
1568static void
1569input_handle_touch_frame(void *data,
1570 struct wl_input_device *wl_input_device)
1571{
1572}
1573
1574static void
1575input_handle_touch_cancel(void *data,
1576 struct wl_input_device *wl_input_device)
1577{
1578}
1579
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001580static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001581 input_handle_motion,
1582 input_handle_button,
1583 input_handle_key,
1584 input_handle_pointer_focus,
1585 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001586 input_handle_touch_down,
1587 input_handle_touch_up,
1588 input_handle_touch_motion,
1589 input_handle_touch_frame,
1590 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001591};
1592
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593void
1594input_get_position(struct input *input, int32_t *x, int32_t *y)
1595{
1596 *x = input->sx;
1597 *y = input->sy;
1598}
1599
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001600struct wl_input_device *
1601input_get_input_device(struct input *input)
1602{
1603 return input->input_device;
1604}
1605
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001606uint32_t
1607input_get_modifiers(struct input *input)
1608{
1609 return input->modifiers;
1610}
1611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001612struct data_offer {
1613 struct wl_data_offer *offer;
1614 struct input *input;
1615 struct wl_array types;
1616 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001617
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001618 struct task io_task;
1619 int fd;
1620 data_func_t func;
1621 int32_t x, y;
1622 void *user_data;
1623};
1624
1625static void
1626data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1627{
1628 struct data_offer *offer = data;
1629 char **p;
1630
1631 p = wl_array_add(&offer->types, sizeof *p);
1632 *p = strdup(type);
1633}
1634
1635static const struct wl_data_offer_listener data_offer_listener = {
1636 data_offer_offer,
1637};
1638
1639static void
1640data_offer_destroy(struct data_offer *offer)
1641{
1642 char **p;
1643
1644 offer->refcount--;
1645 if (offer->refcount == 0) {
1646 wl_data_offer_destroy(offer->offer);
1647 for (p = offer->types.data; *p; p++)
1648 free(*p);
1649 wl_array_release(&offer->types);
1650 free(offer);
1651 }
1652}
1653
1654static void
1655data_device_data_offer(void *data,
1656 struct wl_data_device *data_device, uint32_t id)
1657{
1658 struct data_offer *offer;
1659
1660 offer = malloc(sizeof *offer);
1661
1662 wl_array_init(&offer->types);
1663 offer->refcount = 1;
1664 offer->input = data;
1665
1666 /* FIXME: Generate typesafe wrappers for this */
1667 offer->offer = (struct wl_data_offer *)
1668 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1669 id, &wl_data_offer_interface);
1670
1671 wl_data_offer_add_listener(offer->offer,
1672 &data_offer_listener, offer);
1673}
1674
1675static void
1676data_device_enter(void *data, struct wl_data_device *data_device,
1677 uint32_t time, struct wl_surface *surface,
1678 int32_t x, int32_t y, struct wl_data_offer *offer)
1679{
1680 struct input *input = data;
1681 struct window *window;
1682 char **p;
1683
1684 input->drag_offer = wl_data_offer_get_user_data(offer);
1685 window = wl_surface_get_user_data(surface);
1686 input->pointer_focus = window;
1687
1688 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1689 *p = NULL;
1690
1691 window = input->pointer_focus;
1692 if (window->data_handler)
1693 window->data_handler(window, input, time, x, y,
1694 input->drag_offer->types.data,
1695 window->user_data);
1696}
1697
1698static void
1699data_device_leave(void *data, struct wl_data_device *data_device)
1700{
1701 struct input *input = data;
1702
1703 data_offer_destroy(input->drag_offer);
1704 input->drag_offer = NULL;
1705}
1706
1707static void
1708data_device_motion(void *data, struct wl_data_device *data_device,
1709 uint32_t time, int32_t x, int32_t y)
1710{
1711 struct input *input = data;
1712 struct window *window = input->pointer_focus;
1713
1714 input->sx = x;
1715 input->sy = y;
1716
1717 if (window->data_handler)
1718 window->data_handler(window, input, time, x, y,
1719 input->drag_offer->types.data,
1720 window->user_data);
1721}
1722
1723static void
1724data_device_drop(void *data, struct wl_data_device *data_device)
1725{
1726 struct input *input = data;
1727 struct window *window = input->pointer_focus;
1728
1729 if (window->drop_handler)
1730 window->drop_handler(window, input,
1731 input->sx, input->sy, window->user_data);
1732}
1733
1734static void
1735data_device_selection(void *data,
1736 struct wl_data_device *wl_data_device,
1737 struct wl_data_offer *offer)
1738{
1739 struct input *input = data;
1740 char **p;
1741
1742 if (input->selection_offer)
1743 data_offer_destroy(input->selection_offer);
1744
1745 input->selection_offer = wl_data_offer_get_user_data(offer);
1746 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1747 *p = NULL;
1748}
1749
1750static const struct wl_data_device_listener data_device_listener = {
1751 data_device_data_offer,
1752 data_device_enter,
1753 data_device_leave,
1754 data_device_motion,
1755 data_device_drop,
1756 data_device_selection
1757};
1758
1759struct wl_data_device *
1760input_get_data_device(struct input *input)
1761{
1762 return input->data_device;
1763}
1764
1765void
1766input_set_selection(struct input *input,
1767 struct wl_data_source *source, uint32_t time)
1768{
1769 wl_data_device_set_selection(input->data_device, source, time);
1770}
1771
1772void
1773input_accept(struct input *input, uint32_t time, const char *type)
1774{
1775 wl_data_offer_accept(input->drag_offer->offer, time, type);
1776}
1777
1778static void
1779offer_io_func(struct task *task, uint32_t events)
1780{
1781 struct data_offer *offer =
1782 container_of(task, struct data_offer, io_task);
1783 unsigned int len;
1784 char buffer[4096];
1785
1786 len = read(offer->fd, buffer, sizeof buffer);
1787 offer->func(buffer, len,
1788 offer->x, offer->y, offer->user_data);
1789
1790 if (len == 0) {
1791 close(offer->fd);
1792 data_offer_destroy(offer);
1793 }
1794}
1795
1796static void
1797data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1798 data_func_t func, void *user_data)
1799{
1800 int p[2];
1801
1802 pipe2(p, O_CLOEXEC);
1803 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1804 close(p[1]);
1805
1806 offer->io_task.run = offer_io_func;
1807 offer->fd = p[0];
1808 offer->func = func;
1809 offer->refcount++;
1810 offer->user_data = user_data;
1811
1812 display_watch_fd(offer->input->display,
1813 offer->fd, EPOLLIN, &offer->io_task);
1814}
1815
1816void
1817input_receive_drag_data(struct input *input, const char *mime_type,
1818 data_func_t func, void *data)
1819{
1820 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1821 input->drag_offer->x = input->sx;
1822 input->drag_offer->y = input->sy;
1823}
1824
1825int
1826input_receive_selection_data(struct input *input, const char *mime_type,
1827 data_func_t func, void *data)
1828{
1829 char **p;
1830
1831 if (input->selection_offer == NULL)
1832 return -1;
1833
1834 for (p = input->selection_offer->types.data; *p; p++)
1835 if (strcmp(mime_type, *p) == 0)
1836 break;
1837
1838 if (*p == NULL)
1839 return -1;
1840
1841 data_offer_receive_data(input->selection_offer,
1842 mime_type, func, data);
1843 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001844}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001845
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001846int
1847input_receive_selection_data_to_fd(struct input *input,
1848 const char *mime_type, int fd)
1849{
1850 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1851
1852 return 0;
1853}
1854
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001855void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001856window_move(struct window *window, struct input *input, uint32_t time)
1857{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001858 if (!window->shell_surface)
1859 return;
1860
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001861 wl_shell_surface_move(window->shell_surface,
1862 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001863}
1864
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001865static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001866handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001867 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001868 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001869{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001870 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001871 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001872
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001873 /* FIXME: this is probably the wrong place to check for width
1874 * or height <= 0, but it prevents the compositor from crashing
1875 */
1876 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001877 return;
1878
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001879 window->resize_edges = edges;
1880
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001881 if (window->resize_handler) {
1882 child_width = width - 20 - window->margin * 2;
1883 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001884
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001885 (*window->resize_handler)(window,
1886 child_width, child_height,
1887 window->user_data);
1888 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001889 window->allocation.width = width;
1890 window->allocation.height = height;
1891
1892 if (window->redraw_handler)
1893 window_schedule_redraw(window);
1894 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001895}
1896
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001897static void
1898handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1899{
1900 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001901 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001902 /* FIXME: Need more context in this event, at least the input
1903 * device. Or just use wl_callback. */
1904
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001905 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001906 window_destroy(window);
1907}
1908
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001909static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001910 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001911 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001912};
1913
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001914void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001915window_get_allocation(struct window *window,
1916 struct rectangle *allocation)
1917{
1918 *allocation = window->allocation;
1919}
1920
1921void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001922window_get_child_allocation(struct window *window,
1923 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001924{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001925 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001926 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001927 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001928 allocation->x = window->margin + 10;
1929 allocation->y = window->margin + 50;
1930 allocation->width =
1931 window->allocation.width - 20 - window->margin * 2;
1932 allocation->height =
1933 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001934 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001935}
1936
1937void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001938window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001939{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001940 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001941 window->allocation.x = 20 + window->margin;
1942 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001943 window->allocation.width = width + 20 + window->margin * 2;
1944 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001945 } else {
1946 window->allocation.x = 0;
1947 window->allocation.y = 0;
1948 window->allocation.width = width;
1949 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001950 }
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05001951
1952 window->widget->allocation = window->allocation;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001953}
1954
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001955static void
1956idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001957{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001958 struct window *window =
1959 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001960
1961 window->redraw_handler(window, window->user_data);
1962 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001963}
1964
1965void
1966window_schedule_redraw(struct window *window)
1967{
1968 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001969 window->redraw_task.run = idle_redraw;
1970 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001971 window->redraw_scheduled = 1;
1972 }
1973}
1974
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001975void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001976window_set_custom(struct window *window)
1977{
1978 window->type = TYPE_CUSTOM;
1979}
1980
1981void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001982window_set_fullscreen(struct window *window, int fullscreen)
1983{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001984 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001985 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001986
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001987 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001988 return;
1989
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001990 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001991 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001992 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001993 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001994 width = output->allocation.width;
1995 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001996 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001997 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001998 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001999 width = window->saved_allocation.width - 20 - window->margin * 2;
2000 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002001 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002002 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002003
2004 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002005}
2006
2007void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002008window_set_decoration(struct window *window, int decoration)
2009{
2010 window->decoration = decoration;
2011}
2012
2013void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002014window_set_user_data(struct window *window, void *data)
2015{
2016 window->user_data = data;
2017}
2018
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002019void *
2020window_get_user_data(struct window *window)
2021{
2022 return window->user_data;
2023}
2024
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002025void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002026window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002027 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002028{
2029 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002030}
2031
2032void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002033window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002034 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002035{
2036 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002037}
2038
2039void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002040window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002041 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002042{
2043 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002044}
2045
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002046void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002047window_set_button_handler(struct window *window,
2048 window_button_handler_t handler)
2049{
2050 window->button_handler = handler;
2051}
2052
2053void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002054window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002055 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002056{
2057 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002058}
2059
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002060void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061window_set_data_handler(struct window *window, window_data_handler_t handler)
2062{
2063 window->data_handler = handler;
2064}
2065
2066void
2067window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2068{
2069 window->drop_handler = handler;
2070}
2071
2072void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002073window_set_close_handler(struct window *window,
2074 window_close_handler_t handler)
2075{
2076 window->close_handler = handler;
2077}
2078
2079void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002080window_set_transparent(struct window *window, int transparent)
2081{
2082 window->transparent = transparent;
2083}
2084
2085void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002086window_set_title(struct window *window, const char *title)
2087{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002088 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002089 window->title = strdup(title);
2090}
2091
2092const char *
2093window_get_title(struct window *window)
2094{
2095 return window->title;
2096}
2097
2098void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002099display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2100 int32_t x, int32_t y, int32_t width, int32_t height)
2101{
2102 struct wl_buffer *buffer;
2103
2104 buffer = display_get_buffer_for_surface(display, cairo_surface);
2105
2106 wl_buffer_damage(buffer, x, y, width, height);
2107}
2108
2109void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002110window_damage(struct window *window, int32_t x, int32_t y,
2111 int32_t width, int32_t height)
2112{
2113 wl_surface_damage(window->surface, x, y, width, height);
2114}
2115
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002116static struct window *
2117window_create_internal(struct display *display, struct window *parent,
2118 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002119{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002120 struct window *window;
2121
2122 window = malloc(sizeof *window);
2123 if (window == NULL)
2124 return NULL;
2125
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002126 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002127 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002128 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002129 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002130 if (display->shell) {
2131 window->shell_surface =
2132 wl_shell_get_shell_surface(display->shell,
2133 window->surface);
2134 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002135 window->allocation.x = 0;
2136 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002137 window->allocation.width = width;
2138 window->allocation.height = height;
2139 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002140 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002141 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002142 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002143
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002144 wl_list_init(&window->widget_list);
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002145 window->widget = window_add_widget(window, window);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002146
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002147 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002148#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002149 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2150 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002151#else
2152 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2153#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002154 else
2155 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002156
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002157 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002158 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002159
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002160 if (window->shell_surface) {
2161 wl_shell_surface_set_user_data(window->shell_surface, window);
2162 wl_shell_surface_add_listener(window->shell_surface,
2163 &shell_surface_listener, window);
2164 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002165
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002166 return window;
2167}
2168
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002169struct window *
2170window_create(struct display *display, int32_t width, int32_t height)
2171{
2172 struct window *window;
2173
2174 window = window_create_internal(display, NULL, width, height);
2175 if (!window)
2176 return NULL;
2177
2178 return window;
2179}
2180
2181struct window *
2182window_create_transient(struct display *display, struct window *parent,
2183 int32_t x, int32_t y, int32_t width, int32_t height)
2184{
2185 struct window *window;
2186
2187 window = window_create_internal(parent->display,
2188 parent, width, height);
2189 if (!window)
2190 return NULL;
2191
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002192 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002193 window->x = x;
2194 window->y = y;
2195
2196 return window;
2197}
2198
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002199static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002200menu_set_item(struct widget *widget, struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002201{
2202 int next;
2203
2204 next = (sy - 8) / 20;
2205 if (menu->current != next) {
2206 menu->current = next;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002207 widget_schedule_redraw(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002208 }
2209
2210 return POINTER_LEFT_PTR;
2211}
2212
2213static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002214menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002215 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002216 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002217{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002218 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002219
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002220 return menu_set_item(menu->widget, menu, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002221}
2222
2223static void
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002224menu_enter_handler(struct widget *widget,
2225 struct input *input, uint32_t time,
2226 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002227{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002228 menu_set_item(widget, data, y);
2229}
2230
2231static void
2232menu_leave_handler(struct widget *widget, struct input *input, void *data)
2233{
2234 menu_set_item(widget, data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002235}
2236
2237static void
2238menu_button_handler(struct window *window,
2239 struct input *input, uint32_t time,
2240 int button, int state, void *data)
2241
2242{
2243 struct menu *menu = data;
2244
2245 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002246 if (state == 0 && time - menu->time > 500) {
2247 menu->func(window->parent,
2248 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002249 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002250 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002251}
2252
2253static void
2254menu_redraw_handler(struct window *window, void *data)
2255{
2256 cairo_t *cr;
2257 const int32_t r = 3, margin = 3;
2258 struct menu *menu = data;
2259 int32_t width, height, i;
2260
2261 width = 200;
2262 height = menu->count * 20 + margin * 2;
2263 window_set_child_size(window, width, height);
2264 window_create_surface(window);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002265 widget_set_allocation(menu->widget, 0, 0, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002266
2267 cr = cairo_create(window->cairo_surface);
2268 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2269 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2270 cairo_paint(cr);
2271
2272 width = window->allocation.width;
2273 height = window->allocation.height;
2274 rounded_rect(cr, 0, 0, width, height, r);
2275 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2276 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2277 cairo_fill(cr);
2278
2279 for (i = 0; i < menu->count; i++) {
2280 if (i == menu->current) {
2281 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2282 cairo_rectangle(cr, margin, i * 20 + margin,
2283 width - 2 * margin, 20);
2284 cairo_fill(cr);
2285 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2286 cairo_move_to(cr, 10, i * 20 + 16);
2287 cairo_show_text(cr, menu->entries[i]);
2288 } else {
2289 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2290 cairo_move_to(cr, 10, i * 20 + 16);
2291 cairo_show_text(cr, menu->entries[i]);
2292 }
2293 }
2294
2295 cairo_destroy(cr);
2296 window_flush(window);
2297}
2298
2299struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002300window_create_menu(struct display *display,
2301 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002302 int32_t x, int32_t y,
2303 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002304{
2305 struct window *window;
2306 struct menu *menu;
2307
2308 menu = malloc(sizeof *menu);
2309 if (!menu)
2310 return NULL;
2311
2312 window = window_create_internal(parent->display, parent, 0, 0);
2313 if (!window)
2314 return NULL;
2315
2316 menu->window = window;
2317 menu->entries = entries;
2318 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002319 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002320 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002321 window->decoration = 0;
2322 window->type = TYPE_MENU;
2323 window->x = x;
2324 window->y = y;
2325
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002326 wl_shell_surface_set_popup(window->shell_surface,
2327 input->input_device, time,
2328 window->parent->shell_surface,
2329 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002330
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331 window_set_button_handler(window, menu_button_handler);
2332 window_set_redraw_handler(window, menu_redraw_handler);
2333 window_set_user_data(window, menu);
2334
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002335 menu->widget = window_add_widget(window, menu);
2336 widget_set_enter_handler(menu->widget, menu_enter_handler);
2337 widget_set_leave_handler(menu->widget, menu_leave_handler);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002338 widget_set_motion_handler(menu->widget, menu_motion_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002339
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002340 return window;
2341}
2342
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002343void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002344window_set_buffer_type(struct window *window, enum window_buffer_type type)
2345{
2346 window->buffer_type = type;
2347}
2348
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002349
2350static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002351display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002352 struct wl_output *wl_output,
2353 int x, int y,
2354 int physical_width,
2355 int physical_height,
2356 int subpixel,
2357 const char *make,
2358 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002359{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002360 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002361
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002362 output->allocation.x = x;
2363 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002364}
2365
2366static void
2367display_handle_mode(void *data,
2368 struct wl_output *wl_output,
2369 uint32_t flags,
2370 int width,
2371 int height,
2372 int refresh)
2373{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002374 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002375 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002376
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002377 if (flags & WL_OUTPUT_MODE_CURRENT) {
2378 output->allocation.width = width;
2379 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002380 if (display->output_configure_handler)
2381 (*display->output_configure_handler)(
2382 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002383 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002384}
2385
2386static const struct wl_output_listener output_listener = {
2387 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002388 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002389};
2390
2391static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002392display_add_output(struct display *d, uint32_t id)
2393{
2394 struct output *output;
2395
2396 output = malloc(sizeof *output);
2397 if (output == NULL)
2398 return;
2399
2400 memset(output, 0, sizeof *output);
2401 output->display = d;
2402 output->output =
2403 wl_display_bind(d->display, id, &wl_output_interface);
2404 wl_list_insert(d->output_list.prev, &output->link);
2405
2406 wl_output_add_listener(output->output, &output_listener, output);
2407}
2408
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002409static void
2410output_destroy(struct output *output)
2411{
2412 if (output->destroy_handler)
2413 (*output->destroy_handler)(output, output->user_data);
2414
2415 wl_output_destroy(output->output);
2416 wl_list_remove(&output->link);
2417 free(output);
2418}
2419
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002420void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002421display_set_output_configure_handler(struct display *display,
2422 display_output_handler_t handler)
2423{
2424 struct output *output;
2425
2426 display->output_configure_handler = handler;
2427 if (!handler)
2428 return;
2429
2430 wl_list_for_each(output, &display->output_list, link)
2431 (*display->output_configure_handler)(output,
2432 display->user_data);
2433}
2434
2435void
2436output_set_user_data(struct output *output, void *data)
2437{
2438 output->user_data = data;
2439}
2440
2441void *
2442output_get_user_data(struct output *output)
2443{
2444 return output->user_data;
2445}
2446
2447void
2448output_set_destroy_handler(struct output *output,
2449 display_output_handler_t handler)
2450{
2451 output->destroy_handler = handler;
2452 /* FIXME: implement this, once we have way to remove outputs */
2453}
2454
2455void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002456output_get_allocation(struct output *output, struct rectangle *allocation)
2457{
2458 *allocation = output->allocation;
2459}
2460
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002461struct wl_output *
2462output_get_wl_output(struct output *output)
2463{
2464 return output->output;
2465}
2466
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002467static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002468display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002469{
2470 struct input *input;
2471
2472 input = malloc(sizeof *input);
2473 if (input == NULL)
2474 return;
2475
2476 memset(input, 0, sizeof *input);
2477 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002478 input->input_device =
2479 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002480 input->pointer_focus = NULL;
2481 input->keyboard_focus = NULL;
2482 wl_list_insert(d->input_list.prev, &input->link);
2483
2484 wl_input_device_add_listener(input->input_device,
2485 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002486 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002487
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002488 input->data_device =
2489 wl_data_device_manager_get_data_device(d->data_device_manager,
2490 input->input_device);
2491 wl_data_device_add_listener(input->data_device,
2492 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002493}
2494
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002495static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002496input_destroy(struct input *input)
2497{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002498 input_remove_keyboard_focus(input);
2499 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002500
2501 if (input->drag_offer)
2502 data_offer_destroy(input->drag_offer);
2503
2504 if (input->selection_offer)
2505 data_offer_destroy(input->selection_offer);
2506
2507 wl_data_device_destroy(input->data_device);
2508 wl_list_remove(&input->link);
2509 wl_input_device_destroy(input->input_device);
2510 free(input);
2511}
2512
2513static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002514display_handle_global(struct wl_display *display, uint32_t id,
2515 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002516{
2517 struct display *d = data;
2518
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002519 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002520 d->compositor =
2521 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002522 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002523 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002524 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002525 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002526 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002527 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002528 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002529 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002530 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2531 d->data_device_manager =
2532 wl_display_bind(display, id,
2533 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002534 }
2535}
2536
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002537static void
2538display_render_frame(struct display *d)
2539{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002540 int radius = 8;
2541 cairo_t *cr;
2542
2543 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2544 cr = cairo_create(d->shadow);
2545 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2546 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2547 rounded_rect(cr, 16, 16, 112, 112, radius);
2548 cairo_fill(cr);
2549 cairo_destroy(cr);
2550 blur_surface(d->shadow, 64);
2551
2552 d->active_frame =
2553 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2554 cr = cairo_create(d->active_frame);
2555 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2556 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2557 rounded_rect(cr, 16, 16, 112, 112, radius);
2558 cairo_fill(cr);
2559 cairo_destroy(cr);
2560
2561 d->inactive_frame =
2562 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2563 cr = cairo_create(d->inactive_frame);
2564 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2565 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2566 rounded_rect(cr, 16, 16, 112, 112, radius);
2567 cairo_fill(cr);
2568 cairo_destroy(cr);
2569}
2570
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002571static void
2572init_xkb(struct display *d)
2573{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002574 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002575
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002576 names.rules = "evdev";
2577 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002578 names.layout = option_xkb_layout;
2579 names.variant = option_xkb_variant;
2580 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002581
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002582 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002583 if (!d->xkb) {
2584 fprintf(stderr, "Failed to compile keymap\n");
2585 exit(1);
2586 }
2587}
2588
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002589static void
2590fini_xkb(struct display *display)
2591{
2592 xkb_free_keymap(display->xkb);
2593}
2594
Yuval Fledel45568f62010-12-06 09:18:12 -05002595static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002596init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002597{
2598 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002599 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002600
2601 static const EGLint premul_argb_cfg_attribs[] = {
2602 EGL_SURFACE_TYPE,
2603 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2604 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002605 EGL_RED_SIZE, 1,
2606 EGL_GREEN_SIZE, 1,
2607 EGL_BLUE_SIZE, 1,
2608 EGL_ALPHA_SIZE, 1,
2609 EGL_DEPTH_SIZE, 1,
2610 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2611 EGL_NONE
2612 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002613
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002614 static const EGLint rgb_cfg_attribs[] = {
2615 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2616 EGL_RED_SIZE, 1,
2617 EGL_GREEN_SIZE, 1,
2618 EGL_BLUE_SIZE, 1,
2619 EGL_ALPHA_SIZE, 0,
2620 EGL_DEPTH_SIZE, 1,
2621 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2622 EGL_NONE
2623 };
2624
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002625 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002626 if (!eglInitialize(d->dpy, &major, &minor)) {
2627 fprintf(stderr, "failed to initialize display\n");
2628 return -1;
2629 }
2630
2631 if (!eglBindAPI(EGL_OPENGL_API)) {
2632 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2633 return -1;
2634 }
2635
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002636 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002637 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002638 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002639 return -1;
2640 }
2641
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002642 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2643 &d->rgb_config, 1, &n) || n != 1) {
2644 fprintf(stderr, "failed to choose rgb config\n");
2645 return -1;
2646 }
2647
Benjamin Franzke0c991632011-09-27 21:57:31 +02002648 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2649 if (d->rgb_ctx == NULL) {
2650 fprintf(stderr, "failed to create context\n");
2651 return -1;
2652 }
2653 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2654 EGL_NO_CONTEXT, NULL);
2655 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002656 fprintf(stderr, "failed to create context\n");
2657 return -1;
2658 }
2659
Benjamin Franzke0c991632011-09-27 21:57:31 +02002660 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002661 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002662 return -1;
2663 }
2664
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002665#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002666 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2667 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002668 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002669 return -1;
2670 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002671 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2672 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2673 fprintf(stderr, "failed to get cairo egl argb device\n");
2674 return -1;
2675 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002676#endif
2677
2678 return 0;
2679}
2680
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002681static void
2682fini_egl(struct display *display)
2683{
2684#ifdef HAVE_CAIRO_EGL
2685 cairo_device_destroy(display->argb_device);
2686 cairo_device_destroy(display->rgb_device);
2687#endif
2688
2689 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2690 EGL_NO_CONTEXT);
2691
2692 eglTerminate(display->dpy);
2693 eglReleaseThread();
2694}
2695
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002696static int
2697event_mask_update(uint32_t mask, void *data)
2698{
2699 struct display *d = data;
2700
2701 d->mask = mask;
2702
2703 return 0;
2704}
2705
2706static void
2707handle_display_data(struct task *task, uint32_t events)
2708{
2709 struct display *display =
2710 container_of(task, struct display, display_task);
2711
2712 wl_display_iterate(display->display, display->mask);
2713}
2714
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002715struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002716display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002717{
2718 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002719 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002720 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002721 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002722
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002723 g_type_init();
2724
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002725 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002726 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002727 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002728
2729 xkb_option_group = g_option_group_new("xkb",
2730 "Keyboard options",
2731 "Show all XKB options",
2732 NULL, NULL);
2733 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2734 g_option_context_add_group (context, xkb_option_group);
2735
2736 if (!g_option_context_parse(context, argc, argv, &error)) {
2737 fprintf(stderr, "option parsing failed: %s\n", error->message);
2738 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002739 }
2740
Tim Wiederhake748f6722011-01-23 23:25:25 +01002741 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002742
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002743 d = malloc(sizeof *d);
2744 if (d == NULL)
2745 return NULL;
2746
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002747 memset(d, 0, sizeof *d);
2748
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002749 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002750 if (d->display == NULL) {
2751 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002752 return NULL;
2753 }
2754
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002755 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2756 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2757 d->display_task.run = handle_display_data;
2758 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2759
2760 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002761 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002762 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002763
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002764 /* Set up listener so we'll catch all events. */
2765 wl_display_add_global_listener(d->display,
2766 display_handle_global, d);
2767
2768 /* Process connection events. */
2769 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002770 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002771 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002772
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002773 d->image_target_texture_2d =
2774 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2775 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2776 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2777
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002778 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002779
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002780 display_render_frame(d);
2781
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002782 wl_list_init(&d->window_list);
2783
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002784 init_xkb(d);
2785
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002786 return d;
2787}
2788
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002789static void
2790display_destroy_outputs(struct display *display)
2791{
2792 struct output *tmp;
2793 struct output *output;
2794
2795 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2796 output_destroy(output);
2797}
2798
Pekka Paalanene1207c72011-12-16 12:02:09 +02002799static void
2800display_destroy_inputs(struct display *display)
2801{
2802 struct input *tmp;
2803 struct input *input;
2804
2805 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2806 input_destroy(input);
2807}
2808
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002809void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002810display_destroy(struct display *display)
2811{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002812 if (!wl_list_empty(&display->window_list))
2813 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2814
2815 if (!wl_list_empty(&display->deferred_list))
2816 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2817
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002818 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002819 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002820
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002821 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002822
2823 cairo_surface_destroy(display->active_frame);
2824 cairo_surface_destroy(display->inactive_frame);
2825 cairo_surface_destroy(display->shadow);
2826 destroy_pointer_surfaces(display);
2827
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002828 fini_egl(display);
2829
Pekka Paalanenc2052982011-12-16 11:41:32 +02002830 if (display->shell)
2831 wl_shell_destroy(display->shell);
2832
2833 if (display->shm)
2834 wl_shm_destroy(display->shm);
2835
2836 if (display->data_device_manager)
2837 wl_data_device_manager_destroy(display->data_device_manager);
2838
2839 wl_compositor_destroy(display->compositor);
2840
2841 close(display->epoll_fd);
2842
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002843 wl_display_flush(display->display);
2844 wl_display_destroy(display->display);
2845 free(display);
2846}
2847
2848void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002849display_set_user_data(struct display *display, void *data)
2850{
2851 display->user_data = data;
2852}
2853
2854void *
2855display_get_user_data(struct display *display)
2856{
2857 return display->user_data;
2858}
2859
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002860struct wl_display *
2861display_get_display(struct display *display)
2862{
2863 return display->display;
2864}
2865
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002866struct output *
2867display_get_output(struct display *display)
2868{
2869 return container_of(display->output_list.next, struct output, link);
2870}
2871
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002872struct wl_compositor *
2873display_get_compositor(struct display *display)
2874{
2875 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002876}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002877
2878EGLDisplay
2879display_get_egl_display(struct display *d)
2880{
2881 return d->dpy;
2882}
2883
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002884struct wl_data_source *
2885display_create_data_source(struct display *display)
2886{
2887 return wl_data_device_manager_create_data_source(display->data_device_manager);
2888}
2889
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002890EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002891display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002892{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002893 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002894}
2895
Benjamin Franzke0c991632011-09-27 21:57:31 +02002896EGLConfig
2897display_get_argb_egl_config(struct display *d)
2898{
2899 return d->premultiplied_argb_config;
2900}
2901
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002902struct wl_shell *
2903display_get_shell(struct display *display)
2904{
2905 return display->shell;
2906}
2907
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002908int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002909display_acquire_window_surface(struct display *display,
2910 struct window *window,
2911 EGLContext ctx)
2912{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002913#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002914 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002915 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002916
2917 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002918 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002919 device = cairo_surface_get_device(window->cairo_surface);
2920 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002921 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002922
Benjamin Franzke0c991632011-09-27 21:57:31 +02002923 if (!ctx) {
2924 if (device == display->rgb_device)
2925 ctx = display->rgb_ctx;
2926 else if (device == display->argb_device)
2927 ctx = display->argb_ctx;
2928 else
2929 assert(0);
2930 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002931
2932 data = cairo_surface_get_user_data(window->cairo_surface,
2933 &surface_data_key);
2934
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002935 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002936 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002937 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2938 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002939
2940 return 0;
2941#else
2942 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002943#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002944}
2945
2946void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002947display_release_window_surface(struct display *display,
2948 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002949{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002950#ifdef HAVE_CAIRO_EGL
2951 cairo_device_t *device;
2952
2953 device = cairo_surface_get_device(window->cairo_surface);
2954 if (!device)
2955 return;
2956
2957 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002958 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002959 cairo_device_release(device);
2960#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002961}
2962
2963void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002964display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002965{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002966 wl_list_insert(&display->deferred_list, &task->link);
2967}
2968
2969void
2970display_watch_fd(struct display *display,
2971 int fd, uint32_t events, struct task *task)
2972{
2973 struct epoll_event ep;
2974
2975 ep.events = events;
2976 ep.data.ptr = task;
2977 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2978}
2979
2980void
2981display_run(struct display *display)
2982{
2983 struct task *task;
2984 struct epoll_event ep[16];
2985 int i, count;
2986
Pekka Paalanen826d7952011-12-15 10:14:07 +02002987 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002988 while (1) {
2989 while (display->mask & WL_DISPLAY_WRITABLE)
2990 wl_display_iterate(display->display,
2991 WL_DISPLAY_WRITABLE);
2992
Pekka Paalanen826d7952011-12-15 10:14:07 +02002993 if (!display->running)
2994 break;
2995
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002996 count = epoll_wait(display->epoll_fd,
2997 ep, ARRAY_LENGTH(ep), -1);
2998 for (i = 0; i < count; i++) {
2999 task = ep[i].data.ptr;
3000 task->run(task, ep[i].events);
3001 }
3002
3003 while (!wl_list_empty(&display->deferred_list)) {
3004 task = container_of(display->deferred_list.next,
3005 struct task, link);
3006 wl_list_remove(&task->link);
3007 task->run(task, 0);
3008 }
3009 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003010}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003011
3012void
3013display_exit(struct display *display)
3014{
3015 display->running = 0;
3016}