blob: 854c8adc9ac53d346323999cd485ac4797f9d6ec [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040052#include <X11/extensions/XKBcommon.h>
53
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050054#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040056#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050058#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050059
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050061 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040066 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040067 EGLConfig rgb_config;
Benjamin Franzke4b87a132011-09-01 10:36:16 +020068 EGLConfig premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020069 EGLContext rgb_ctx;
70 EGLContext argb_ctx;
71 cairo_device_t *rgb_device;
72 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040073
74 int display_fd;
75 uint32_t mask;
76 struct task display_task;
77
78 int epoll_fd;
79 struct wl_list deferred_list;
80
Pekka Paalanen826d7952011-12-15 10:14:07 +020081 int running;
82
Kristian Høgsberg478d9262010-06-08 20:34:11 -040083 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040084 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050085 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040086 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040087 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040088 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040089
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050090 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020093
94 display_output_handler_t output_configure_handler;
95
96 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050097};
98
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400101 TYPE_FULLSCREEN,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500103 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104 TYPE_CUSTOM
105};
106
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500107struct window {
108 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500109 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_shell_surface *shell_surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500112 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500113 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500119 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400121 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500127 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500128
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500129 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400130 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400132 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500133 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 window_motion_handler_t motion_handler;
Kristian Hø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øgsberg012a0072010-10-26 00:02:20 -0400922void
923window_create_surface(struct window *window)
924{
925 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400926 uint32_t flags = 0;
927
928 if (!window->transparent)
929 flags = SURFACE_OPAQUE;
930
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500932#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100933 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
934 if (window->cairo_surface) {
935 window_resize_cairo_window_surface(window);
936 return;
937 }
938 surface = display_create_surface(window->display,
939 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400940 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100941 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500942 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400943 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400946 break;
947#endif
948 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400950 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400951 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800952 default:
953 surface = NULL;
954 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400955 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400956
957 window_set_surface(window, surface);
958 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400959}
960
961static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500963{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500964 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500965 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400966 cairo_surface_t *frame;
967 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500968
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400969 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400970
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400971 width = window->allocation.width;
972 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500973
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500974 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500975
Kristian Høgsberg09531622010-06-14 23:22:15 -0400976 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400977 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400978 cairo_paint(cr);
979
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400980 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400981 tile_mask(cr, window->display->shadow,
982 shadow_dx, shadow_dy, width, height,
983 window->margin + 10 - shadow_dx,
984 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500985
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400986 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400987 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400988 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400989 frame = window->display->inactive_frame;
990
991 tile_source(cr, frame, 0, 0, width, height,
992 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500993
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500994 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
995 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500996 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400997 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400998 if (window->keyboard_device)
999 cairo_set_source_rgb(cr, 0, 0, 0);
1000 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -04001001 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001002 cairo_show_text(cr, window->title);
1003
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001004 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001005}
1006
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001007void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001008window_destroy(struct window *window)
1009{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001010 struct display *display = window->display;
1011 struct input *input;
1012
1013 if (window->redraw_scheduled)
1014 wl_list_remove(&window->redraw_task.link);
1015
1016 wl_list_for_each(input, &display->input_list, link) {
1017 if (input->pointer_focus == window)
1018 input->pointer_focus = NULL;
1019 if (input->keyboard_focus == window)
1020 input->keyboard_focus = NULL;
1021 }
1022
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001023 if (window->shell_surface)
1024 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001025 wl_surface_destroy(window->surface);
1026 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001027
1028 if (window->cairo_surface != NULL)
1029 cairo_surface_destroy(window->cairo_surface);
1030 if (window->pending_surface != NULL)
1031 cairo_surface_destroy(window->pending_surface);
1032
1033 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001034 free(window);
1035}
1036
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001037static struct widget *
1038window_find_widget(struct window *window, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001039{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001040 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001042 wl_list_for_each(widget, &window->widget_list, link) {
1043 if (widget->allocation.x <= x &&
1044 x < widget->allocation.x + widget->allocation.width &&
1045 widget->allocation.y <= y &&
1046 y < widget->allocation.y + widget->allocation.height) {
1047 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048 }
1049 }
1050
1051 return NULL;
1052}
1053
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001054struct widget *
1055window_add_widget(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001056{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001057 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001059 widget = malloc(sizeof *widget);
1060 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001061 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001062 widget->user_data = data;
1063 wl_list_insert(window->widget_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066}
1067
1068void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069window_for_each_widget(struct window *window, widget_func_t func, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 wl_list_for_each(widget, &window->widget_list, link)
1074 func(widget, data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001075}
1076
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001077struct widget *
1078window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001079{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001080 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001081}
1082
1083void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087}
1088
1089void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090widget_set_allocation(struct widget *widget,
1091 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001092{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001093 widget->allocation.x = x;
1094 widget->allocation.y = y;
1095 widget->allocation.width = width;
1096 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097}
1098
1099void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001100widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001101{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001103}
1104
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001105void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001106widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001107{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001108 widget->enter_handler = handler;
1109}
1110
1111void
1112widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1113{
1114 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001115}
1116
1117void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001118widget_set_motion_handler(struct widget *widget,
1119 widget_motion_handler_t handler)
1120{
1121 widget->motion_handler = handler;
1122}
1123
1124void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001125widget_schedule_redraw(struct widget *widget)
1126{
1127 window_schedule_redraw(widget->window);
1128}
1129
1130void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001131window_draw(struct window *window)
1132{
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05001133 if (!window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001134 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001135 else
1136 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -05001137}
1138
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001139cairo_surface_t *
1140window_get_surface(struct window *window)
1141{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001142 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001143}
1144
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001145struct wl_surface *
1146window_get_wl_surface(struct window *window)
1147{
1148 return window->surface;
1149}
1150
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001151struct wl_shell_surface *
1152window_get_wl_shell_surface(struct window *window)
1153{
1154 return window->shell_surface;
1155}
1156
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001157static int
1158get_pointer_location(struct window *window, int32_t x, int32_t y)
1159{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001160 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001161 const int grip_size = 8;
1162
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001163 if (!window->decoration)
1164 return WINDOW_CLIENT_AREA;
1165
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001167 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168 else if (window->margin <= x && x < window->margin + grip_size)
1169 hlocation = WINDOW_RESIZING_LEFT;
1170 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001171 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001172 else if (x < window->allocation.width - window->margin)
1173 hlocation = WINDOW_RESIZING_RIGHT;
1174 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001175 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001176
1177 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001178 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001179 else if (window->margin <= y && y < window->margin + grip_size)
1180 vlocation = WINDOW_RESIZING_TOP;
1181 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001182 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001183 else if (y < window->allocation.height - window->margin)
1184 vlocation = WINDOW_RESIZING_BOTTOM;
1185 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001186 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001187
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001188 location = vlocation | hlocation;
1189 if (location & WINDOW_EXTERIOR)
1190 location = WINDOW_EXTERIOR;
1191 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1192 location = WINDOW_TITLEBAR;
1193 else if (location == WINDOW_INTERIOR)
1194 location = WINDOW_CLIENT_AREA;
1195
1196 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001197}
1198
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001199static int
1200input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001201{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001202 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203
1204 location = get_pointer_location(input->pointer_focus,
1205 input->sx, input->sy);
1206 switch (location) {
1207 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001208 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001210 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001211 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001212 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001214 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001215 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001216 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001217 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001218 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001219 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001220 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001221 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001222 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 case WINDOW_EXTERIOR:
1224 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001225 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001226 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001227 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001228 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001229}
1230
1231void
1232input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1233{
1234 struct display *display = input->display;
1235 struct wl_buffer *buffer;
1236 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001237
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001238 if (pointer == input->current_pointer_image)
1239 return;
1240
1241 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001243
1244 if (!surface)
1245 return;
1246
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001248 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249 pointer_images[pointer].hotspot_x,
1250 pointer_images[pointer].hotspot_y);
1251}
1252
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001253static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001254window_set_focus_widget(struct window *window, struct widget *focus,
1255 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001256{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001257 struct widget *old;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001259 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001260 return;
1261
Kristian Høgsbergee143232012-01-09 08:42:24 -05001262 old = window->focus_widget;
1263 if (old) {
1264 if (old->leave_handler)
1265 old->leave_handler(old, input, old->user_data);
1266 window->focus_widget = NULL;
1267 }
1268
1269 if (focus) {
1270 if (focus->enter_handler)
1271 focus->enter_handler(focus, input, time,
1272 x, y, focus->user_data);
1273 window->focus_widget = focus;
1274 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001275}
1276
1277static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001278input_handle_motion(void *data, struct wl_input_device *input_device,
1279 uint32_t time,
1280 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001281{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001282 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001283 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001284 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001285 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001286
1287 input->x = x;
1288 input->y = y;
1289 input->sx = sx;
1290 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001292 if (!window->focus_widget || !window->widget_grab_button) {
1293 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001294 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001295 }
1296
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001297 widget = window->focus_widget;
1298 if (widget && widget->motion_handler)
1299 pointer = widget->motion_handler(widget, input, time, sx, sy,
1300 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001301 if (window->motion_handler)
1302 pointer = (*window->motion_handler)(window, input, time,
1303 x, y, sx, sy,
1304 window->user_data);
1305
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001307 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001308}
1309
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001310static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001311window_menu_func(struct window *window, int index, void *data)
1312{
1313 switch (index) {
1314 case 0: /* close */
1315 if (window->close_handler)
1316 window->close_handler(window->parent,
1317 window->user_data);
1318 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001319 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001320 break;
1321 case 1: /* fullscreen */
1322 /* we don't have a way to get out of fullscreen for now */
1323 window_set_fullscreen(window, 1);
1324 break;
1325 case 2: /* rotate */
1326 case 3: /* scale */
1327 break;
1328 }
1329}
1330
1331
1332static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001333input_handle_button(void *data,
1334 struct wl_input_device *input_device,
1335 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001336{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001337 struct input *input = data;
1338 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001339 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001340 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001341 int32_t x, y;
1342 static const char *entries[] = {
1343 "Close", "Fullscreen", "Rotate", "Scale"
1344 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001345
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001346 if (window->focus_widget && window->widget_grab_button == 0 && state)
1347 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001348
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001349 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001350
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001351 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001352 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001353 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001354 if (!window->shell_surface)
1355 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001356 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001357 wl_shell_surface_move(window->shell_surface,
1358 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001359 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001360 case WINDOW_RESIZING_TOP:
1361 case WINDOW_RESIZING_BOTTOM:
1362 case WINDOW_RESIZING_LEFT:
1363 case WINDOW_RESIZING_RIGHT:
1364 case WINDOW_RESIZING_TOP_LEFT:
1365 case WINDOW_RESIZING_TOP_RIGHT:
1366 case WINDOW_RESIZING_BOTTOM_LEFT:
1367 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001368 if (!window->shell_surface)
1369 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001370 wl_shell_surface_resize(window->shell_surface,
1371 input_device, time,
1372 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001373 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001374 case WINDOW_CLIENT_AREA:
1375 if (window->button_handler)
1376 (*window->button_handler)(window,
1377 input, time,
1378 button, state,
1379 window->user_data);
1380 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001381 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001382 } else if (button == BTN_RIGHT && state == 1) {
1383 switch (location) {
1384 default:
1385 input_get_position(input, &x, &y);
1386 window->menu = window_create_menu(window->display,
1387 input, time,
1388 window,
1389 x - 10, y - 10,
1390 window_menu_func,
1391 entries, 4);
1392 window_schedule_redraw(window->menu);
1393 break;
1394 case WINDOW_CLIENT_AREA:
1395 if (window->button_handler)
1396 (*window->button_handler)(window,
1397 input, time,
1398 button, state,
1399 window->user_data);
1400 break;
1401 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001402 } else {
1403 if (window->button_handler)
1404 (*window->button_handler)(window,
1405 input, time,
1406 button, state,
1407 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001408 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001409
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001410 if (window->focus_widget &&
1411 window->widget_grab_button == button && !state) {
1412 window->widget_grab_button = 0;
1413 widget = window_find_widget(window, input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001414 window_set_focus_widget(window, widget, input, time,
1415 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001416 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001417}
1418
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001419static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001420input_handle_key(void *data, struct wl_input_device *input_device,
1421 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001422{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001423 struct input *input = data;
1424 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001425 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001426 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001427
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001428 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001429 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001430 return;
1431
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001432 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001433 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001434 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1435 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001436
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001437 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1438
1439 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001440 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001441 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001442 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001443
1444 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001445 (*window->key_handler)(window, input, time, key, sym, state,
1446 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001447}
1448
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001449static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001450input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001451{
1452 struct window *window = input->pointer_focus;
1453
1454 if (!window)
1455 return;
1456
Kristian Høgsbergee143232012-01-09 08:42:24 -05001457 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001458
Pekka Paalanene1207c72011-12-16 12:02:09 +02001459 input->pointer_focus = NULL;
1460 input->current_pointer_image = POINTER_UNSET;
1461}
1462
1463static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001464input_handle_pointer_focus(void *data,
1465 struct wl_input_device *input_device,
1466 uint32_t time, struct wl_surface *surface,
1467 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001468{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001469 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001470 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001471 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001472 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001473
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001474 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001475 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001476 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001477
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001478 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001479 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001480 window = input->pointer_focus;
1481
Kristian Høgsberg59826582011-01-20 11:56:57 -05001482 input->x = x;
1483 input->y = y;
1484 input->sx = sx;
1485 input->sy = sy;
1486
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001487 pointer = POINTER_LEFT_PTR;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001488 widget = window_find_widget(window, x, y);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001489 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001490
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001491 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001492 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001493 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001494}
1495
1496static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001497input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001498{
1499 struct window *window = input->keyboard_focus;
1500
1501 if (!window)
1502 return;
1503
1504 window->keyboard_device = NULL;
1505 if (window->keyboard_focus_handler)
1506 (*window->keyboard_focus_handler)(window, NULL,
1507 window->user_data);
1508
1509 input->keyboard_focus = NULL;
1510}
1511
1512static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001513input_handle_keyboard_focus(void *data,
1514 struct wl_input_device *input_device,
1515 uint32_t time,
1516 struct wl_surface *surface,
1517 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001518{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001519 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001520 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001521 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001522 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001523
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001524 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001525
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001526 if (surface)
1527 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001528
1529 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001530 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001531 for (k = keys->data; k < end; k++)
1532 input->modifiers |= d->xkb->map->modmap[*k];
1533
1534 window = input->keyboard_focus;
1535 if (window) {
1536 window->keyboard_device = input;
1537 if (window->keyboard_focus_handler)
1538 (*window->keyboard_focus_handler)(window,
1539 window->keyboard_device,
1540 window->user_data);
1541 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001542}
1543
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001544static void
1545input_handle_touch_down(void *data,
1546 struct wl_input_device *wl_input_device,
1547 uint32_t time, struct wl_surface *surface,
1548 int32_t id, int32_t x, int32_t y)
1549{
1550}
1551
1552static void
1553input_handle_touch_up(void *data,
1554 struct wl_input_device *wl_input_device,
1555 uint32_t time, int32_t id)
1556{
1557}
1558
1559static void
1560input_handle_touch_motion(void *data,
1561 struct wl_input_device *wl_input_device,
1562 uint32_t time, int32_t id, int32_t x, int32_t y)
1563{
1564}
1565
1566static void
1567input_handle_touch_frame(void *data,
1568 struct wl_input_device *wl_input_device)
1569{
1570}
1571
1572static void
1573input_handle_touch_cancel(void *data,
1574 struct wl_input_device *wl_input_device)
1575{
1576}
1577
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001578static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001579 input_handle_motion,
1580 input_handle_button,
1581 input_handle_key,
1582 input_handle_pointer_focus,
1583 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001584 input_handle_touch_down,
1585 input_handle_touch_up,
1586 input_handle_touch_motion,
1587 input_handle_touch_frame,
1588 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001589};
1590
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001591void
1592input_get_position(struct input *input, int32_t *x, int32_t *y)
1593{
1594 *x = input->sx;
1595 *y = input->sy;
1596}
1597
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001598struct wl_input_device *
1599input_get_input_device(struct input *input)
1600{
1601 return input->input_device;
1602}
1603
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001604uint32_t
1605input_get_modifiers(struct input *input)
1606{
1607 return input->modifiers;
1608}
1609
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001610struct data_offer {
1611 struct wl_data_offer *offer;
1612 struct input *input;
1613 struct wl_array types;
1614 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001615
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001616 struct task io_task;
1617 int fd;
1618 data_func_t func;
1619 int32_t x, y;
1620 void *user_data;
1621};
1622
1623static void
1624data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1625{
1626 struct data_offer *offer = data;
1627 char **p;
1628
1629 p = wl_array_add(&offer->types, sizeof *p);
1630 *p = strdup(type);
1631}
1632
1633static const struct wl_data_offer_listener data_offer_listener = {
1634 data_offer_offer,
1635};
1636
1637static void
1638data_offer_destroy(struct data_offer *offer)
1639{
1640 char **p;
1641
1642 offer->refcount--;
1643 if (offer->refcount == 0) {
1644 wl_data_offer_destroy(offer->offer);
1645 for (p = offer->types.data; *p; p++)
1646 free(*p);
1647 wl_array_release(&offer->types);
1648 free(offer);
1649 }
1650}
1651
1652static void
1653data_device_data_offer(void *data,
1654 struct wl_data_device *data_device, uint32_t id)
1655{
1656 struct data_offer *offer;
1657
1658 offer = malloc(sizeof *offer);
1659
1660 wl_array_init(&offer->types);
1661 offer->refcount = 1;
1662 offer->input = data;
1663
1664 /* FIXME: Generate typesafe wrappers for this */
1665 offer->offer = (struct wl_data_offer *)
1666 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1667 id, &wl_data_offer_interface);
1668
1669 wl_data_offer_add_listener(offer->offer,
1670 &data_offer_listener, offer);
1671}
1672
1673static void
1674data_device_enter(void *data, struct wl_data_device *data_device,
1675 uint32_t time, struct wl_surface *surface,
1676 int32_t x, int32_t y, struct wl_data_offer *offer)
1677{
1678 struct input *input = data;
1679 struct window *window;
1680 char **p;
1681
1682 input->drag_offer = wl_data_offer_get_user_data(offer);
1683 window = wl_surface_get_user_data(surface);
1684 input->pointer_focus = window;
1685
1686 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1687 *p = NULL;
1688
1689 window = input->pointer_focus;
1690 if (window->data_handler)
1691 window->data_handler(window, input, time, x, y,
1692 input->drag_offer->types.data,
1693 window->user_data);
1694}
1695
1696static void
1697data_device_leave(void *data, struct wl_data_device *data_device)
1698{
1699 struct input *input = data;
1700
1701 data_offer_destroy(input->drag_offer);
1702 input->drag_offer = NULL;
1703}
1704
1705static void
1706data_device_motion(void *data, struct wl_data_device *data_device,
1707 uint32_t time, int32_t x, int32_t y)
1708{
1709 struct input *input = data;
1710 struct window *window = input->pointer_focus;
1711
1712 input->sx = x;
1713 input->sy = y;
1714
1715 if (window->data_handler)
1716 window->data_handler(window, input, time, x, y,
1717 input->drag_offer->types.data,
1718 window->user_data);
1719}
1720
1721static void
1722data_device_drop(void *data, struct wl_data_device *data_device)
1723{
1724 struct input *input = data;
1725 struct window *window = input->pointer_focus;
1726
1727 if (window->drop_handler)
1728 window->drop_handler(window, input,
1729 input->sx, input->sy, window->user_data);
1730}
1731
1732static void
1733data_device_selection(void *data,
1734 struct wl_data_device *wl_data_device,
1735 struct wl_data_offer *offer)
1736{
1737 struct input *input = data;
1738 char **p;
1739
1740 if (input->selection_offer)
1741 data_offer_destroy(input->selection_offer);
1742
1743 input->selection_offer = wl_data_offer_get_user_data(offer);
1744 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1745 *p = NULL;
1746}
1747
1748static const struct wl_data_device_listener data_device_listener = {
1749 data_device_data_offer,
1750 data_device_enter,
1751 data_device_leave,
1752 data_device_motion,
1753 data_device_drop,
1754 data_device_selection
1755};
1756
1757struct wl_data_device *
1758input_get_data_device(struct input *input)
1759{
1760 return input->data_device;
1761}
1762
1763void
1764input_set_selection(struct input *input,
1765 struct wl_data_source *source, uint32_t time)
1766{
1767 wl_data_device_set_selection(input->data_device, source, time);
1768}
1769
1770void
1771input_accept(struct input *input, uint32_t time, const char *type)
1772{
1773 wl_data_offer_accept(input->drag_offer->offer, time, type);
1774}
1775
1776static void
1777offer_io_func(struct task *task, uint32_t events)
1778{
1779 struct data_offer *offer =
1780 container_of(task, struct data_offer, io_task);
1781 unsigned int len;
1782 char buffer[4096];
1783
1784 len = read(offer->fd, buffer, sizeof buffer);
1785 offer->func(buffer, len,
1786 offer->x, offer->y, offer->user_data);
1787
1788 if (len == 0) {
1789 close(offer->fd);
1790 data_offer_destroy(offer);
1791 }
1792}
1793
1794static void
1795data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1796 data_func_t func, void *user_data)
1797{
1798 int p[2];
1799
1800 pipe2(p, O_CLOEXEC);
1801 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1802 close(p[1]);
1803
1804 offer->io_task.run = offer_io_func;
1805 offer->fd = p[0];
1806 offer->func = func;
1807 offer->refcount++;
1808 offer->user_data = user_data;
1809
1810 display_watch_fd(offer->input->display,
1811 offer->fd, EPOLLIN, &offer->io_task);
1812}
1813
1814void
1815input_receive_drag_data(struct input *input, const char *mime_type,
1816 data_func_t func, void *data)
1817{
1818 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1819 input->drag_offer->x = input->sx;
1820 input->drag_offer->y = input->sy;
1821}
1822
1823int
1824input_receive_selection_data(struct input *input, const char *mime_type,
1825 data_func_t func, void *data)
1826{
1827 char **p;
1828
1829 if (input->selection_offer == NULL)
1830 return -1;
1831
1832 for (p = input->selection_offer->types.data; *p; p++)
1833 if (strcmp(mime_type, *p) == 0)
1834 break;
1835
1836 if (*p == NULL)
1837 return -1;
1838
1839 data_offer_receive_data(input->selection_offer,
1840 mime_type, func, data);
1841 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001842}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001843
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001844int
1845input_receive_selection_data_to_fd(struct input *input,
1846 const char *mime_type, int fd)
1847{
1848 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1849
1850 return 0;
1851}
1852
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001853void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001854window_move(struct window *window, struct input *input, uint32_t time)
1855{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001856 if (!window->shell_surface)
1857 return;
1858
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001859 wl_shell_surface_move(window->shell_surface,
1860 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001861}
1862
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001863static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001864handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001865 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001866 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001867{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001868 struct window *window = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001869 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001870
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001871 /* FIXME: this is probably the wrong place to check for width
1872 * or height <= 0, but it prevents the compositor from crashing
1873 */
1874 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001875 return;
1876
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001877 window->resize_edges = edges;
1878
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001879 if (window->resize_handler) {
1880 child_width = width - 20 - window->margin * 2;
1881 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001882
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001883 (*window->resize_handler)(window,
1884 child_width, child_height,
1885 window->user_data);
1886 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001887 window->allocation.width = width;
1888 window->allocation.height = height;
1889
1890 if (window->redraw_handler)
1891 window_schedule_redraw(window);
1892 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001893}
1894
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001895static void
1896handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1897{
1898 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001899 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001900 /* FIXME: Need more context in this event, at least the input
1901 * device. Or just use wl_callback. */
1902
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001903 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001904 window_destroy(window);
1905}
1906
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001907static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001908 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001909 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001910};
1911
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001912void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001913window_get_allocation(struct window *window,
1914 struct rectangle *allocation)
1915{
1916 *allocation = window->allocation;
1917}
1918
1919void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001920window_get_child_allocation(struct window *window,
1921 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001922{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001923 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001924 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001925 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001926 allocation->x = window->margin + 10;
1927 allocation->y = window->margin + 50;
1928 allocation->width =
1929 window->allocation.width - 20 - window->margin * 2;
1930 allocation->height =
1931 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001932 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001933}
1934
1935void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001936window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001937{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001938 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001939 window->allocation.x = 20 + window->margin;
1940 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001941 window->allocation.width = width + 20 + window->margin * 2;
1942 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001943 } else {
1944 window->allocation.x = 0;
1945 window->allocation.y = 0;
1946 window->allocation.width = width;
1947 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001948 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001949}
1950
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001951static void
1952idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001953{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001954 struct window *window =
1955 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001956
1957 window->redraw_handler(window, window->user_data);
1958 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001959}
1960
1961void
1962window_schedule_redraw(struct window *window)
1963{
1964 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04001965 window->redraw_task.run = idle_redraw;
1966 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001967 window->redraw_scheduled = 1;
1968 }
1969}
1970
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001971void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001972window_set_custom(struct window *window)
1973{
1974 window->type = TYPE_CUSTOM;
1975}
1976
1977void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001978window_set_fullscreen(struct window *window, int fullscreen)
1979{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001980 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001981 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001982
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04001983 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001984 return;
1985
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001986 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001987 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001988 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001989 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05001990 width = output->allocation.width;
1991 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001992 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001993 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001994 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001995 width = window->saved_allocation.width - 20 - window->margin * 2;
1996 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001997 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001998 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001999
2000 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002001}
2002
2003void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002004window_set_decoration(struct window *window, int decoration)
2005{
2006 window->decoration = decoration;
2007}
2008
2009void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002010window_set_user_data(struct window *window, void *data)
2011{
2012 window->user_data = data;
2013}
2014
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002015void *
2016window_get_user_data(struct window *window)
2017{
2018 return window->user_data;
2019}
2020
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002021void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002022window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002023 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002024{
2025 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002026}
2027
2028void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002029window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002030 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002031{
2032 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002033}
2034
2035void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002036window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002037 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002038{
2039 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002040}
2041
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002042void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002043window_set_button_handler(struct window *window,
2044 window_button_handler_t handler)
2045{
2046 window->button_handler = handler;
2047}
2048
2049void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002050window_set_motion_handler(struct window *window,
2051 window_motion_handler_t handler)
2052{
2053 window->motion_handler = handler;
2054}
2055
2056void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002057window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002058 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002059{
2060 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002061}
2062
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002063void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064window_set_data_handler(struct window *window, window_data_handler_t handler)
2065{
2066 window->data_handler = handler;
2067}
2068
2069void
2070window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2071{
2072 window->drop_handler = handler;
2073}
2074
2075void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002076window_set_close_handler(struct window *window,
2077 window_close_handler_t handler)
2078{
2079 window->close_handler = handler;
2080}
2081
2082void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002083window_set_transparent(struct window *window, int transparent)
2084{
2085 window->transparent = transparent;
2086}
2087
2088void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002089window_set_title(struct window *window, const char *title)
2090{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002091 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002092 window->title = strdup(title);
2093}
2094
2095const char *
2096window_get_title(struct window *window)
2097{
2098 return window->title;
2099}
2100
2101void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002102display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2103 int32_t x, int32_t y, int32_t width, int32_t height)
2104{
2105 struct wl_buffer *buffer;
2106
2107 buffer = display_get_buffer_for_surface(display, cairo_surface);
2108
2109 wl_buffer_damage(buffer, x, y, width, height);
2110}
2111
2112void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002113window_damage(struct window *window, int32_t x, int32_t y,
2114 int32_t width, int32_t height)
2115{
2116 wl_surface_damage(window->surface, x, y, width, height);
2117}
2118
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002119static struct window *
2120window_create_internal(struct display *display, struct window *parent,
2121 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002122{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002123 struct window *window;
2124
2125 window = malloc(sizeof *window);
2126 if (window == NULL)
2127 return NULL;
2128
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002129 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002130 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002131 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002132 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002133 if (display->shell) {
2134 window->shell_surface =
2135 wl_shell_get_shell_surface(display->shell,
2136 window->surface);
2137 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002138 window->allocation.x = 0;
2139 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002140 window->allocation.width = width;
2141 window->allocation.height = height;
2142 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002143 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002144 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002145 window->transparent = 1;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002146 wl_list_init(&window->widget_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002147
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002148 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002149#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002150 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2151 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002152#else
2153 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2154#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002155 else
2156 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002157
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002158 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002159 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002160
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002161 if (window->shell_surface) {
2162 wl_shell_surface_set_user_data(window->shell_surface, window);
2163 wl_shell_surface_add_listener(window->shell_surface,
2164 &shell_surface_listener, window);
2165 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002166
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002167 return window;
2168}
2169
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002170struct window *
2171window_create(struct display *display, int32_t width, int32_t height)
2172{
2173 struct window *window;
2174
2175 window = window_create_internal(display, NULL, width, height);
2176 if (!window)
2177 return NULL;
2178
2179 return window;
2180}
2181
2182struct window *
2183window_create_transient(struct display *display, struct window *parent,
2184 int32_t x, int32_t y, int32_t width, int32_t height)
2185{
2186 struct window *window;
2187
2188 window = window_create_internal(parent->display,
2189 parent, width, height);
2190 if (!window)
2191 return NULL;
2192
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002193 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002194 window->x = x;
2195 window->y = y;
2196
2197 return window;
2198}
2199
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002200static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002201menu_set_item(struct widget *widget, struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002202{
2203 int next;
2204
2205 next = (sy - 8) / 20;
2206 if (menu->current != next) {
2207 menu->current = next;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002208 widget_schedule_redraw(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002209 }
2210
2211 return POINTER_LEFT_PTR;
2212}
2213
2214static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002215menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002216 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002217 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002218{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002219 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002220
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002221 return menu_set_item(menu->widget, menu, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002222}
2223
2224static void
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002225menu_enter_handler(struct widget *widget,
2226 struct input *input, uint32_t time,
2227 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002228{
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002229 menu_set_item(widget, data, y);
2230}
2231
2232static void
2233menu_leave_handler(struct widget *widget, struct input *input, void *data)
2234{
2235 menu_set_item(widget, data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002236}
2237
2238static void
2239menu_button_handler(struct window *window,
2240 struct input *input, uint32_t time,
2241 int button, int state, void *data)
2242
2243{
2244 struct menu *menu = data;
2245
2246 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002247 if (state == 0 && time - menu->time > 500) {
2248 menu->func(window->parent,
2249 menu->current, window->parent->user_data);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002250 window_destroy(window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002251 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002252}
2253
2254static void
2255menu_redraw_handler(struct window *window, void *data)
2256{
2257 cairo_t *cr;
2258 const int32_t r = 3, margin = 3;
2259 struct menu *menu = data;
2260 int32_t width, height, i;
2261
2262 width = 200;
2263 height = menu->count * 20 + margin * 2;
2264 window_set_child_size(window, width, height);
2265 window_create_surface(window);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002266 widget_set_allocation(menu->widget, 0, 0, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002267
2268 cr = cairo_create(window->cairo_surface);
2269 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2270 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2271 cairo_paint(cr);
2272
2273 width = window->allocation.width;
2274 height = window->allocation.height;
2275 rounded_rect(cr, 0, 0, width, height, r);
2276 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2277 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2278 cairo_fill(cr);
2279
2280 for (i = 0; i < menu->count; i++) {
2281 if (i == menu->current) {
2282 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2283 cairo_rectangle(cr, margin, i * 20 + margin,
2284 width - 2 * margin, 20);
2285 cairo_fill(cr);
2286 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2287 cairo_move_to(cr, 10, i * 20 + 16);
2288 cairo_show_text(cr, menu->entries[i]);
2289 } else {
2290 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2291 cairo_move_to(cr, 10, i * 20 + 16);
2292 cairo_show_text(cr, menu->entries[i]);
2293 }
2294 }
2295
2296 cairo_destroy(cr);
2297 window_flush(window);
2298}
2299
2300struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002301window_create_menu(struct display *display,
2302 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002303 int32_t x, int32_t y,
2304 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002305{
2306 struct window *window;
2307 struct menu *menu;
2308
2309 menu = malloc(sizeof *menu);
2310 if (!menu)
2311 return NULL;
2312
2313 window = window_create_internal(parent->display, parent, 0, 0);
2314 if (!window)
2315 return NULL;
2316
2317 menu->window = window;
2318 menu->entries = entries;
2319 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002320 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002321 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002322 window->decoration = 0;
2323 window->type = TYPE_MENU;
2324 window->x = x;
2325 window->y = y;
2326
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002327 wl_shell_surface_set_popup(window->shell_surface,
2328 input->input_device, time,
2329 window->parent->shell_surface,
2330 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332 window_set_button_handler(window, menu_button_handler);
2333 window_set_redraw_handler(window, menu_redraw_handler);
2334 window_set_user_data(window, menu);
2335
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002336 menu->widget = window_add_widget(window, menu);
2337 widget_set_enter_handler(menu->widget, menu_enter_handler);
2338 widget_set_leave_handler(menu->widget, menu_leave_handler);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002339 widget_set_motion_handler(menu->widget, menu_motion_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002340
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002341 return window;
2342}
2343
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002344void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002345window_set_buffer_type(struct window *window, enum window_buffer_type type)
2346{
2347 window->buffer_type = type;
2348}
2349
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002350
2351static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002352display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002353 struct wl_output *wl_output,
2354 int x, int y,
2355 int physical_width,
2356 int physical_height,
2357 int subpixel,
2358 const char *make,
2359 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002360{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002361 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002362
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002363 output->allocation.x = x;
2364 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002365}
2366
2367static void
2368display_handle_mode(void *data,
2369 struct wl_output *wl_output,
2370 uint32_t flags,
2371 int width,
2372 int height,
2373 int refresh)
2374{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002375 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002376 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002377
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002378 if (flags & WL_OUTPUT_MODE_CURRENT) {
2379 output->allocation.width = width;
2380 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002381 if (display->output_configure_handler)
2382 (*display->output_configure_handler)(
2383 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002384 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002385}
2386
2387static const struct wl_output_listener output_listener = {
2388 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002389 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002390};
2391
2392static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002393display_add_output(struct display *d, uint32_t id)
2394{
2395 struct output *output;
2396
2397 output = malloc(sizeof *output);
2398 if (output == NULL)
2399 return;
2400
2401 memset(output, 0, sizeof *output);
2402 output->display = d;
2403 output->output =
2404 wl_display_bind(d->display, id, &wl_output_interface);
2405 wl_list_insert(d->output_list.prev, &output->link);
2406
2407 wl_output_add_listener(output->output, &output_listener, output);
2408}
2409
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002410static void
2411output_destroy(struct output *output)
2412{
2413 if (output->destroy_handler)
2414 (*output->destroy_handler)(output, output->user_data);
2415
2416 wl_output_destroy(output->output);
2417 wl_list_remove(&output->link);
2418 free(output);
2419}
2420
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002421void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002422display_set_output_configure_handler(struct display *display,
2423 display_output_handler_t handler)
2424{
2425 struct output *output;
2426
2427 display->output_configure_handler = handler;
2428 if (!handler)
2429 return;
2430
2431 wl_list_for_each(output, &display->output_list, link)
2432 (*display->output_configure_handler)(output,
2433 display->user_data);
2434}
2435
2436void
2437output_set_user_data(struct output *output, void *data)
2438{
2439 output->user_data = data;
2440}
2441
2442void *
2443output_get_user_data(struct output *output)
2444{
2445 return output->user_data;
2446}
2447
2448void
2449output_set_destroy_handler(struct output *output,
2450 display_output_handler_t handler)
2451{
2452 output->destroy_handler = handler;
2453 /* FIXME: implement this, once we have way to remove outputs */
2454}
2455
2456void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002457output_get_allocation(struct output *output, struct rectangle *allocation)
2458{
2459 *allocation = output->allocation;
2460}
2461
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002462struct wl_output *
2463output_get_wl_output(struct output *output)
2464{
2465 return output->output;
2466}
2467
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002468static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002469display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002470{
2471 struct input *input;
2472
2473 input = malloc(sizeof *input);
2474 if (input == NULL)
2475 return;
2476
2477 memset(input, 0, sizeof *input);
2478 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002479 input->input_device =
2480 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002481 input->pointer_focus = NULL;
2482 input->keyboard_focus = NULL;
2483 wl_list_insert(d->input_list.prev, &input->link);
2484
2485 wl_input_device_add_listener(input->input_device,
2486 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002487 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002488
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002489 input->data_device =
2490 wl_data_device_manager_get_data_device(d->data_device_manager,
2491 input->input_device);
2492 wl_data_device_add_listener(input->data_device,
2493 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002494}
2495
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002496static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002497input_destroy(struct input *input)
2498{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002499 input_remove_keyboard_focus(input);
2500 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002501
2502 if (input->drag_offer)
2503 data_offer_destroy(input->drag_offer);
2504
2505 if (input->selection_offer)
2506 data_offer_destroy(input->selection_offer);
2507
2508 wl_data_device_destroy(input->data_device);
2509 wl_list_remove(&input->link);
2510 wl_input_device_destroy(input->input_device);
2511 free(input);
2512}
2513
2514static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002515display_handle_global(struct wl_display *display, uint32_t id,
2516 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002517{
2518 struct display *d = data;
2519
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002520 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002521 d->compositor =
2522 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002523 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002524 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002525 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002526 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002527 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002528 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002529 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002530 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002531 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2532 d->data_device_manager =
2533 wl_display_bind(display, id,
2534 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002535 }
2536}
2537
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002538static void
2539display_render_frame(struct display *d)
2540{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002541 int radius = 8;
2542 cairo_t *cr;
2543
2544 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2545 cr = cairo_create(d->shadow);
2546 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2547 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2548 rounded_rect(cr, 16, 16, 112, 112, radius);
2549 cairo_fill(cr);
2550 cairo_destroy(cr);
2551 blur_surface(d->shadow, 64);
2552
2553 d->active_frame =
2554 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2555 cr = cairo_create(d->active_frame);
2556 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2557 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2558 rounded_rect(cr, 16, 16, 112, 112, radius);
2559 cairo_fill(cr);
2560 cairo_destroy(cr);
2561
2562 d->inactive_frame =
2563 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2564 cr = cairo_create(d->inactive_frame);
2565 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2566 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2567 rounded_rect(cr, 16, 16, 112, 112, radius);
2568 cairo_fill(cr);
2569 cairo_destroy(cr);
2570}
2571
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002572static void
2573init_xkb(struct display *d)
2574{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002575 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002576
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002577 names.rules = "evdev";
2578 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002579 names.layout = option_xkb_layout;
2580 names.variant = option_xkb_variant;
2581 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002582
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002583 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002584 if (!d->xkb) {
2585 fprintf(stderr, "Failed to compile keymap\n");
2586 exit(1);
2587 }
2588}
2589
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002590static void
2591fini_xkb(struct display *display)
2592{
2593 xkb_free_keymap(display->xkb);
2594}
2595
Yuval Fledel45568f62010-12-06 09:18:12 -05002596static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002597init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002598{
2599 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002600 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002601
2602 static const EGLint premul_argb_cfg_attribs[] = {
2603 EGL_SURFACE_TYPE,
2604 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2605 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002606 EGL_RED_SIZE, 1,
2607 EGL_GREEN_SIZE, 1,
2608 EGL_BLUE_SIZE, 1,
2609 EGL_ALPHA_SIZE, 1,
2610 EGL_DEPTH_SIZE, 1,
2611 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2612 EGL_NONE
2613 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002614
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002615 static const EGLint rgb_cfg_attribs[] = {
2616 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2617 EGL_RED_SIZE, 1,
2618 EGL_GREEN_SIZE, 1,
2619 EGL_BLUE_SIZE, 1,
2620 EGL_ALPHA_SIZE, 0,
2621 EGL_DEPTH_SIZE, 1,
2622 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2623 EGL_NONE
2624 };
2625
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002626 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002627 if (!eglInitialize(d->dpy, &major, &minor)) {
2628 fprintf(stderr, "failed to initialize display\n");
2629 return -1;
2630 }
2631
2632 if (!eglBindAPI(EGL_OPENGL_API)) {
2633 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2634 return -1;
2635 }
2636
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002637 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002638 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002639 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002640 return -1;
2641 }
2642
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002643 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2644 &d->rgb_config, 1, &n) || n != 1) {
2645 fprintf(stderr, "failed to choose rgb config\n");
2646 return -1;
2647 }
2648
Benjamin Franzke0c991632011-09-27 21:57:31 +02002649 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2650 if (d->rgb_ctx == NULL) {
2651 fprintf(stderr, "failed to create context\n");
2652 return -1;
2653 }
2654 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2655 EGL_NO_CONTEXT, NULL);
2656 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002657 fprintf(stderr, "failed to create context\n");
2658 return -1;
2659 }
2660
Benjamin Franzke0c991632011-09-27 21:57:31 +02002661 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002662 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002663 return -1;
2664 }
2665
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002666#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002667 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2668 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002669 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002670 return -1;
2671 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002672 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2673 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2674 fprintf(stderr, "failed to get cairo egl argb device\n");
2675 return -1;
2676 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002677#endif
2678
2679 return 0;
2680}
2681
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002682static void
2683fini_egl(struct display *display)
2684{
2685#ifdef HAVE_CAIRO_EGL
2686 cairo_device_destroy(display->argb_device);
2687 cairo_device_destroy(display->rgb_device);
2688#endif
2689
2690 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2691 EGL_NO_CONTEXT);
2692
2693 eglTerminate(display->dpy);
2694 eglReleaseThread();
2695}
2696
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002697static int
2698event_mask_update(uint32_t mask, void *data)
2699{
2700 struct display *d = data;
2701
2702 d->mask = mask;
2703
2704 return 0;
2705}
2706
2707static void
2708handle_display_data(struct task *task, uint32_t events)
2709{
2710 struct display *display =
2711 container_of(task, struct display, display_task);
2712
2713 wl_display_iterate(display->display, display->mask);
2714}
2715
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002716struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002717display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002718{
2719 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002720 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002721 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002722 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002723
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002724 g_type_init();
2725
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002726 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002727 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002728 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002729
2730 xkb_option_group = g_option_group_new("xkb",
2731 "Keyboard options",
2732 "Show all XKB options",
2733 NULL, NULL);
2734 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2735 g_option_context_add_group (context, xkb_option_group);
2736
2737 if (!g_option_context_parse(context, argc, argv, &error)) {
2738 fprintf(stderr, "option parsing failed: %s\n", error->message);
2739 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002740 }
2741
Tim Wiederhake748f6722011-01-23 23:25:25 +01002742 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002743
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002744 d = malloc(sizeof *d);
2745 if (d == NULL)
2746 return NULL;
2747
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002748 memset(d, 0, sizeof *d);
2749
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002750 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002751 if (d->display == NULL) {
2752 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002753 return NULL;
2754 }
2755
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002756 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2757 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2758 d->display_task.run = handle_display_data;
2759 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2760
2761 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002762 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002763 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002764
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002765 /* Set up listener so we'll catch all events. */
2766 wl_display_add_global_listener(d->display,
2767 display_handle_global, d);
2768
2769 /* Process connection events. */
2770 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002771 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002772 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002773
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002774 d->image_target_texture_2d =
2775 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2776 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2777 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2778
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002779 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002780
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002781 display_render_frame(d);
2782
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002783 wl_list_init(&d->window_list);
2784
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002785 init_xkb(d);
2786
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002787 return d;
2788}
2789
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002790static void
2791display_destroy_outputs(struct display *display)
2792{
2793 struct output *tmp;
2794 struct output *output;
2795
2796 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2797 output_destroy(output);
2798}
2799
Pekka Paalanene1207c72011-12-16 12:02:09 +02002800static void
2801display_destroy_inputs(struct display *display)
2802{
2803 struct input *tmp;
2804 struct input *input;
2805
2806 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2807 input_destroy(input);
2808}
2809
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002810void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002811display_destroy(struct display *display)
2812{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002813 if (!wl_list_empty(&display->window_list))
2814 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2815
2816 if (!wl_list_empty(&display->deferred_list))
2817 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2818
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002819 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002820 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002821
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002822 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002823
2824 cairo_surface_destroy(display->active_frame);
2825 cairo_surface_destroy(display->inactive_frame);
2826 cairo_surface_destroy(display->shadow);
2827 destroy_pointer_surfaces(display);
2828
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002829 fini_egl(display);
2830
Pekka Paalanenc2052982011-12-16 11:41:32 +02002831 if (display->shell)
2832 wl_shell_destroy(display->shell);
2833
2834 if (display->shm)
2835 wl_shm_destroy(display->shm);
2836
2837 if (display->data_device_manager)
2838 wl_data_device_manager_destroy(display->data_device_manager);
2839
2840 wl_compositor_destroy(display->compositor);
2841
2842 close(display->epoll_fd);
2843
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002844 wl_display_flush(display->display);
2845 wl_display_destroy(display->display);
2846 free(display);
2847}
2848
2849void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002850display_set_user_data(struct display *display, void *data)
2851{
2852 display->user_data = data;
2853}
2854
2855void *
2856display_get_user_data(struct display *display)
2857{
2858 return display->user_data;
2859}
2860
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002861struct wl_display *
2862display_get_display(struct display *display)
2863{
2864 return display->display;
2865}
2866
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002867struct output *
2868display_get_output(struct display *display)
2869{
2870 return container_of(display->output_list.next, struct output, link);
2871}
2872
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002873struct wl_compositor *
2874display_get_compositor(struct display *display)
2875{
2876 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002877}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002878
2879EGLDisplay
2880display_get_egl_display(struct display *d)
2881{
2882 return d->dpy;
2883}
2884
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002885struct wl_data_source *
2886display_create_data_source(struct display *display)
2887{
2888 return wl_data_device_manager_create_data_source(display->data_device_manager);
2889}
2890
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002891EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002892display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002893{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002894 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002895}
2896
Benjamin Franzke0c991632011-09-27 21:57:31 +02002897EGLConfig
2898display_get_argb_egl_config(struct display *d)
2899{
2900 return d->premultiplied_argb_config;
2901}
2902
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002903struct wl_shell *
2904display_get_shell(struct display *display)
2905{
2906 return display->shell;
2907}
2908
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002909int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002910display_acquire_window_surface(struct display *display,
2911 struct window *window,
2912 EGLContext ctx)
2913{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002914#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002915 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002916 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002917
2918 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002919 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002920 device = cairo_surface_get_device(window->cairo_surface);
2921 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002922 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002923
Benjamin Franzke0c991632011-09-27 21:57:31 +02002924 if (!ctx) {
2925 if (device == display->rgb_device)
2926 ctx = display->rgb_ctx;
2927 else if (device == display->argb_device)
2928 ctx = display->argb_ctx;
2929 else
2930 assert(0);
2931 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002932
2933 data = cairo_surface_get_user_data(window->cairo_surface,
2934 &surface_data_key);
2935
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002936 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002937 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002938 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2939 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002940
2941 return 0;
2942#else
2943 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002944#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002945}
2946
2947void
Benjamin Franzke0c991632011-09-27 21:57:31 +02002948display_release_window_surface(struct display *display,
2949 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002950{
Benjamin Franzke0c991632011-09-27 21:57:31 +02002951#ifdef HAVE_CAIRO_EGL
2952 cairo_device_t *device;
2953
2954 device = cairo_surface_get_device(window->cairo_surface);
2955 if (!device)
2956 return;
2957
2958 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002959 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02002960 cairo_device_release(device);
2961#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002962}
2963
2964void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002965display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002966{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002967 wl_list_insert(&display->deferred_list, &task->link);
2968}
2969
2970void
2971display_watch_fd(struct display *display,
2972 int fd, uint32_t events, struct task *task)
2973{
2974 struct epoll_event ep;
2975
2976 ep.events = events;
2977 ep.data.ptr = task;
2978 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2979}
2980
2981void
2982display_run(struct display *display)
2983{
2984 struct task *task;
2985 struct epoll_event ep[16];
2986 int i, count;
2987
Pekka Paalanen826d7952011-12-15 10:14:07 +02002988 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002989 while (1) {
2990 while (display->mask & WL_DISPLAY_WRITABLE)
2991 wl_display_iterate(display->display,
2992 WL_DISPLAY_WRITABLE);
2993
Pekka Paalanen826d7952011-12-15 10:14:07 +02002994 if (!display->running)
2995 break;
2996
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002997 count = epoll_wait(display->epoll_fd,
2998 ep, ARRAY_LENGTH(ep), -1);
2999 for (i = 0; i < count; i++) {
3000 task = ep[i].data.ptr;
3001 task->run(task, ep[i].events);
3002 }
3003
3004 while (!wl_list_empty(&display->deferred_list)) {
3005 task = container_of(display->deferred_list.next,
3006 struct task, link);
3007 wl_list_remove(&task->link);
3008 task->run(task, 0);
3009 }
3010 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003011}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003012
3013void
3014display_exit(struct display *display)
3015{
3016 display->running = 0;
3017}