blob: 67e8dd05154be116fbb04a536bc89e82c83507d2 [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øgsbergb67e94b2012-01-10 12:23:19 -0500118 int resize_scheduled;
119 struct task resize_task;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500121 int margin;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122 int type;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400123 int decoration;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400124 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400125 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400127 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500128
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500129 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500130
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500131 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500132 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400133 window_data_handler_t data_handler;
134 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500135 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400136
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500137 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500138 struct widget *focus_widget;
139 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400140
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141 struct window *menu;
142
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500143 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400144 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500145};
146
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500147struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500148 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500149 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400150 struct wl_list link;
151 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500152 widget_resize_handler_t resize_handler;
153 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500154 widget_enter_handler_t enter_handler;
155 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500156 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500157 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400158 void *user_data;
159};
160
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400161struct input {
162 struct display *display;
163 struct wl_input_device *input_device;
164 struct window *pointer_focus;
165 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400166 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400167 uint32_t modifiers;
168 int32_t x, y, sx, sy;
169 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400170
171 struct wl_data_device *data_device;
172 struct data_offer *drag_offer;
173 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174};
175
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500176struct output {
177 struct display *display;
178 struct wl_output *output;
179 struct rectangle allocation;
180 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200181
182 display_output_handler_t destroy_handler;
183 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500184};
185
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500186struct menu {
187 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500188 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500189 const char **entries;
190 uint32_t time;
191 int current;
192 int count;
193 menu_func_t func;
194};
195
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400196enum {
197 POINTER_DEFAULT = 100,
198 POINTER_UNSET
199};
200
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500201enum window_location {
202 WINDOW_INTERIOR = 0,
203 WINDOW_RESIZING_TOP = 1,
204 WINDOW_RESIZING_BOTTOM = 2,
205 WINDOW_RESIZING_LEFT = 4,
206 WINDOW_RESIZING_TOP_LEFT = 5,
207 WINDOW_RESIZING_BOTTOM_LEFT = 6,
208 WINDOW_RESIZING_RIGHT = 8,
209 WINDOW_RESIZING_TOP_RIGHT = 9,
210 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
211 WINDOW_RESIZING_MASK = 15,
212 WINDOW_EXTERIOR = 16,
213 WINDOW_TITLEBAR = 17,
214 WINDOW_CLIENT_AREA = 18,
215};
216
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400217const char *option_xkb_layout = "us";
218const char *option_xkb_variant = "";
219const char *option_xkb_options = "";
220
221static const GOptionEntry xkb_option_entries[] = {
222 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
223 &option_xkb_layout, "XKB Layout" },
224 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
225 &option_xkb_variant, "XKB Variant" },
226 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_options, "XKB Options" },
228 { NULL }
229};
230
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400231static const cairo_user_data_key_t surface_data_key;
232struct surface_data {
233 struct wl_buffer *buffer;
234};
235
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500236#define MULT(_d,c,a,t) \
237 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
238
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500239#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400240
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100241struct egl_window_surface_data {
242 struct display *display;
243 struct wl_surface *surface;
244 struct wl_egl_window *window;
245 EGLSurface surf;
246};
247
248static void
249egl_window_surface_data_destroy(void *p)
250{
251 struct egl_window_surface_data *data = p;
252 struct display *d = data->display;
253
254 eglDestroySurface(d->dpy, data->surf);
255 wl_egl_window_destroy(data->window);
256 data->surface = NULL;
257
258 free(p);
259}
260
261static cairo_surface_t *
262display_create_egl_window_surface(struct display *display,
263 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400264 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100265 struct rectangle *rectangle)
266{
267 cairo_surface_t *cairo_surface;
268 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 EGLConfig config;
270 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200271 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100272
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 static const EGLint premul_attribs[] = {
274 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
275 EGL_NONE
276 };
277
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278 data = malloc(sizeof *data);
279 if (data == NULL)
280 return NULL;
281
282 data->display = display;
283 data->surface = surface;
284
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 if (flags & SURFACE_OPAQUE) {
286 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200287 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200288 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 } else {
290 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400292 attribs = premul_attribs;
293 }
294
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400295 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100296 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400299 data->surf = eglCreateWindowSurface(display->dpy, config,
300 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Benjamin Franzke0c991632011-09-27 21:57:31 +0200302 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100303 data->surf,
304 rectangle->width,
305 rectangle->height);
306
307 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
308 data, egl_window_surface_data_destroy);
309
310 return cairo_surface;
311}
312
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500313struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400314 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200315 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316 EGLImageKHR image;
317 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800318 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320};
321
322static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400324{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500325 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800326 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400327
Benjamin Franzke0c991632011-09-27 21:57:31 +0200328 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200330 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800331
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500332 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500334 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336}
337
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339display_get_image_for_egl_image_surface(struct display *display,
340 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500343
344 data = cairo_surface_get_user_data (surface, &surface_data_key);
345
346 return data->image;
347}
348
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500349static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400351 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500352 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400353{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355 EGLDisplay dpy = display->dpy;
356 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200357 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358
359 data = malloc(sizeof *data);
360 if (data == NULL)
361 return NULL;
362
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800363 data->display = display;
364
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400365 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400366 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500367 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000368 free(data);
369 return NULL;
370 }
371
Benjamin Franzke0c991632011-09-27 21:57:31 +0200372 if (flags & SURFACE_OPAQUE) {
373 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 content = CAIRO_CONTENT_COLOR;
375 } else {
376 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 content = CAIRO_CONTENT_COLOR_ALPHA;
378 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400379
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500380 data->image = display->create_image(dpy, NULL,
381 EGL_NATIVE_PIXMAP_KHR,
382 (EGLClientBuffer) data->pixmap,
383 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500385 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500386 free(data);
387 return NULL;
388 }
389
390 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400391 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500392
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500396 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398
Benjamin Franzke0c991632011-09-27 21:57:31 +0200399 surface = cairo_gl_surface_create_for_texture(data->device,
400 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 data->texture,
402 rectangle->width,
403 rectangle->height);
404
405 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500406 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407
408 return surface;
409}
410
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500411static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412display_create_egl_image_surface_from_file(struct display *display,
413 const char *filename,
414 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415{
416 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417 GdkPixbuf *pixbuf;
418 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400419 int stride, i;
420 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500421 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400422
423 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425 FALSE, &error);
426 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
430 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500431 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400432 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400434
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435
436 stride = gdk_pixbuf_get_rowstride(pixbuf);
437 pixels = gdk_pixbuf_get_pixels(pixbuf);
438
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400439 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400440 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400441 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400442 while (p < end) {
443 unsigned int t;
444
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400445 MULT(p[0], p[0], p[3], t);
446 MULT(p[1], p[1], p[3], t);
447 MULT(p[2], p[2], p[3], t);
448 p += 4;
449
450 }
451 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400452
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200453 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000454 if (surface == NULL) {
455 g_object_unref(pixbuf);
456 return NULL;
457 }
458
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800459 data = cairo_surface_get_user_data(surface, &surface_data_key);
460
Benjamin Franzke0c991632011-09-27 21:57:31 +0200461 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800462 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800463 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
464 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200465 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400466
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500467 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400468
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400469 return surface;
470}
471
472#endif
473
474struct wl_buffer *
475display_get_buffer_for_surface(struct display *display,
476 cairo_surface_t *surface)
477{
478 struct surface_data *data;
479
480 data = cairo_surface_get_user_data (surface, &surface_data_key);
481
482 return data->buffer;
483}
484
485struct shm_surface_data {
486 struct surface_data data;
487 void *map;
488 size_t length;
489};
490
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500491static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492shm_surface_data_destroy(void *p)
493{
494 struct shm_surface_data *data = p;
495
496 wl_buffer_destroy(data->data.buffer);
497 munmap(data->map, data->length);
498}
499
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500500static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503{
504 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400505 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400506 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800507 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400508 char filename[] = "/tmp/wayland-shm-XXXXXX";
509
510 data = malloc(sizeof *data);
511 if (data == NULL)
512 return NULL;
513
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
515 rectangle->width);
516 data->length = stride * rectangle->height;
517 fd = mkstemp(filename);
518 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000519 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 return NULL;
521 }
522 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000523 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 close(fd);
525 return NULL;
526 }
527
528 data->map = mmap(NULL, data->length,
529 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
530 unlink(filename);
531
532 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000533 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 close(fd);
535 return NULL;
536 }
537
538 surface = cairo_image_surface_create_for_data (data->map,
539 CAIRO_FORMAT_ARGB32,
540 rectangle->width,
541 rectangle->height,
542 stride);
543
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
546
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400548 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400549 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400550 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400552 data->data.buffer = wl_shm_create_buffer(display->shm,
553 fd,
554 rectangle->width,
555 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400556 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400557
558 close(fd);
559
560 return surface;
561}
562
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500563static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400564display_create_shm_surface_from_file(struct display *display,
565 const char *filename,
566 struct rectangle *rect)
567{
568 cairo_surface_t *surface;
569 GdkPixbuf *pixbuf;
570 GError *error = NULL;
571 int stride, i;
572 unsigned char *pixels, *p, *end, *dest_data;
573 int dest_stride;
574 uint32_t *d;
575
576 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
577 rect->width, rect->height,
578 FALSE, &error);
579 if (error != NULL)
580 return NULL;
581
582 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
583 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500584 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400585 return NULL;
586 }
587
588 stride = gdk_pixbuf_get_rowstride(pixbuf);
589 pixels = gdk_pixbuf_get_pixels(pixbuf);
590
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400591 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000592 if (surface == NULL) {
593 g_object_unref(pixbuf);
594 return NULL;
595 }
596
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597 dest_data = cairo_image_surface_get_data (surface);
598 dest_stride = cairo_image_surface_get_stride (surface);
599
600 for (i = 0; i < rect->height; i++) {
601 d = (uint32_t *) (dest_data + i * dest_stride);
602 p = pixels + i * stride;
603 end = p + rect->width * 4;
604 while (p < end) {
605 unsigned int t;
606 unsigned char a, r, g, b;
607
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400608 a = p[3];
609 MULT(r, p[0], a, t);
610 MULT(g, p[1], a, t);
611 MULT(b, p[2], a, t);
612 p += 4;
613 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
614 }
615 }
616
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500617 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400618
619 return surface;
620}
621
nobled7b87cb02011-02-01 18:51:47 +0000622static int
623check_size(struct rectangle *rect)
624{
625 if (rect->width && rect->height)
626 return 0;
627
628 fprintf(stderr, "tried to create surface of "
629 "width: %d, height: %d\n", rect->width, rect->height);
630 return -1;
631}
632
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400633cairo_surface_t *
634display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400636 struct rectangle *rectangle,
637 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400638{
nobled7b87cb02011-02-01 18:51:47 +0000639 if (check_size(rectangle) < 0)
640 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500641#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500642 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643 if (surface)
644 return display_create_egl_window_surface(display,
645 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 rectangle);
648 else
649 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400650 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500652 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400654 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655}
656
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500657static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658display_create_surface_from_file(struct display *display,
659 const char *filename,
660 struct rectangle *rectangle)
661{
nobled7b87cb02011-02-01 18:51:47 +0000662 if (check_size(rectangle) < 0)
663 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500664#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500665 if (display->dpy) {
666 return display_create_egl_image_surface_from_file(display,
667 filename,
668 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500671 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400672}
Yuval Fledel45568f62010-12-06 09:18:12 -0500673 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400674 const char *filename;
675 int hotspot_x, hotspot_y;
676} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500677 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
678 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
679 { DATADIR "/weston/bottom_side.png", 16, 20 },
680 { DATADIR "/weston/grabbing.png", 20, 17 },
681 { DATADIR "/weston/left_ptr.png", 10, 5 },
682 { DATADIR "/weston/left_side.png", 10, 20 },
683 { DATADIR "/weston/right_side.png", 30, 19 },
684 { DATADIR "/weston/top_left_corner.png", 8, 8 },
685 { DATADIR "/weston/top_right_corner.png", 26, 8 },
686 { DATADIR "/weston/top_side.png", 18, 8 },
687 { DATADIR "/weston/xterm.png", 15, 15 },
688 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689};
690
691static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693{
694 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400698 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400699 display->pointer_surfaces =
700 malloc(count * sizeof *display->pointer_surfaces);
701 rect.width = width;
702 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400704 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705 display_create_surface_from_file(display,
706 pointer_images[i].filename,
707 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100708 if (!display->pointer_surfaces[i]) {
709 fprintf(stderr, "Error loading pointer image: %s\n",
710 pointer_images[i].filename);
711 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400712 }
713
714}
715
Pekka Paalanen325bb602011-12-19 10:31:45 +0200716static void
717destroy_pointer_surfaces(struct display *display)
718{
719 int i, count;
720
721 count = ARRAY_LENGTH(pointer_images);
722 for (i = 0; i < count; ++i) {
723 if (display->pointer_surfaces[i])
724 cairo_surface_destroy(display->pointer_surfaces[i]);
725 }
726 free(display->pointer_surfaces);
727}
728
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400729cairo_surface_t *
730display_get_pointer_surface(struct display *display, int pointer,
731 int *width, int *height,
732 int *hotspot_x, int *hotspot_y)
733{
734 cairo_surface_t *surface;
735
736 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500737#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400738 *width = cairo_gl_surface_get_width(surface);
739 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000740#else
741 *width = cairo_image_surface_get_width(surface);
742 *height = cairo_image_surface_get_height(surface);
743#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 *hotspot_x = pointer_images[pointer].hotspot_x;
745 *hotspot_y = pointer_images[pointer].hotspot_y;
746
747 return cairo_surface_reference(surface);
748}
749
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400750static void
751window_attach_surface(struct window *window);
752
753static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755{
756 struct window *window = data;
757
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400758 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759 cairo_surface_destroy(window->pending_surface);
760 window->pending_surface = NULL;
761 if (window->cairo_surface)
762 window_attach_surface(window);
763}
764
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400765static const struct wl_callback_listener free_surface_listener = {
766 free_surface
767};
768
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500769static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200770window_get_resize_dx_dy(struct window *window, int *x, int *y)
771{
772 if (window->resize_edges & WINDOW_RESIZING_LEFT)
773 *x = window->server_allocation.width - window->allocation.width;
774 else
775 *x = 0;
776
777 if (window->resize_edges & WINDOW_RESIZING_TOP)
778 *y = window->server_allocation.height -
779 window->allocation.height;
780 else
781 *y = 0;
782
783 window->resize_edges = 0;
784}
785
786static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400787window_set_type(struct window *window)
788{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200789 if (!window->shell_surface)
790 return;
791
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400792 switch (window->type) {
793 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200794 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
796 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400798 break;
799 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200800 wl_shell_surface_set_transient(window->shell_surface,
801 window->parent->shell_surface,
802 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400803 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500804 case TYPE_MENU:
805 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400806 case TYPE_CUSTOM:
807 break;
808 }
809}
810
811static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500812window_attach_surface(struct window *window)
813{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400814 struct display *display = window->display;
815 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400816 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000817#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000819#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500820 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400822 if (display->shell)
823 window_set_type(window);
824
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000826#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
828 data = cairo_surface_get_user_data(window->cairo_surface,
829 &surface_data_key);
830
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 cairo_gl_surface_swapbuffers(window->cairo_surface);
832 wl_egl_window_get_attached_size(data->window,
833 &window->server_allocation.width,
834 &window->server_allocation.height);
835 break;
836 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000837#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200839 window_get_resize_dx_dy(window, &x, &y);
840
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100841 if (window->pending_surface != NULL)
842 return;
843
844 window->pending_surface = window->cairo_surface;
845 window->cairo_surface = NULL;
846
847 buffer =
848 display_get_buffer_for_surface(display,
849 window->pending_surface);
850
851 wl_surface_attach(window->surface, buffer, x, y);
852 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400853 cb = wl_display_sync(display->display);
854 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000856 default:
857 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100858 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500859
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500860 wl_surface_damage(window->surface, 0, 0,
861 window->allocation.width,
862 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500863}
864
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500865void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400866window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500867{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100868 if (window->cairo_surface) {
869 switch (window->buffer_type) {
870 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
871 case WINDOW_BUFFER_TYPE_SHM:
872 display_surface_damage(window->display,
873 window->cairo_surface,
874 0, 0,
875 window->allocation.width,
876 window->allocation.height);
877 break;
878 default:
879 break;
880 }
881 window_attach_surface(window);
882 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500883}
884
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400885void
886window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400887{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500888 cairo_surface_reference(surface);
889
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890 if (window->cairo_surface != NULL)
891 cairo_surface_destroy(window->cairo_surface);
892
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500893 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894}
895
Benjamin Franzke22d54812011-07-16 19:50:32 +0000896#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100897static void
898window_resize_cairo_window_surface(struct window *window)
899{
900 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200901 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100902
903 data = cairo_surface_get_user_data(window->cairo_surface,
904 &surface_data_key);
905
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200906 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100907 wl_egl_window_resize(data->window,
908 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200909 window->allocation.height,
910 x,y);
911
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912 cairo_gl_surface_set_size(window->cairo_surface,
913 window->allocation.width,
914 window->allocation.height);
915}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000916#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100917
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400918struct display *
919window_get_display(struct window *window)
920{
921 return window->display;
922}
923
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400924void
925window_create_surface(struct window *window)
926{
927 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400928 uint32_t flags = 0;
929
930 if (!window->transparent)
931 flags = SURFACE_OPAQUE;
932
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500934#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100935 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
936 if (window->cairo_surface) {
937 window_resize_cairo_window_surface(window);
938 return;
939 }
940 surface = display_create_surface(window->display,
941 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400942 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100943 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500944 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400945 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100946 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400947 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400948 break;
949#endif
950 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400951 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400952 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800954 default:
955 surface = NULL;
956 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400958
959 window_set_surface(window, surface);
960 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961}
962
963static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500964window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500965{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500966 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500967 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400968 cairo_surface_t *frame;
969 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500970
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);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001015 if (window->resize_scheduled)
1016 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001017
1018 wl_list_for_each(input, &display->input_list, link) {
1019 if (input->pointer_focus == window)
1020 input->pointer_focus = NULL;
1021 if (input->keyboard_focus == window)
1022 input->keyboard_focus = NULL;
1023 }
1024
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001025 if (window->shell_surface)
1026 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001027 wl_surface_destroy(window->surface);
1028 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001029
1030 if (window->cairo_surface != NULL)
1031 cairo_surface_destroy(window->cairo_surface);
1032 if (window->pending_surface != NULL)
1033 cairo_surface_destroy(window->pending_surface);
1034
1035 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001036 free(window);
1037}
1038
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001040widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001041{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001042 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001043
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001044 wl_list_for_each(child, &widget->child_list, link) {
1045 target = widget_find_widget(child, x, y);
1046 if (target)
1047 return target;
1048 }
1049
1050 if (widget->allocation.x <= x &&
1051 x < widget->allocation.x + widget->allocation.width &&
1052 widget->allocation.y <= y &&
1053 y < widget->allocation.y + widget->allocation.height) {
1054 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055 }
1056
1057 return NULL;
1058}
1059
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001060static struct widget *
1061widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001062{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001063 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 widget = malloc(sizeof *widget);
1066 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001067 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001068 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001069 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001070 wl_list_init(&widget->child_list);
1071
1072 return widget;
1073}
1074
1075struct widget *
1076window_add_widget(struct window *window, void *data)
1077{
1078 window->widget = widget_create(window, data);
1079 wl_list_init(&window->widget->link);
1080
1081 return window->widget;
1082}
1083
1084struct widget *
1085widget_add_widget(struct widget *parent, void *data)
1086{
1087 struct widget *widget;
1088
1089 widget = widget_create(parent->window, data);
1090 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001096widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001098 wl_list_remove(&widget->link);
1099 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100}
1101
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001102struct widget *
1103window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001104{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001105 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001106}
1107
1108void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001109widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001110{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001111 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001112}
1113
1114void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001115widget_set_allocation(struct widget *widget,
1116 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001117{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001118 widget->allocation.x = x;
1119 widget->allocation.y = y;
1120 widget->allocation.width = width;
1121 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001122}
1123
1124void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001125widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001126{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001127 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001128}
1129
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001130void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001131widget_set_resize_handler(struct widget *widget,
1132 widget_resize_handler_t handler)
1133{
1134 widget->resize_handler = handler;
1135}
1136
1137void
1138widget_set_redraw_handler(struct widget *widget,
1139 widget_redraw_handler_t handler)
1140{
1141 widget->redraw_handler = handler;
1142}
1143
1144void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001145widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001146{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001147 widget->enter_handler = handler;
1148}
1149
1150void
1151widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1152{
1153 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001154}
1155
1156void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001157widget_set_motion_handler(struct widget *widget,
1158 widget_motion_handler_t handler)
1159{
1160 widget->motion_handler = handler;
1161}
1162
1163void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001164widget_set_button_handler(struct widget *widget,
1165 widget_button_handler_t handler)
1166{
1167 widget->button_handler = handler;
1168}
1169
1170void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001171widget_schedule_redraw(struct widget *widget)
1172{
1173 window_schedule_redraw(widget->window);
1174}
1175
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001176cairo_surface_t *
1177window_get_surface(struct window *window)
1178{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001179 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001180}
1181
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001182struct wl_surface *
1183window_get_wl_surface(struct window *window)
1184{
1185 return window->surface;
1186}
1187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001188struct wl_shell_surface *
1189window_get_wl_shell_surface(struct window *window)
1190{
1191 return window->shell_surface;
1192}
1193
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001194static int
1195get_pointer_location(struct window *window, int32_t x, int32_t y)
1196{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001197 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001198 const int grip_size = 8;
1199
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001200 if (!window->decoration)
1201 return WINDOW_CLIENT_AREA;
1202
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001203 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001204 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001205 else if (window->margin <= x && x < window->margin + grip_size)
1206 hlocation = WINDOW_RESIZING_LEFT;
1207 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001209 else if (x < window->allocation.width - window->margin)
1210 hlocation = WINDOW_RESIZING_RIGHT;
1211 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001212 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001213
1214 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001215 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001216 else if (window->margin <= y && y < window->margin + grip_size)
1217 vlocation = WINDOW_RESIZING_TOP;
1218 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001219 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001220 else if (y < window->allocation.height - window->margin)
1221 vlocation = WINDOW_RESIZING_BOTTOM;
1222 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001223 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001224
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001225 location = vlocation | hlocation;
1226 if (location & WINDOW_EXTERIOR)
1227 location = WINDOW_EXTERIOR;
1228 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1229 location = WINDOW_TITLEBAR;
1230 else if (location == WINDOW_INTERIOR)
1231 location = WINDOW_CLIENT_AREA;
1232
1233 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001234}
1235
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001236static int
1237input_get_pointer_image_for_location(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001238{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001239 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001240
1241 location = get_pointer_location(input->pointer_focus,
1242 input->sx, input->sy);
1243 switch (location) {
1244 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001245 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001247 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001249 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001251 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001253 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001255 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001257 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001258 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001259 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001260 case WINDOW_EXTERIOR:
1261 case WINDOW_TITLEBAR:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001262 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 default:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001264 return pointer;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001266}
1267
1268void
1269input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1270{
1271 struct display *display = input->display;
1272 struct wl_buffer *buffer;
1273 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001275 if (pointer == input->current_pointer_image)
1276 return;
1277
1278 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001279 surface = display->pointer_surfaces[pointer];
Rob Bradford8bd35c72011-10-25 12:20:51 +01001280
1281 if (!surface)
1282 return;
1283
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001284 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001285 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 pointer_images[pointer].hotspot_x,
1287 pointer_images[pointer].hotspot_y);
1288}
1289
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001290static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001291window_set_focus_widget(struct window *window, struct widget *focus,
1292 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001293{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001294 struct widget *old;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001295 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001296
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001297 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001298 return;
1299
Kristian Høgsbergee143232012-01-09 08:42:24 -05001300 old = window->focus_widget;
1301 if (old) {
1302 if (old->leave_handler)
1303 old->leave_handler(old, input, old->user_data);
1304 window->focus_widget = NULL;
1305 }
1306
1307 if (focus) {
1308 if (focus->enter_handler)
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001309 pointer = focus->enter_handler(focus, input, time,
1310 x, y, focus->user_data);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001311 window->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001312
1313 pointer = input_get_pointer_image_for_location(input, pointer);
1314 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001315 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001316}
1317
1318static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001319input_handle_motion(void *data, struct wl_input_device *input_device,
1320 uint32_t time,
1321 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001322{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001323 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001324 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001325 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001326 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001327
1328 input->x = x;
1329 input->y = y;
1330 input->sx = sx;
1331 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001332
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001333 if (!window->focus_widget || !window->widget_grab_button) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001334 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001335 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001336 }
1337
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001338 widget = window->focus_widget;
1339 if (widget && widget->motion_handler)
1340 pointer = widget->motion_handler(widget, input, time, sx, sy,
1341 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001342
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001343 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001344 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001345}
1346
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001347static void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001348window_menu_func(struct window *window, int index, void *data)
1349{
1350 switch (index) {
1351 case 0: /* close */
1352 if (window->close_handler)
1353 window->close_handler(window->parent,
1354 window->user_data);
1355 else
Kristian Høgsberg21eabf62012-01-08 15:41:52 -05001356 display_exit(window->display);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001357 break;
1358 case 1: /* fullscreen */
1359 /* we don't have a way to get out of fullscreen for now */
1360 window_set_fullscreen(window, 1);
1361 break;
1362 case 2: /* rotate */
1363 case 3: /* scale */
1364 break;
1365 }
1366}
1367
1368
1369static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001370input_handle_button(void *data,
1371 struct wl_input_device *input_device,
1372 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001373{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001374 struct input *input = data;
1375 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001376 struct widget *widget;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001377 int location;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001378 int32_t x, y;
1379 static const char *entries[] = {
1380 "Close", "Fullscreen", "Rotate", "Scale"
1381 };
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001382
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001383 if (window->focus_widget && window->widget_grab_button == 0 && state)
1384 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001385
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001386 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001387
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001388 widget = window->focus_widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001389 if (window->display->shell && button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001390 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001391 case WINDOW_TITLEBAR:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001392 if (!window->shell_surface)
1393 break;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001394 input_set_pointer_image(input, time, POINTER_DRAGGING);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001395 wl_shell_surface_move(window->shell_surface,
1396 input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001397 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001398 case WINDOW_RESIZING_TOP:
1399 case WINDOW_RESIZING_BOTTOM:
1400 case WINDOW_RESIZING_LEFT:
1401 case WINDOW_RESIZING_RIGHT:
1402 case WINDOW_RESIZING_TOP_LEFT:
1403 case WINDOW_RESIZING_TOP_RIGHT:
1404 case WINDOW_RESIZING_BOTTOM_LEFT:
1405 case WINDOW_RESIZING_BOTTOM_RIGHT:
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001406 if (!window->shell_surface)
1407 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001408 wl_shell_surface_resize(window->shell_surface,
1409 input_device, time,
1410 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001411 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001412 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001413 if (widget && widget->button_handler)
1414 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001415 input, time,
1416 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001417 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001418 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001419 }
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001420 } else if (button == BTN_RIGHT && state == 1) {
1421 switch (location) {
1422 default:
1423 input_get_position(input, &x, &y);
1424 window->menu = window_create_menu(window->display,
1425 input, time,
1426 window,
1427 x - 10, y - 10,
1428 window_menu_func,
1429 entries, 4);
1430 window_schedule_redraw(window->menu);
1431 break;
1432 case WINDOW_CLIENT_AREA:
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001433 if (widget && widget->button_handler)
1434 (*widget->button_handler)(widget,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001435 input, time,
1436 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001437 widget->user_data);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001438 break;
1439 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001440 } else {
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001441 if (widget && widget->button_handler)
1442 (*widget->button_handler)(widget,
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001443 input, time,
1444 button, state,
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001445 widget->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001446 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001447
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001448 if (window->focus_widget &&
1449 window->widget_grab_button == button && !state) {
1450 window->widget_grab_button = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001451 widget = widget_find_widget(window->widget,
1452 input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001453 window_set_focus_widget(window, widget, input, time,
1454 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001455 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001456}
1457
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001458static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001459input_handle_key(void *data, struct wl_input_device *input_device,
1460 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001461{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001462 struct input *input = data;
1463 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001464 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001465 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001466
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001467 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001468 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001469 return;
1470
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001471 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001472 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001473 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1474 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001475
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001476 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1477
1478 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001479 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001480 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001481 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001482
1483 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001484 (*window->key_handler)(window, input, time, key, sym, state,
1485 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001486}
1487
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001488static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001489input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001490{
1491 struct window *window = input->pointer_focus;
1492
1493 if (!window)
1494 return;
1495
Kristian Høgsbergee143232012-01-09 08:42:24 -05001496 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001497
Pekka Paalanene1207c72011-12-16 12:02:09 +02001498 input->pointer_focus = NULL;
1499 input->current_pointer_image = POINTER_UNSET;
1500}
1501
1502static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001503input_handle_pointer_focus(void *data,
1504 struct wl_input_device *input_device,
1505 uint32_t time, struct wl_surface *surface,
1506 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001507{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001508 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001509 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001510 struct widget *widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001511 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001512
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001513 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001514 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001515 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001516
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001517 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001518 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001519 window = input->pointer_focus;
1520
Kristian Høgsberg59826582011-01-20 11:56:57 -05001521 input->x = x;
1522 input->y = y;
1523 input->sx = sx;
1524 input->sy = sy;
1525
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001526 pointer = POINTER_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001527 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001528 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001529
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001530 pointer = input_get_pointer_image_for_location(input, pointer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001531 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001532 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001533}
1534
1535static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001536input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001537{
1538 struct window *window = input->keyboard_focus;
1539
1540 if (!window)
1541 return;
1542
1543 window->keyboard_device = NULL;
1544 if (window->keyboard_focus_handler)
1545 (*window->keyboard_focus_handler)(window, NULL,
1546 window->user_data);
1547
1548 input->keyboard_focus = NULL;
1549}
1550
1551static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001552input_handle_keyboard_focus(void *data,
1553 struct wl_input_device *input_device,
1554 uint32_t time,
1555 struct wl_surface *surface,
1556 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001557{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001558 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001559 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001560 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001561 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001562
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001563 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001564
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001565 if (surface)
1566 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001567
1568 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001569 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 for (k = keys->data; k < end; k++)
1571 input->modifiers |= d->xkb->map->modmap[*k];
1572
1573 window = input->keyboard_focus;
1574 if (window) {
1575 window->keyboard_device = input;
1576 if (window->keyboard_focus_handler)
1577 (*window->keyboard_focus_handler)(window,
1578 window->keyboard_device,
1579 window->user_data);
1580 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001581}
1582
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001583static void
1584input_handle_touch_down(void *data,
1585 struct wl_input_device *wl_input_device,
1586 uint32_t time, struct wl_surface *surface,
1587 int32_t id, int32_t x, int32_t y)
1588{
1589}
1590
1591static void
1592input_handle_touch_up(void *data,
1593 struct wl_input_device *wl_input_device,
1594 uint32_t time, int32_t id)
1595{
1596}
1597
1598static void
1599input_handle_touch_motion(void *data,
1600 struct wl_input_device *wl_input_device,
1601 uint32_t time, int32_t id, int32_t x, int32_t y)
1602{
1603}
1604
1605static void
1606input_handle_touch_frame(void *data,
1607 struct wl_input_device *wl_input_device)
1608{
1609}
1610
1611static void
1612input_handle_touch_cancel(void *data,
1613 struct wl_input_device *wl_input_device)
1614{
1615}
1616
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001617static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001618 input_handle_motion,
1619 input_handle_button,
1620 input_handle_key,
1621 input_handle_pointer_focus,
1622 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001623 input_handle_touch_down,
1624 input_handle_touch_up,
1625 input_handle_touch_motion,
1626 input_handle_touch_frame,
1627 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001628};
1629
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001630void
1631input_get_position(struct input *input, int32_t *x, int32_t *y)
1632{
1633 *x = input->sx;
1634 *y = input->sy;
1635}
1636
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001637struct wl_input_device *
1638input_get_input_device(struct input *input)
1639{
1640 return input->input_device;
1641}
1642
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001643uint32_t
1644input_get_modifiers(struct input *input)
1645{
1646 return input->modifiers;
1647}
1648
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001649struct data_offer {
1650 struct wl_data_offer *offer;
1651 struct input *input;
1652 struct wl_array types;
1653 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001654
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001655 struct task io_task;
1656 int fd;
1657 data_func_t func;
1658 int32_t x, y;
1659 void *user_data;
1660};
1661
1662static void
1663data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1664{
1665 struct data_offer *offer = data;
1666 char **p;
1667
1668 p = wl_array_add(&offer->types, sizeof *p);
1669 *p = strdup(type);
1670}
1671
1672static const struct wl_data_offer_listener data_offer_listener = {
1673 data_offer_offer,
1674};
1675
1676static void
1677data_offer_destroy(struct data_offer *offer)
1678{
1679 char **p;
1680
1681 offer->refcount--;
1682 if (offer->refcount == 0) {
1683 wl_data_offer_destroy(offer->offer);
1684 for (p = offer->types.data; *p; p++)
1685 free(*p);
1686 wl_array_release(&offer->types);
1687 free(offer);
1688 }
1689}
1690
1691static void
1692data_device_data_offer(void *data,
1693 struct wl_data_device *data_device, uint32_t id)
1694{
1695 struct data_offer *offer;
1696
1697 offer = malloc(sizeof *offer);
1698
1699 wl_array_init(&offer->types);
1700 offer->refcount = 1;
1701 offer->input = data;
1702
1703 /* FIXME: Generate typesafe wrappers for this */
1704 offer->offer = (struct wl_data_offer *)
1705 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1706 id, &wl_data_offer_interface);
1707
1708 wl_data_offer_add_listener(offer->offer,
1709 &data_offer_listener, offer);
1710}
1711
1712static void
1713data_device_enter(void *data, struct wl_data_device *data_device,
1714 uint32_t time, struct wl_surface *surface,
1715 int32_t x, int32_t y, struct wl_data_offer *offer)
1716{
1717 struct input *input = data;
1718 struct window *window;
1719 char **p;
1720
1721 input->drag_offer = wl_data_offer_get_user_data(offer);
1722 window = wl_surface_get_user_data(surface);
1723 input->pointer_focus = window;
1724
1725 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1726 *p = NULL;
1727
1728 window = input->pointer_focus;
1729 if (window->data_handler)
1730 window->data_handler(window, input, time, x, y,
1731 input->drag_offer->types.data,
1732 window->user_data);
1733}
1734
1735static void
1736data_device_leave(void *data, struct wl_data_device *data_device)
1737{
1738 struct input *input = data;
1739
1740 data_offer_destroy(input->drag_offer);
1741 input->drag_offer = NULL;
1742}
1743
1744static void
1745data_device_motion(void *data, struct wl_data_device *data_device,
1746 uint32_t time, int32_t x, int32_t y)
1747{
1748 struct input *input = data;
1749 struct window *window = input->pointer_focus;
1750
1751 input->sx = x;
1752 input->sy = y;
1753
1754 if (window->data_handler)
1755 window->data_handler(window, input, time, x, y,
1756 input->drag_offer->types.data,
1757 window->user_data);
1758}
1759
1760static void
1761data_device_drop(void *data, struct wl_data_device *data_device)
1762{
1763 struct input *input = data;
1764 struct window *window = input->pointer_focus;
1765
1766 if (window->drop_handler)
1767 window->drop_handler(window, input,
1768 input->sx, input->sy, window->user_data);
1769}
1770
1771static void
1772data_device_selection(void *data,
1773 struct wl_data_device *wl_data_device,
1774 struct wl_data_offer *offer)
1775{
1776 struct input *input = data;
1777 char **p;
1778
1779 if (input->selection_offer)
1780 data_offer_destroy(input->selection_offer);
1781
1782 input->selection_offer = wl_data_offer_get_user_data(offer);
1783 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1784 *p = NULL;
1785}
1786
1787static const struct wl_data_device_listener data_device_listener = {
1788 data_device_data_offer,
1789 data_device_enter,
1790 data_device_leave,
1791 data_device_motion,
1792 data_device_drop,
1793 data_device_selection
1794};
1795
1796struct wl_data_device *
1797input_get_data_device(struct input *input)
1798{
1799 return input->data_device;
1800}
1801
1802void
1803input_set_selection(struct input *input,
1804 struct wl_data_source *source, uint32_t time)
1805{
1806 wl_data_device_set_selection(input->data_device, source, time);
1807}
1808
1809void
1810input_accept(struct input *input, uint32_t time, const char *type)
1811{
1812 wl_data_offer_accept(input->drag_offer->offer, time, type);
1813}
1814
1815static void
1816offer_io_func(struct task *task, uint32_t events)
1817{
1818 struct data_offer *offer =
1819 container_of(task, struct data_offer, io_task);
1820 unsigned int len;
1821 char buffer[4096];
1822
1823 len = read(offer->fd, buffer, sizeof buffer);
1824 offer->func(buffer, len,
1825 offer->x, offer->y, offer->user_data);
1826
1827 if (len == 0) {
1828 close(offer->fd);
1829 data_offer_destroy(offer);
1830 }
1831}
1832
1833static void
1834data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1835 data_func_t func, void *user_data)
1836{
1837 int p[2];
1838
1839 pipe2(p, O_CLOEXEC);
1840 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1841 close(p[1]);
1842
1843 offer->io_task.run = offer_io_func;
1844 offer->fd = p[0];
1845 offer->func = func;
1846 offer->refcount++;
1847 offer->user_data = user_data;
1848
1849 display_watch_fd(offer->input->display,
1850 offer->fd, EPOLLIN, &offer->io_task);
1851}
1852
1853void
1854input_receive_drag_data(struct input *input, const char *mime_type,
1855 data_func_t func, void *data)
1856{
1857 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1858 input->drag_offer->x = input->sx;
1859 input->drag_offer->y = input->sy;
1860}
1861
1862int
1863input_receive_selection_data(struct input *input, const char *mime_type,
1864 data_func_t func, void *data)
1865{
1866 char **p;
1867
1868 if (input->selection_offer == NULL)
1869 return -1;
1870
1871 for (p = input->selection_offer->types.data; *p; p++)
1872 if (strcmp(mime_type, *p) == 0)
1873 break;
1874
1875 if (*p == NULL)
1876 return -1;
1877
1878 data_offer_receive_data(input->selection_offer,
1879 mime_type, func, data);
1880 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001881}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001882
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001883int
1884input_receive_selection_data_to_fd(struct input *input,
1885 const char *mime_type, int fd)
1886{
1887 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1888
1889 return 0;
1890}
1891
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001892void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001893window_move(struct window *window, struct input *input, uint32_t time)
1894{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001895 if (!window->shell_surface)
1896 return;
1897
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001898 wl_shell_surface_move(window->shell_surface,
1899 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001900}
1901
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001902static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001903window_resize(struct window *window, int32_t width, int32_t height)
1904{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001905 struct rectangle allocation;
1906
1907 if (window->decoration) {
1908 allocation.x = 20;
1909 allocation.y = 60;
1910 allocation.width = width - 20 - window->margin * 2;
1911 allocation.height = height - 60 - window->margin * 2;
1912 } else {
1913 allocation.x = 0;
1914 allocation.y = 0;
1915 allocation.width = width;
1916 allocation.height = height;
1917 }
1918
1919 window->allocation.width = width;
1920 window->allocation.height = height;
1921
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001922 widget_set_allocation(window->widget, allocation.x, allocation.y,
1923 allocation.width, allocation.height);
1924
1925 if (window->widget->resize_handler)
1926 window->widget->resize_handler(window->widget,
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001927 allocation.width,
1928 allocation.height,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001929 window->widget->user_data);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001930
1931 window_schedule_redraw(window);
1932}
1933
1934static void
1935idle_resize(struct task *task, uint32_t events)
1936{
1937 struct window *window =
1938 container_of(task, struct window, resize_task);
1939
1940 window_resize(window,
1941 window->allocation.width, window->allocation.height);
1942 window->resize_scheduled = 0;
1943}
1944
1945void
1946window_schedule_resize(struct window *window, int width, int height)
1947{
1948 if (!window->resize_scheduled) {
1949 window->allocation.width = width;
1950 window->allocation.height = height;
1951 window->resize_task.run = idle_resize;
1952 display_defer(window->display, &window->resize_task);
1953 window->resize_scheduled = 1;
1954 }
1955}
1956
1957static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001958handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001959 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001960 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001961{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001962 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001963
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001964 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001965 return;
1966
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001967 window->resize_edges = edges;
1968
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001969 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001970}
1971
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972static void
1973handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1974{
1975 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001976 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001977 /* FIXME: Need more context in this event, at least the input
1978 * device. Or just use wl_callback. */
1979
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05001980 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001981 window_destroy(window);
1982}
1983
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001984static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001985 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001986 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001987};
1988
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001989void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001990window_get_allocation(struct window *window,
1991 struct rectangle *allocation)
1992{
1993 *allocation = window->allocation;
1994}
1995
1996void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001997window_get_child_allocation(struct window *window,
1998 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001999{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002000 if (!window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002001 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002002 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002003 allocation->x = window->margin + 10;
2004 allocation->y = window->margin + 50;
2005 allocation->width =
2006 window->allocation.width - 20 - window->margin * 2;
2007 allocation->height =
2008 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002009 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002010}
2011
2012void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002013window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05002014{
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002015 if (window->decoration) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002016 window->allocation.x = 20 + window->margin;
2017 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002018 window->allocation.width = width + 20 + window->margin * 2;
2019 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002020 } else {
2021 window->allocation.x = 0;
2022 window->allocation.y = 0;
2023 window->allocation.width = width;
2024 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002025 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05002026}
2027
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002028static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002029widget_redraw(struct widget *widget)
2030{
2031 struct widget *child;
2032
2033 if (widget->redraw_handler)
2034 widget->redraw_handler(widget, widget->user_data);
2035 wl_list_for_each(child, &widget->child_list, link)
2036 widget_redraw(child);
2037}
2038
2039static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002040idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002041{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002042 struct window *window =
2043 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002044
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002045 window_create_surface(window);
2046 if (window->decoration)
2047 window_draw_decorations(window);
2048
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002049 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002050
2051 window_flush(window);
2052
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002053 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002054}
2055
2056void
2057window_schedule_redraw(struct window *window)
2058{
2059 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002060 window->redraw_task.run = idle_redraw;
2061 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002062 window->redraw_scheduled = 1;
2063 }
2064}
2065
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002066void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002067window_set_custom(struct window *window)
2068{
2069 window->type = TYPE_CUSTOM;
2070}
2071
2072void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002073window_set_fullscreen(struct window *window, int fullscreen)
2074{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002075 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002076 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002077
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002078 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002079 return;
2080
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002081 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002082 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002083 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002084 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002085 width = output->allocation.width;
2086 height = output->allocation.height;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002087 window->decoration = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002088 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002089 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002090 width = window->saved_allocation.width - 20 - window->margin * 2;
2091 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002092 window->decoration = 1;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002093 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002094
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002095 window_resize(window, width, height);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002096}
2097
2098void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002099window_set_decoration(struct window *window, int decoration)
2100{
2101 window->decoration = decoration;
2102}
2103
2104void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002105window_set_user_data(struct window *window, void *data)
2106{
2107 window->user_data = data;
2108}
2109
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002110void *
2111window_get_user_data(struct window *window)
2112{
2113 return window->user_data;
2114}
2115
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002116void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002117window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002118 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002119{
2120 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002121}
2122
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002123void
2124window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002125 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002126{
2127 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002128}
2129
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002130void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002131window_set_data_handler(struct window *window, window_data_handler_t handler)
2132{
2133 window->data_handler = handler;
2134}
2135
2136void
2137window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2138{
2139 window->drop_handler = handler;
2140}
2141
2142void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002143window_set_close_handler(struct window *window,
2144 window_close_handler_t handler)
2145{
2146 window->close_handler = handler;
2147}
2148
2149void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002150window_set_transparent(struct window *window, int transparent)
2151{
2152 window->transparent = transparent;
2153}
2154
2155void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002156window_set_title(struct window *window, const char *title)
2157{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002158 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002159 window->title = strdup(title);
2160}
2161
2162const char *
2163window_get_title(struct window *window)
2164{
2165 return window->title;
2166}
2167
2168void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002169display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2170 int32_t x, int32_t y, int32_t width, int32_t height)
2171{
2172 struct wl_buffer *buffer;
2173
2174 buffer = display_get_buffer_for_surface(display, cairo_surface);
2175
2176 wl_buffer_damage(buffer, x, y, width, height);
2177}
2178
2179void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002180window_damage(struct window *window, int32_t x, int32_t y,
2181 int32_t width, int32_t height)
2182{
2183 wl_surface_damage(window->surface, x, y, width, height);
2184}
2185
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002186static struct window *
2187window_create_internal(struct display *display, struct window *parent,
2188 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002189{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002190 struct window *window;
2191
2192 window = malloc(sizeof *window);
2193 if (window == NULL)
2194 return NULL;
2195
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002196 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002197 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002198 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002199 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002200 if (display->shell) {
2201 window->shell_surface =
2202 wl_shell_get_shell_surface(display->shell,
2203 window->surface);
2204 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002205 window->allocation.x = 0;
2206 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002207 window->allocation.width = width;
2208 window->allocation.height = height;
2209 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05002210 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002211 window->decoration = 1;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002212 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002213
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002214 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002215#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002216 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2217 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002218#else
2219 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2220#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002221 else
2222 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002223
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002224 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002225 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002226
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002227 if (window->shell_surface) {
2228 wl_shell_surface_set_user_data(window->shell_surface, window);
2229 wl_shell_surface_add_listener(window->shell_surface,
2230 &shell_surface_listener, window);
2231 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002232
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002233 return window;
2234}
2235
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002236struct window *
2237window_create(struct display *display, int32_t width, int32_t height)
2238{
2239 struct window *window;
2240
2241 window = window_create_internal(display, NULL, width, height);
2242 if (!window)
2243 return NULL;
2244
2245 return window;
2246}
2247
2248struct window *
2249window_create_transient(struct display *display, struct window *parent,
2250 int32_t x, int32_t y, int32_t width, int32_t height)
2251{
2252 struct window *window;
2253
2254 window = window_create_internal(parent->display,
2255 parent, width, height);
2256 if (!window)
2257 return NULL;
2258
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002259 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002260 window->x = x;
2261 window->y = y;
2262
2263 return window;
2264}
2265
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002266static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002267menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002268{
2269 int next;
2270
2271 next = (sy - 8) / 20;
2272 if (menu->current != next) {
2273 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002274 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002275 }
2276
2277 return POINTER_LEFT_PTR;
2278}
2279
2280static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002281menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002282 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002283 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002284{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002285 return menu_set_item(data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002286}
2287
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002288static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002289menu_enter_handler(struct widget *widget,
2290 struct input *input, uint32_t time,
2291 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002292{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002293 return menu_set_item(data, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002294}
2295
2296static void
2297menu_leave_handler(struct widget *widget, struct input *input, void *data)
2298{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002299 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002300}
2301
2302static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002303menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002304 struct input *input, uint32_t time,
2305 int button, int state, void *data)
2306
2307{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002308 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002309
2310 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002311 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002312 menu->func(menu->window->parent,
2313 menu->current, menu->window->parent->user_data);
2314 window_destroy(widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002315 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002316}
2317
2318static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002319menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002320{
2321 cairo_t *cr;
2322 const int32_t r = 3, margin = 3;
2323 struct menu *menu = data;
2324 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002325 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002326
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002327 cr = cairo_create(window->cairo_surface);
2328 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2329 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2330 cairo_paint(cr);
2331
2332 width = window->allocation.width;
2333 height = window->allocation.height;
2334 rounded_rect(cr, 0, 0, width, height, r);
2335 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2336 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2337 cairo_fill(cr);
2338
2339 for (i = 0; i < menu->count; i++) {
2340 if (i == menu->current) {
2341 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2342 cairo_rectangle(cr, margin, i * 20 + margin,
2343 width - 2 * margin, 20);
2344 cairo_fill(cr);
2345 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2346 cairo_move_to(cr, 10, i * 20 + 16);
2347 cairo_show_text(cr, menu->entries[i]);
2348 } else {
2349 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2350 cairo_move_to(cr, 10, i * 20 + 16);
2351 cairo_show_text(cr, menu->entries[i]);
2352 }
2353 }
2354
2355 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002356}
2357
2358struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002359window_create_menu(struct display *display,
2360 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002361 int32_t x, int32_t y,
2362 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002363{
2364 struct window *window;
2365 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002366 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002367
2368 menu = malloc(sizeof *menu);
2369 if (!menu)
2370 return NULL;
2371
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002372 window = window_create_internal(parent->display, parent,
2373 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002374 if (!window)
2375 return NULL;
2376
2377 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002378 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002379 menu->entries = entries;
2380 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002381 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002382 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002383 window->decoration = 0;
2384 window->type = TYPE_MENU;
2385 window->x = x;
2386 window->y = y;
2387
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002388 wl_shell_surface_set_popup(window->shell_surface,
2389 input->input_device, time,
2390 window->parent->shell_surface,
2391 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002392
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002393 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002394 widget_set_enter_handler(menu->widget, menu_enter_handler);
2395 widget_set_leave_handler(menu->widget, menu_leave_handler);
2396 widget_set_motion_handler(menu->widget, menu_motion_handler);
2397 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002398
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002399 return window;
2400}
2401
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002402void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002403window_set_buffer_type(struct window *window, enum window_buffer_type type)
2404{
2405 window->buffer_type = type;
2406}
2407
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002408
2409static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002410display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002411 struct wl_output *wl_output,
2412 int x, int y,
2413 int physical_width,
2414 int physical_height,
2415 int subpixel,
2416 const char *make,
2417 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002418{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002419 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002420
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002421 output->allocation.x = x;
2422 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002423}
2424
2425static void
2426display_handle_mode(void *data,
2427 struct wl_output *wl_output,
2428 uint32_t flags,
2429 int width,
2430 int height,
2431 int refresh)
2432{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002433 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002434 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002435
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002436 if (flags & WL_OUTPUT_MODE_CURRENT) {
2437 output->allocation.width = width;
2438 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002439 if (display->output_configure_handler)
2440 (*display->output_configure_handler)(
2441 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002442 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002443}
2444
2445static const struct wl_output_listener output_listener = {
2446 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002447 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002448};
2449
2450static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002451display_add_output(struct display *d, uint32_t id)
2452{
2453 struct output *output;
2454
2455 output = malloc(sizeof *output);
2456 if (output == NULL)
2457 return;
2458
2459 memset(output, 0, sizeof *output);
2460 output->display = d;
2461 output->output =
2462 wl_display_bind(d->display, id, &wl_output_interface);
2463 wl_list_insert(d->output_list.prev, &output->link);
2464
2465 wl_output_add_listener(output->output, &output_listener, output);
2466}
2467
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002468static void
2469output_destroy(struct output *output)
2470{
2471 if (output->destroy_handler)
2472 (*output->destroy_handler)(output, output->user_data);
2473
2474 wl_output_destroy(output->output);
2475 wl_list_remove(&output->link);
2476 free(output);
2477}
2478
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002479void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002480display_set_output_configure_handler(struct display *display,
2481 display_output_handler_t handler)
2482{
2483 struct output *output;
2484
2485 display->output_configure_handler = handler;
2486 if (!handler)
2487 return;
2488
2489 wl_list_for_each(output, &display->output_list, link)
2490 (*display->output_configure_handler)(output,
2491 display->user_data);
2492}
2493
2494void
2495output_set_user_data(struct output *output, void *data)
2496{
2497 output->user_data = data;
2498}
2499
2500void *
2501output_get_user_data(struct output *output)
2502{
2503 return output->user_data;
2504}
2505
2506void
2507output_set_destroy_handler(struct output *output,
2508 display_output_handler_t handler)
2509{
2510 output->destroy_handler = handler;
2511 /* FIXME: implement this, once we have way to remove outputs */
2512}
2513
2514void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002515output_get_allocation(struct output *output, struct rectangle *allocation)
2516{
2517 *allocation = output->allocation;
2518}
2519
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002520struct wl_output *
2521output_get_wl_output(struct output *output)
2522{
2523 return output->output;
2524}
2525
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002526static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002527display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002528{
2529 struct input *input;
2530
2531 input = malloc(sizeof *input);
2532 if (input == NULL)
2533 return;
2534
2535 memset(input, 0, sizeof *input);
2536 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002537 input->input_device =
2538 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002539 input->pointer_focus = NULL;
2540 input->keyboard_focus = NULL;
2541 wl_list_insert(d->input_list.prev, &input->link);
2542
2543 wl_input_device_add_listener(input->input_device,
2544 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002545 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002546
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002547 input->data_device =
2548 wl_data_device_manager_get_data_device(d->data_device_manager,
2549 input->input_device);
2550 wl_data_device_add_listener(input->data_device,
2551 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002552}
2553
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002554static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002555input_destroy(struct input *input)
2556{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002557 input_remove_keyboard_focus(input);
2558 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002559
2560 if (input->drag_offer)
2561 data_offer_destroy(input->drag_offer);
2562
2563 if (input->selection_offer)
2564 data_offer_destroy(input->selection_offer);
2565
2566 wl_data_device_destroy(input->data_device);
2567 wl_list_remove(&input->link);
2568 wl_input_device_destroy(input->input_device);
2569 free(input);
2570}
2571
2572static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002573display_handle_global(struct wl_display *display, uint32_t id,
2574 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002575{
2576 struct display *d = data;
2577
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002578 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002579 d->compositor =
2580 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002581 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002582 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002583 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002584 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002585 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002586 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002587 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002588 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002589 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2590 d->data_device_manager =
2591 wl_display_bind(display, id,
2592 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002593 }
2594}
2595
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002596static void
2597display_render_frame(struct display *d)
2598{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002599 int radius = 8;
2600 cairo_t *cr;
2601
2602 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2603 cr = cairo_create(d->shadow);
2604 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2605 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2606 rounded_rect(cr, 16, 16, 112, 112, radius);
2607 cairo_fill(cr);
2608 cairo_destroy(cr);
2609 blur_surface(d->shadow, 64);
2610
2611 d->active_frame =
2612 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2613 cr = cairo_create(d->active_frame);
2614 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2615 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2616 rounded_rect(cr, 16, 16, 112, 112, radius);
2617 cairo_fill(cr);
2618 cairo_destroy(cr);
2619
2620 d->inactive_frame =
2621 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2622 cr = cairo_create(d->inactive_frame);
2623 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2624 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2625 rounded_rect(cr, 16, 16, 112, 112, radius);
2626 cairo_fill(cr);
2627 cairo_destroy(cr);
2628}
2629
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002630static void
2631init_xkb(struct display *d)
2632{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002633 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002634
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002635 names.rules = "evdev";
2636 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002637 names.layout = option_xkb_layout;
2638 names.variant = option_xkb_variant;
2639 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002640
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002641 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002642 if (!d->xkb) {
2643 fprintf(stderr, "Failed to compile keymap\n");
2644 exit(1);
2645 }
2646}
2647
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002648static void
2649fini_xkb(struct display *display)
2650{
2651 xkb_free_keymap(display->xkb);
2652}
2653
Yuval Fledel45568f62010-12-06 09:18:12 -05002654static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002655init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002656{
2657 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002658 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002659
2660 static const EGLint premul_argb_cfg_attribs[] = {
2661 EGL_SURFACE_TYPE,
2662 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2663 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002664 EGL_RED_SIZE, 1,
2665 EGL_GREEN_SIZE, 1,
2666 EGL_BLUE_SIZE, 1,
2667 EGL_ALPHA_SIZE, 1,
2668 EGL_DEPTH_SIZE, 1,
2669 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2670 EGL_NONE
2671 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002672
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002673 static const EGLint rgb_cfg_attribs[] = {
2674 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2675 EGL_RED_SIZE, 1,
2676 EGL_GREEN_SIZE, 1,
2677 EGL_BLUE_SIZE, 1,
2678 EGL_ALPHA_SIZE, 0,
2679 EGL_DEPTH_SIZE, 1,
2680 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2681 EGL_NONE
2682 };
2683
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002684 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002685 if (!eglInitialize(d->dpy, &major, &minor)) {
2686 fprintf(stderr, "failed to initialize display\n");
2687 return -1;
2688 }
2689
2690 if (!eglBindAPI(EGL_OPENGL_API)) {
2691 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2692 return -1;
2693 }
2694
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002695 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002696 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002697 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002698 return -1;
2699 }
2700
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002701 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2702 &d->rgb_config, 1, &n) || n != 1) {
2703 fprintf(stderr, "failed to choose rgb config\n");
2704 return -1;
2705 }
2706
Benjamin Franzke0c991632011-09-27 21:57:31 +02002707 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2708 if (d->rgb_ctx == NULL) {
2709 fprintf(stderr, "failed to create context\n");
2710 return -1;
2711 }
2712 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2713 EGL_NO_CONTEXT, NULL);
2714 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002715 fprintf(stderr, "failed to create context\n");
2716 return -1;
2717 }
2718
Benjamin Franzke0c991632011-09-27 21:57:31 +02002719 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002720 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002721 return -1;
2722 }
2723
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002724#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002725 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2726 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002727 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002728 return -1;
2729 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002730 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2731 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2732 fprintf(stderr, "failed to get cairo egl argb device\n");
2733 return -1;
2734 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002735#endif
2736
2737 return 0;
2738}
2739
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002740static void
2741fini_egl(struct display *display)
2742{
2743#ifdef HAVE_CAIRO_EGL
2744 cairo_device_destroy(display->argb_device);
2745 cairo_device_destroy(display->rgb_device);
2746#endif
2747
2748 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2749 EGL_NO_CONTEXT);
2750
2751 eglTerminate(display->dpy);
2752 eglReleaseThread();
2753}
2754
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002755static int
2756event_mask_update(uint32_t mask, void *data)
2757{
2758 struct display *d = data;
2759
2760 d->mask = mask;
2761
2762 return 0;
2763}
2764
2765static void
2766handle_display_data(struct task *task, uint32_t events)
2767{
2768 struct display *display =
2769 container_of(task, struct display, display_task);
2770
2771 wl_display_iterate(display->display, display->mask);
2772}
2773
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002774struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002775display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002776{
2777 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002778 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002779 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002780 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002781
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002782 g_type_init();
2783
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002784 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002785 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002786 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002787
2788 xkb_option_group = g_option_group_new("xkb",
2789 "Keyboard options",
2790 "Show all XKB options",
2791 NULL, NULL);
2792 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2793 g_option_context_add_group (context, xkb_option_group);
2794
2795 if (!g_option_context_parse(context, argc, argv, &error)) {
2796 fprintf(stderr, "option parsing failed: %s\n", error->message);
2797 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002798 }
2799
Tim Wiederhake748f6722011-01-23 23:25:25 +01002800 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002801
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002802 d = malloc(sizeof *d);
2803 if (d == NULL)
2804 return NULL;
2805
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002806 memset(d, 0, sizeof *d);
2807
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002808 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002809 if (d->display == NULL) {
2810 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002811 return NULL;
2812 }
2813
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002814 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2815 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2816 d->display_task.run = handle_display_data;
2817 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2818
2819 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002820 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002821 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002822
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002823 /* Set up listener so we'll catch all events. */
2824 wl_display_add_global_listener(d->display,
2825 display_handle_global, d);
2826
2827 /* Process connection events. */
2828 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002829 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002830 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002831
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002832 d->image_target_texture_2d =
2833 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2834 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2835 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2836
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002837 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002838
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002839 display_render_frame(d);
2840
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002841 wl_list_init(&d->window_list);
2842
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002843 init_xkb(d);
2844
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002845 return d;
2846}
2847
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002848static void
2849display_destroy_outputs(struct display *display)
2850{
2851 struct output *tmp;
2852 struct output *output;
2853
2854 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2855 output_destroy(output);
2856}
2857
Pekka Paalanene1207c72011-12-16 12:02:09 +02002858static void
2859display_destroy_inputs(struct display *display)
2860{
2861 struct input *tmp;
2862 struct input *input;
2863
2864 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2865 input_destroy(input);
2866}
2867
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002868void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002869display_destroy(struct display *display)
2870{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002871 if (!wl_list_empty(&display->window_list))
2872 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2873
2874 if (!wl_list_empty(&display->deferred_list))
2875 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2876
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002877 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002878 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002879
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002880 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002881
2882 cairo_surface_destroy(display->active_frame);
2883 cairo_surface_destroy(display->inactive_frame);
2884 cairo_surface_destroy(display->shadow);
2885 destroy_pointer_surfaces(display);
2886
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002887 fini_egl(display);
2888
Pekka Paalanenc2052982011-12-16 11:41:32 +02002889 if (display->shell)
2890 wl_shell_destroy(display->shell);
2891
2892 if (display->shm)
2893 wl_shm_destroy(display->shm);
2894
2895 if (display->data_device_manager)
2896 wl_data_device_manager_destroy(display->data_device_manager);
2897
2898 wl_compositor_destroy(display->compositor);
2899
2900 close(display->epoll_fd);
2901
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002902 wl_display_flush(display->display);
2903 wl_display_destroy(display->display);
2904 free(display);
2905}
2906
2907void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002908display_set_user_data(struct display *display, void *data)
2909{
2910 display->user_data = data;
2911}
2912
2913void *
2914display_get_user_data(struct display *display)
2915{
2916 return display->user_data;
2917}
2918
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002919struct wl_display *
2920display_get_display(struct display *display)
2921{
2922 return display->display;
2923}
2924
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002925struct output *
2926display_get_output(struct display *display)
2927{
2928 return container_of(display->output_list.next, struct output, link);
2929}
2930
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002931struct wl_compositor *
2932display_get_compositor(struct display *display)
2933{
2934 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002935}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002936
2937EGLDisplay
2938display_get_egl_display(struct display *d)
2939{
2940 return d->dpy;
2941}
2942
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002943struct wl_data_source *
2944display_create_data_source(struct display *display)
2945{
2946 return wl_data_device_manager_create_data_source(display->data_device_manager);
2947}
2948
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002949EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002950display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002951{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002952 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002953}
2954
Benjamin Franzke0c991632011-09-27 21:57:31 +02002955EGLConfig
2956display_get_argb_egl_config(struct display *d)
2957{
2958 return d->premultiplied_argb_config;
2959}
2960
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002961struct wl_shell *
2962display_get_shell(struct display *display)
2963{
2964 return display->shell;
2965}
2966
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002967int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002968display_acquire_window_surface(struct display *display,
2969 struct window *window,
2970 EGLContext ctx)
2971{
Benjamin Franzke22d54812011-07-16 19:50:32 +00002972#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002973 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002974 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002975
2976 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002977 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02002978 device = cairo_surface_get_device(window->cairo_surface);
2979 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002980 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002981
Benjamin Franzke0c991632011-09-27 21:57:31 +02002982 if (!ctx) {
2983 if (device == display->rgb_device)
2984 ctx = display->rgb_ctx;
2985 else if (device == display->argb_device)
2986 ctx = display->argb_ctx;
2987 else
2988 assert(0);
2989 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002990
2991 data = cairo_surface_get_user_data(window->cairo_surface,
2992 &surface_data_key);
2993
Pekka Paalanen9015ead2011-12-19 13:57:59 +02002994 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002995 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002996 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2997 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002998
2999 return 0;
3000#else
3001 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003002#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003003}
3004
3005void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003006display_release_window_surface(struct display *display,
3007 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003008{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003009#ifdef HAVE_CAIRO_EGL
3010 cairo_device_t *device;
3011
3012 device = cairo_surface_get_device(window->cairo_surface);
3013 if (!device)
3014 return;
3015
3016 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003017 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003018 cairo_device_release(device);
3019#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003020}
3021
3022void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003023display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003024{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003025 wl_list_insert(&display->deferred_list, &task->link);
3026}
3027
3028void
3029display_watch_fd(struct display *display,
3030 int fd, uint32_t events, struct task *task)
3031{
3032 struct epoll_event ep;
3033
3034 ep.events = events;
3035 ep.data.ptr = task;
3036 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3037}
3038
3039void
3040display_run(struct display *display)
3041{
3042 struct task *task;
3043 struct epoll_event ep[16];
3044 int i, count;
3045
Pekka Paalanen826d7952011-12-15 10:14:07 +02003046 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003047 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003048 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003049
3050 while (!wl_list_empty(&display->deferred_list)) {
3051 task = container_of(display->deferred_list.next,
3052 struct task, link);
3053 wl_list_remove(&task->link);
3054 task->run(task, 0);
3055 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003056
3057 if (!display->running)
3058 break;
3059
3060 wl_display_flush(display->display);
3061
3062 count = epoll_wait(display->epoll_fd,
3063 ep, ARRAY_LENGTH(ep), -1);
3064 for (i = 0; i < count; i++) {
3065 task = ep[i].data.ptr;
3066 task->run(task, ep[i].events);
3067 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003068 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003069}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003070
3071void
3072display_exit(struct display *display)
3073{
3074 display->running = 0;
3075}