blob: 9d575c2c45c6e12c3bfad7ad8c11c059218617d6 [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øgsberg0c29eb22011-09-06 18:02:34 -0400121 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400122 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400123 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400125 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500126
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500127 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500128
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500129 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500130 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400131 window_data_handler_t data_handler;
132 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500133 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400134
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500135 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500136 struct window *menu;
137
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500138 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400139 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500140};
141
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500142struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500143 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500144 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400145 struct wl_list link;
146 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500147 widget_resize_handler_t resize_handler;
148 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500149 widget_enter_handler_t enter_handler;
150 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500151 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500152 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400153 void *user_data;
154};
155
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400156struct input {
157 struct display *display;
158 struct wl_input_device *input_device;
159 struct window *pointer_focus;
160 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400161 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400162 uint32_t modifiers;
163 int32_t x, y, sx, sy;
164 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400165
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500166 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500167 struct widget *grab;
168 uint32_t grab_button;
169
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400170 struct wl_data_device *data_device;
171 struct data_offer *drag_offer;
172 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400173};
174
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500175struct output {
176 struct display *display;
177 struct wl_output *output;
178 struct rectangle allocation;
179 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200180
181 display_output_handler_t destroy_handler;
182 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500183};
184
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500185struct frame {
186 struct widget *widget;
187 struct widget *child;
188 int margin;
189};
190
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500191struct menu {
192 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500193 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500194 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500195 const char **entries;
196 uint32_t time;
197 int current;
198 int count;
199 menu_func_t func;
200};
201
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400202enum {
203 POINTER_DEFAULT = 100,
204 POINTER_UNSET
205};
206
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500207enum window_location {
208 WINDOW_INTERIOR = 0,
209 WINDOW_RESIZING_TOP = 1,
210 WINDOW_RESIZING_BOTTOM = 2,
211 WINDOW_RESIZING_LEFT = 4,
212 WINDOW_RESIZING_TOP_LEFT = 5,
213 WINDOW_RESIZING_BOTTOM_LEFT = 6,
214 WINDOW_RESIZING_RIGHT = 8,
215 WINDOW_RESIZING_TOP_RIGHT = 9,
216 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
217 WINDOW_RESIZING_MASK = 15,
218 WINDOW_EXTERIOR = 16,
219 WINDOW_TITLEBAR = 17,
220 WINDOW_CLIENT_AREA = 18,
221};
222
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400223const char *option_xkb_layout = "us";
224const char *option_xkb_variant = "";
225const char *option_xkb_options = "";
226
227static const GOptionEntry xkb_option_entries[] = {
228 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
229 &option_xkb_layout, "XKB Layout" },
230 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_variant, "XKB Variant" },
232 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
233 &option_xkb_options, "XKB Options" },
234 { NULL }
235};
236
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400237static const cairo_user_data_key_t surface_data_key;
238struct surface_data {
239 struct wl_buffer *buffer;
240};
241
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500242#define MULT(_d,c,a,t) \
243 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
244
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500245#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400246
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100247struct egl_window_surface_data {
248 struct display *display;
249 struct wl_surface *surface;
250 struct wl_egl_window *window;
251 EGLSurface surf;
252};
253
254static void
255egl_window_surface_data_destroy(void *p)
256{
257 struct egl_window_surface_data *data = p;
258 struct display *d = data->display;
259
260 eglDestroySurface(d->dpy, data->surf);
261 wl_egl_window_destroy(data->window);
262 data->surface = NULL;
263
264 free(p);
265}
266
267static cairo_surface_t *
268display_create_egl_window_surface(struct display *display,
269 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400270 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100271 struct rectangle *rectangle)
272{
273 cairo_surface_t *cairo_surface;
274 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400275 EGLConfig config;
276 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200277 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100278
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400279 static const EGLint premul_attribs[] = {
280 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
281 EGL_NONE
282 };
283
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100284 data = malloc(sizeof *data);
285 if (data == NULL)
286 return NULL;
287
288 data->display = display;
289 data->surface = surface;
290
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400291 if (flags & SURFACE_OPAQUE) {
292 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200293 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200294 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 } else {
296 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200297 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400298 attribs = premul_attribs;
299 }
300
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400301 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100304
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400305 data->surf = eglCreateWindowSurface(display->dpy, config,
306 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100307
Benjamin Franzke0c991632011-09-27 21:57:31 +0200308 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100309 data->surf,
310 rectangle->width,
311 rectangle->height);
312
313 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
314 data, egl_window_surface_data_destroy);
315
316 return cairo_surface;
317}
318
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200321 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400322 EGLImageKHR image;
323 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800324 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500325 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326};
327
328static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400330{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800332 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333
Benjamin Franzke0c991632011-09-27 21:57:31 +0200334 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400335 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200336 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800337
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500338 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500340 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500341 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400342}
343
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500344EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345display_get_image_for_egl_image_surface(struct display *display,
346 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500347{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500349
350 data = cairo_surface_get_user_data (surface, &surface_data_key);
351
352 return data->image;
353}
354
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500355static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500356display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400357 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500358 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500360 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361 EGLDisplay dpy = display->dpy;
362 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200363 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400364
365 data = malloc(sizeof *data);
366 if (data == NULL)
367 return NULL;
368
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800369 data->display = display;
370
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400371 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400372 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500373 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000374 free(data);
375 return NULL;
376 }
377
Benjamin Franzke0c991632011-09-27 21:57:31 +0200378 if (flags & SURFACE_OPAQUE) {
379 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200380 content = CAIRO_CONTENT_COLOR;
381 } else {
382 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200383 content = CAIRO_CONTENT_COLOR_ALPHA;
384 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400385
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500386 data->image = display->create_image(dpy, NULL,
387 EGL_NATIVE_PIXMAP_KHR,
388 (EGLClientBuffer) data->pixmap,
389 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500391 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500392 free(data);
393 return NULL;
394 }
395
396 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400397 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500398
Benjamin Franzke0c991632011-09-27 21:57:31 +0200399 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500402 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200403 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404
Benjamin Franzke0c991632011-09-27 21:57:31 +0200405 surface = cairo_gl_surface_create_for_texture(data->device,
406 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400407 data->texture,
408 rectangle->width,
409 rectangle->height);
410
411 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500412 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413
414 return surface;
415}
416
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500417static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500418display_create_egl_image_surface_from_file(struct display *display,
419 const char *filename,
420 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421{
422 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400423 GdkPixbuf *pixbuf;
424 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425 int stride, i;
426 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500427 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428
429 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431 FALSE, &error);
432 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400433 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400434
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400435 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
436 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500437 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400440
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441
442 stride = gdk_pixbuf_get_rowstride(pixbuf);
443 pixels = gdk_pixbuf_get_pixels(pixbuf);
444
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400446 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400447 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400448 while (p < end) {
449 unsigned int t;
450
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400451 MULT(p[0], p[0], p[3], t);
452 MULT(p[1], p[1], p[3], t);
453 MULT(p[2], p[2], p[3], t);
454 p += 4;
455
456 }
457 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400458
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200459 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000460 if (surface == NULL) {
461 g_object_unref(pixbuf);
462 return NULL;
463 }
464
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800465 data = cairo_surface_get_user_data(surface, &surface_data_key);
466
Benjamin Franzke0c991632011-09-27 21:57:31 +0200467 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800468 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800469 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
470 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200471 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400472
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500473 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400474
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400475 return surface;
476}
477
478#endif
479
480struct wl_buffer *
481display_get_buffer_for_surface(struct display *display,
482 cairo_surface_t *surface)
483{
484 struct surface_data *data;
485
486 data = cairo_surface_get_user_data (surface, &surface_data_key);
487
488 return data->buffer;
489}
490
491struct shm_surface_data {
492 struct surface_data data;
493 void *map;
494 size_t length;
495};
496
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500497static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498shm_surface_data_destroy(void *p)
499{
500 struct shm_surface_data *data = p;
501
502 wl_buffer_destroy(data->data.buffer);
503 munmap(data->map, data->length);
504}
505
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500506static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400508 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400509{
510 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400511 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400512 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800513 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400514 char filename[] = "/tmp/wayland-shm-XXXXXX";
515
516 data = malloc(sizeof *data);
517 if (data == NULL)
518 return NULL;
519
520 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
521 rectangle->width);
522 data->length = stride * rectangle->height;
523 fd = mkstemp(filename);
524 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000525 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400526 return NULL;
527 }
528 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000529 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 close(fd);
531 return NULL;
532 }
533
534 data->map = mmap(NULL, data->length,
535 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
536 unlink(filename);
537
538 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000539 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400540 close(fd);
541 return NULL;
542 }
543
544 surface = cairo_image_surface_create_for_data (data->map,
545 CAIRO_FORMAT_ARGB32,
546 rectangle->width,
547 rectangle->height,
548 stride);
549
550 cairo_surface_set_user_data (surface, &surface_data_key,
551 data, shm_surface_data_destroy);
552
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400554 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400556 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400557
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400558 data->data.buffer = wl_shm_create_buffer(display->shm,
559 fd,
560 rectangle->width,
561 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400562 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563
564 close(fd);
565
566 return surface;
567}
568
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500569static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400570display_create_shm_surface_from_file(struct display *display,
571 const char *filename,
572 struct rectangle *rect)
573{
574 cairo_surface_t *surface;
575 GdkPixbuf *pixbuf;
576 GError *error = NULL;
577 int stride, i;
578 unsigned char *pixels, *p, *end, *dest_data;
579 int dest_stride;
580 uint32_t *d;
581
582 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
583 rect->width, rect->height,
584 FALSE, &error);
585 if (error != NULL)
586 return NULL;
587
588 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
589 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500590 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400591 return NULL;
592 }
593
594 stride = gdk_pixbuf_get_rowstride(pixbuf);
595 pixels = gdk_pixbuf_get_pixels(pixbuf);
596
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400597 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000598 if (surface == NULL) {
599 g_object_unref(pixbuf);
600 return NULL;
601 }
602
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400603 dest_data = cairo_image_surface_get_data (surface);
604 dest_stride = cairo_image_surface_get_stride (surface);
605
606 for (i = 0; i < rect->height; i++) {
607 d = (uint32_t *) (dest_data + i * dest_stride);
608 p = pixels + i * stride;
609 end = p + rect->width * 4;
610 while (p < end) {
611 unsigned int t;
612 unsigned char a, r, g, b;
613
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614 a = p[3];
615 MULT(r, p[0], a, t);
616 MULT(g, p[1], a, t);
617 MULT(b, p[2], a, t);
618 p += 4;
619 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
620 }
621 }
622
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500623 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400624
625 return surface;
626}
627
nobled7b87cb02011-02-01 18:51:47 +0000628static int
629check_size(struct rectangle *rect)
630{
631 if (rect->width && rect->height)
632 return 0;
633
634 fprintf(stderr, "tried to create surface of "
635 "width: %d, height: %d\n", rect->width, rect->height);
636 return -1;
637}
638
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400639cairo_surface_t *
640display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400642 struct rectangle *rectangle,
643 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400644{
nobled7b87cb02011-02-01 18:51:47 +0000645 if (check_size(rectangle) < 0)
646 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500647#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500648 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649 if (surface)
650 return display_create_egl_window_surface(display,
651 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400652 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100653 rectangle);
654 else
655 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400656 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100657 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500658 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400659#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400660 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400661}
662
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500663static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400664display_create_surface_from_file(struct display *display,
665 const char *filename,
666 struct rectangle *rectangle)
667{
nobled7b87cb02011-02-01 18:51:47 +0000668 if (check_size(rectangle) < 0)
669 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500670#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500671 if (display->dpy) {
672 return display_create_egl_image_surface_from_file(display,
673 filename,
674 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500675 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500677 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678}
Yuval Fledel45568f62010-12-06 09:18:12 -0500679 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400680 const char *filename;
681 int hotspot_x, hotspot_y;
682} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500683 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
684 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
685 { DATADIR "/weston/bottom_side.png", 16, 20 },
686 { DATADIR "/weston/grabbing.png", 20, 17 },
687 { DATADIR "/weston/left_ptr.png", 10, 5 },
688 { DATADIR "/weston/left_side.png", 10, 20 },
689 { DATADIR "/weston/right_side.png", 30, 19 },
690 { DATADIR "/weston/top_left_corner.png", 8, 8 },
691 { DATADIR "/weston/top_right_corner.png", 26, 8 },
692 { DATADIR "/weston/top_side.png", 18, 8 },
693 { DATADIR "/weston/xterm.png", 15, 15 },
694 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695};
696
697static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400698create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699{
700 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400703
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400704 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400705 display->pointer_surfaces =
706 malloc(count * sizeof *display->pointer_surfaces);
707 rect.width = width;
708 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400710 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711 display_create_surface_from_file(display,
712 pointer_images[i].filename,
713 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100714 if (!display->pointer_surfaces[i]) {
715 fprintf(stderr, "Error loading pointer image: %s\n",
716 pointer_images[i].filename);
717 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400718 }
719
720}
721
Pekka Paalanen325bb602011-12-19 10:31:45 +0200722static void
723destroy_pointer_surfaces(struct display *display)
724{
725 int i, count;
726
727 count = ARRAY_LENGTH(pointer_images);
728 for (i = 0; i < count; ++i) {
729 if (display->pointer_surfaces[i])
730 cairo_surface_destroy(display->pointer_surfaces[i]);
731 }
732 free(display->pointer_surfaces);
733}
734
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735cairo_surface_t *
736display_get_pointer_surface(struct display *display, int pointer,
737 int *width, int *height,
738 int *hotspot_x, int *hotspot_y)
739{
740 cairo_surface_t *surface;
741
742 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500743#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400744 *width = cairo_gl_surface_get_width(surface);
745 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000746#else
747 *width = cairo_image_surface_get_width(surface);
748 *height = cairo_image_surface_get_height(surface);
749#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400750 *hotspot_x = pointer_images[pointer].hotspot_x;
751 *hotspot_y = pointer_images[pointer].hotspot_y;
752
753 return cairo_surface_reference(surface);
754}
755
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756static void
757window_attach_surface(struct window *window);
758
759static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400760free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400761{
762 struct window *window = data;
763
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400764 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400765 cairo_surface_destroy(window->pending_surface);
766 window->pending_surface = NULL;
767 if (window->cairo_surface)
768 window_attach_surface(window);
769}
770
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400771static const struct wl_callback_listener free_surface_listener = {
772 free_surface
773};
774
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500775static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200776window_get_resize_dx_dy(struct window *window, int *x, int *y)
777{
778 if (window->resize_edges & WINDOW_RESIZING_LEFT)
779 *x = window->server_allocation.width - window->allocation.width;
780 else
781 *x = 0;
782
783 if (window->resize_edges & WINDOW_RESIZING_TOP)
784 *y = window->server_allocation.height -
785 window->allocation.height;
786 else
787 *y = 0;
788
789 window->resize_edges = 0;
790}
791
792static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400793window_set_type(struct window *window)
794{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200795 if (!window->shell_surface)
796 return;
797
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400798 switch (window->type) {
799 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200800 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400801 break;
802 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200803 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400804 break;
805 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200806 wl_shell_surface_set_transient(window->shell_surface,
807 window->parent->shell_surface,
808 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400809 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500810 case TYPE_MENU:
811 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400812 case TYPE_CUSTOM:
813 break;
814 }
815}
816
817static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500818window_attach_surface(struct window *window)
819{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400820 struct display *display = window->display;
821 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400822 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000825#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500826 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400828 if (display->shell)
829 window_set_type(window);
830
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000832#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
834 data = cairo_surface_get_user_data(window->cairo_surface,
835 &surface_data_key);
836
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 cairo_gl_surface_swapbuffers(window->cairo_surface);
838 wl_egl_window_get_attached_size(data->window,
839 &window->server_allocation.width,
840 &window->server_allocation.height);
841 break;
842 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000843#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100844 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200845 window_get_resize_dx_dy(window, &x, &y);
846
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 if (window->pending_surface != NULL)
848 return;
849
850 window->pending_surface = window->cairo_surface;
851 window->cairo_surface = NULL;
852
853 buffer =
854 display_get_buffer_for_surface(display,
855 window->pending_surface);
856
857 wl_surface_attach(window->surface, buffer, x, y);
858 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400859 cb = wl_display_sync(display->display);
860 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100861 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000862 default:
863 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100864 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500865
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500866 wl_surface_damage(window->surface, 0, 0,
867 window->allocation.width,
868 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500869}
870
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400872window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500873{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100874 if (window->cairo_surface) {
875 switch (window->buffer_type) {
876 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
877 case WINDOW_BUFFER_TYPE_SHM:
878 display_surface_damage(window->display,
879 window->cairo_surface,
880 0, 0,
881 window->allocation.width,
882 window->allocation.height);
883 break;
884 default:
885 break;
886 }
887 window_attach_surface(window);
888 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500889}
890
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891void
892window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400893{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500894 cairo_surface_reference(surface);
895
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 if (window->cairo_surface != NULL)
897 cairo_surface_destroy(window->cairo_surface);
898
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500899 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400900}
901
Benjamin Franzke22d54812011-07-16 19:50:32 +0000902#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903static void
904window_resize_cairo_window_surface(struct window *window)
905{
906 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200907 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908
909 data = cairo_surface_get_user_data(window->cairo_surface,
910 &surface_data_key);
911
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200912 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913 wl_egl_window_resize(data->window,
914 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200915 window->allocation.height,
916 x,y);
917
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100918 cairo_gl_surface_set_size(window->cairo_surface,
919 window->allocation.width,
920 window->allocation.height);
921}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000922#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100923
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400924struct display *
925window_get_display(struct window *window)
926{
927 return window->display;
928}
929
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400930void
931window_create_surface(struct window *window)
932{
933 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400934 uint32_t flags = 0;
935
936 if (!window->transparent)
937 flags = SURFACE_OPAQUE;
938
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400939 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500940#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100941 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
942 if (window->cairo_surface) {
943 window_resize_cairo_window_surface(window);
944 return;
945 }
946 surface = display_create_surface(window->display,
947 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400948 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100949 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500950 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400951 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100952 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400953 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954 break;
955#endif
956 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400957 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400958 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400959 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800960 default:
961 surface = NULL;
962 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400963 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400964
965 window_set_surface(window, surface);
966 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400967}
968
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400969void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500970window_destroy(struct window *window)
971{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200972 struct display *display = window->display;
973 struct input *input;
974
975 if (window->redraw_scheduled)
976 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500977 if (window->resize_scheduled)
978 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200979
980 wl_list_for_each(input, &display->input_list, link) {
981 if (input->pointer_focus == window)
982 input->pointer_focus = NULL;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500983 if (input->focus_widget->window == window)
984 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200985 if (input->keyboard_focus == window)
986 input->keyboard_focus = NULL;
987 }
988
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200989 if (window->shell_surface)
990 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500991 wl_surface_destroy(window->surface);
992 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200993
994 if (window->cairo_surface != NULL)
995 cairo_surface_destroy(window->cairo_surface);
996 if (window->pending_surface != NULL)
997 cairo_surface_destroy(window->pending_surface);
998
999 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001000 free(window);
1001}
1002
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001003static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001005{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001007
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001008 wl_list_for_each(child, &widget->child_list, link) {
1009 target = widget_find_widget(child, x, y);
1010 if (target)
1011 return target;
1012 }
1013
1014 if (widget->allocation.x <= x &&
1015 x < widget->allocation.x + widget->allocation.width &&
1016 widget->allocation.y <= y &&
1017 y < widget->allocation.y + widget->allocation.height) {
1018 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019 }
1020
1021 return NULL;
1022}
1023
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001024static struct widget *
1025widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001028
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 widget = malloc(sizeof *widget);
1030 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001031 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001032 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001033 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001034 wl_list_init(&widget->child_list);
1035
1036 return widget;
1037}
1038
1039struct widget *
1040window_add_widget(struct window *window, void *data)
1041{
1042 window->widget = widget_create(window, data);
1043 wl_list_init(&window->widget->link);
1044
1045 return window->widget;
1046}
1047
1048struct widget *
1049widget_add_widget(struct widget *parent, void *data)
1050{
1051 struct widget *widget;
1052
1053 widget = widget_create(parent->window, data);
1054 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001056 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057}
1058
1059void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001060widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001062 wl_list_remove(&widget->link);
1063 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064}
1065
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001067widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070}
1071
1072void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001073widget_set_size(struct widget *widget, int32_t width, int32_t height)
1074{
1075 struct window *window = widget->window;
1076
1077 widget->allocation.width = width;
1078 widget->allocation.height = height;
1079
1080 window->allocation.x = 0;
1081 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001082 window->allocation.width = width;
1083 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001084}
1085
1086void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087widget_set_allocation(struct widget *widget,
1088 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090 widget->allocation.x = x;
1091 widget->allocation.y = y;
1092 widget->allocation.width = width;
1093 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001094}
1095
1096void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001097widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001098{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100}
1101
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001102void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001103widget_set_resize_handler(struct widget *widget,
1104 widget_resize_handler_t handler)
1105{
1106 widget->resize_handler = handler;
1107}
1108
1109void
1110widget_set_redraw_handler(struct widget *widget,
1111 widget_redraw_handler_t handler)
1112{
1113 widget->redraw_handler = handler;
1114}
1115
1116void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001117widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001118{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001119 widget->enter_handler = handler;
1120}
1121
1122void
1123widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1124{
1125 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001126}
1127
1128void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001129widget_set_motion_handler(struct widget *widget,
1130 widget_motion_handler_t handler)
1131{
1132 widget->motion_handler = handler;
1133}
1134
1135void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001136widget_set_button_handler(struct widget *widget,
1137 widget_button_handler_t handler)
1138{
1139 widget->button_handler = handler;
1140}
1141
1142void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001143widget_schedule_redraw(struct widget *widget)
1144{
1145 window_schedule_redraw(widget->window);
1146}
1147
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001148cairo_surface_t *
1149window_get_surface(struct window *window)
1150{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001151 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001152}
1153
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001154struct wl_surface *
1155window_get_wl_surface(struct window *window)
1156{
1157 return window->surface;
1158}
1159
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160struct wl_shell_surface *
1161window_get_wl_shell_surface(struct window *window)
1162{
1163 return window->shell_surface;
1164}
1165
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001166static void
1167frame_resize_handler(struct widget *widget,
1168 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001169{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001170 struct frame *frame = data;
1171 struct widget *child = frame->child;
1172 struct rectangle allocation;
1173 int decoration_width, decoration_height;
1174
1175 decoration_width = 20 + frame->margin * 2;
1176 decoration_height = 60 + frame->margin * 2;
1177
1178 allocation.x = 10 + frame->margin;
1179 allocation.y = 50 + frame->margin;
1180 allocation.width = width - decoration_width;
1181 allocation.height = height - decoration_height;
1182
1183 widget_set_allocation(child, allocation.x, allocation.y,
1184 allocation.width, allocation.height);
1185
1186 if (child->resize_handler)
1187 child->resize_handler(child,
1188 allocation.width,
1189 allocation.height,
1190 child->user_data);
1191
1192 widget_set_allocation(widget, 0, 0,
1193 child->allocation.width + decoration_width,
1194 child->allocation.height + decoration_height);
1195}
1196
1197static void
1198frame_redraw_handler(struct widget *widget, void *data)
1199{
1200 struct frame *frame = data;
1201 cairo_t *cr;
1202 cairo_text_extents_t extents;
1203 cairo_surface_t *source;
1204 int width, height, shadow_dx = 3, shadow_dy = 3;
1205 struct window *window = widget->window;
1206
1207 width = widget->allocation.width;
1208 height = widget->allocation.height;
1209
1210 cr = cairo_create(window->cairo_surface);
1211
1212 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1213 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1214 cairo_paint(cr);
1215
1216 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1217 tile_mask(cr, window->display->shadow,
1218 shadow_dx, shadow_dy, width, height,
1219 frame->margin + 10 - shadow_dx,
1220 frame->margin + 10 - shadow_dy);
1221
1222 if (window->keyboard_device)
1223 source = window->display->active_frame;
1224 else
1225 source = window->display->inactive_frame;
1226
1227 tile_source(cr, source, 0, 0, width, height,
1228 frame->margin + 10, frame->margin + 50);
1229
1230 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1231 cairo_set_font_size(cr, 14);
1232 cairo_text_extents(cr, window->title, &extents);
1233 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1234 if (window->keyboard_device)
1235 cairo_set_source_rgb(cr, 0, 0, 0);
1236 else
1237 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1238 cairo_show_text(cr, window->title);
1239
1240 cairo_destroy(cr);
1241}
1242
1243static int
1244frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1245{
1246 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001247 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248 const int grip_size = 8;
1249
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001251 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257 hlocation = WINDOW_RESIZING_RIGHT;
1258 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001261 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001262 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001264 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001266 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268 vlocation = WINDOW_RESIZING_BOTTOM;
1269 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001270 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001272 location = vlocation | hlocation;
1273 if (location & WINDOW_EXTERIOR)
1274 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001276 location = WINDOW_TITLEBAR;
1277 else if (location == WINDOW_INTERIOR)
1278 location = WINDOW_CLIENT_AREA;
1279
1280 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001281}
1282
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001283static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001284frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001286 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001288 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289 switch (location) {
1290 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001291 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001292 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001293 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001295 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001296 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001297 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001298 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001299 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001300 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001301 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001302 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001303 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001304 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001305 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001306 case WINDOW_EXTERIOR:
1307 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001308 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001309 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001311}
1312
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001313static void
1314frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001315{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001316 switch (index) {
1317 case 0: /* close */
1318 if (window->close_handler)
1319 window->close_handler(window->parent,
1320 window->user_data);
1321 else
1322 display_exit(window->display);
1323 break;
1324 case 1: /* fullscreen */
1325 /* we don't have a way to get out of fullscreen for now */
1326 window_set_fullscreen(window, 1);
1327 break;
1328 case 2: /* rotate */
1329 case 3: /* scale */
1330 break;
1331 }
1332}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001334static int
1335frame_enter_handler(struct widget *widget,
1336 struct input *input, uint32_t time,
1337 int32_t x, int32_t y, void *data)
1338{
1339 return frame_get_pointer_image_for_location(data, input);
1340}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001341
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001342static int
1343frame_motion_handler(struct widget *widget,
1344 struct input *input, uint32_t time,
1345 int32_t x, int32_t y, void *data)
1346{
1347 return frame_get_pointer_image_for_location(data, input);
1348}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001350static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001351frame_button_handler(struct widget *widget,
1352 struct input *input, uint32_t time,
1353 int button, int state, void *data)
1354
1355{
1356 struct frame *frame = data;
1357 struct window *window = widget->window;
1358 int location;
1359 int32_t x, y;
1360 static const char *entries[] = {
1361 "Close", "Fullscreen", "Rotate", "Scale"
1362 };
1363
1364 location = frame_get_pointer_location(frame, input->sx, input->sy);
1365
1366 if (window->display->shell && button == BTN_LEFT && state == 1) {
1367 switch (location) {
1368 case WINDOW_TITLEBAR:
1369 if (!window->shell_surface)
1370 break;
1371 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001372 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373 wl_shell_surface_move(window->shell_surface,
1374 input_get_input_device(input),
1375 time);
1376 break;
1377 case WINDOW_RESIZING_TOP:
1378 case WINDOW_RESIZING_BOTTOM:
1379 case WINDOW_RESIZING_LEFT:
1380 case WINDOW_RESIZING_RIGHT:
1381 case WINDOW_RESIZING_TOP_LEFT:
1382 case WINDOW_RESIZING_TOP_RIGHT:
1383 case WINDOW_RESIZING_BOTTOM_LEFT:
1384 case WINDOW_RESIZING_BOTTOM_RIGHT:
1385 if (!window->shell_surface)
1386 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001387 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001388 wl_shell_surface_resize(window->shell_surface,
1389 input_get_input_device(input),
1390 time, location);
1391 break;
1392 }
1393 } else if (button == BTN_RIGHT && state == 1) {
1394 input_get_position(input, &x, &y);
1395 window->menu = window_create_menu(window->display,
1396 input, time,
1397 window,
1398 x - 10, y - 10,
1399 frame_menu_func,
1400 entries, 4);
1401 window_schedule_redraw(window->menu);
1402 }
1403}
1404
1405struct widget *
1406frame_create(struct window *window, void *data)
1407{
1408 struct frame *frame;
1409
1410 frame = malloc(sizeof *frame);
1411 memset(frame, 0, sizeof *frame);
1412
1413 frame->widget = window_add_widget(window, frame);
1414 frame->child = widget_add_widget(frame->widget, data);
1415 frame->margin = 16;
1416
1417 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1418 widget_set_resize_handler(frame->widget, frame_resize_handler);
1419 widget_set_enter_handler(frame->widget, frame_enter_handler);
1420 widget_set_motion_handler(frame->widget, frame_motion_handler);
1421 widget_set_button_handler(frame->widget, frame_button_handler);
1422
1423 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001424}
1425
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001426static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001427input_set_focus_widget(struct input *input, struct widget *focus,
1428 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001429{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001430 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001431 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001432
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001433 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001434 return;
1435
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001436 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001437 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001438 widget = old;
1439 if (input->grab)
1440 widget = input->grab;
1441 if (widget->leave_handler)
1442 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001443 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001444 }
1445
1446 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001447 widget = focus;
1448 if (input->grab)
1449 widget = input->grab;
1450 if (widget->enter_handler)
1451 pointer = widget->enter_handler(focus, input, time,
1452 x, y,
1453 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001454 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001455
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001456 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001457 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001458}
1459
1460static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001461input_handle_motion(void *data, struct wl_input_device *input_device,
1462 uint32_t time,
1463 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001464{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001465 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001466 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001467 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001468 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001469
1470 input->x = x;
1471 input->y = y;
1472 input->sx = sx;
1473 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001474
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001475 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001476 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001477 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001478 }
1479
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001480 if (input->grab)
1481 widget = input->grab;
1482 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001483 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001484 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001485 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001486 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001487 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001488
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001489 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001490}
1491
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001492void
1493input_grab(struct input *input, struct widget *widget, uint32_t button)
1494{
1495 input->grab = widget;
1496 input->grab_button = button;
1497}
1498
1499void
1500input_ungrab(struct input *input, uint32_t time)
1501{
1502 struct widget *widget;
1503
1504 input->grab = NULL;
1505 if (input->pointer_focus) {
1506 widget = widget_find_widget(input->pointer_focus->widget,
1507 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001508 input_set_focus_widget(input, widget,
1509 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001510 }
1511}
1512
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001513static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001514input_handle_button(void *data,
1515 struct wl_input_device *input_device,
1516 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001517{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001518 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001519 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001520
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001521 if (input->focus_widget && input->grab == NULL && state)
1522 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001523
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001524 widget = input->focus_widget;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001525 if (widget && widget->button_handler)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001526 (*input->grab->button_handler)(widget,
1527 input, time,
1528 button, state,
1529 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001530
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001531 if (input->grab && input->grab_button == button && !state)
1532 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001533}
1534
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001535static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001536input_handle_key(void *data, struct wl_input_device *input_device,
1537 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001538{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001539 struct input *input = data;
1540 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001541 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001542 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001543
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001544 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001545 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001546 return;
1547
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001548 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001549 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001550 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1551 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001552
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001553 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1554
1555 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001556 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001557 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001558 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001559
1560 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001561 (*window->key_handler)(window, input, time, key, sym, state,
1562 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001563}
1564
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001565static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001566input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001567{
1568 struct window *window = input->pointer_focus;
1569
1570 if (!window)
1571 return;
1572
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001573 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574
Pekka Paalanene1207c72011-12-16 12:02:09 +02001575 input->pointer_focus = NULL;
1576 input->current_pointer_image = POINTER_UNSET;
1577}
1578
1579static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001580input_handle_pointer_focus(void *data,
1581 struct wl_input_device *input_device,
1582 uint32_t time, struct wl_surface *surface,
1583 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001584{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001585 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001586 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001587 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001588
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001589 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001590 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001591 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001592
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001593 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001594 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001595 window = input->pointer_focus;
1596
Kristian Høgsberg59826582011-01-20 11:56:57 -05001597 input->x = x;
1598 input->y = y;
1599 input->sx = sx;
1600 input->sy = sy;
1601
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001602 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001603 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001604 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001605}
1606
1607static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001608input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001609{
1610 struct window *window = input->keyboard_focus;
1611
1612 if (!window)
1613 return;
1614
1615 window->keyboard_device = NULL;
1616 if (window->keyboard_focus_handler)
1617 (*window->keyboard_focus_handler)(window, NULL,
1618 window->user_data);
1619
1620 input->keyboard_focus = NULL;
1621}
1622
1623static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001624input_handle_keyboard_focus(void *data,
1625 struct wl_input_device *input_device,
1626 uint32_t time,
1627 struct wl_surface *surface,
1628 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001629{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001630 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001631 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001632 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001633 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001634
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001635 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001636
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001637 if (surface)
1638 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001639
1640 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001641 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001642 for (k = keys->data; k < end; k++)
1643 input->modifiers |= d->xkb->map->modmap[*k];
1644
1645 window = input->keyboard_focus;
1646 if (window) {
1647 window->keyboard_device = input;
1648 if (window->keyboard_focus_handler)
1649 (*window->keyboard_focus_handler)(window,
1650 window->keyboard_device,
1651 window->user_data);
1652 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001653}
1654
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001655static void
1656input_handle_touch_down(void *data,
1657 struct wl_input_device *wl_input_device,
1658 uint32_t time, struct wl_surface *surface,
1659 int32_t id, int32_t x, int32_t y)
1660{
1661}
1662
1663static void
1664input_handle_touch_up(void *data,
1665 struct wl_input_device *wl_input_device,
1666 uint32_t time, int32_t id)
1667{
1668}
1669
1670static void
1671input_handle_touch_motion(void *data,
1672 struct wl_input_device *wl_input_device,
1673 uint32_t time, int32_t id, int32_t x, int32_t y)
1674{
1675}
1676
1677static void
1678input_handle_touch_frame(void *data,
1679 struct wl_input_device *wl_input_device)
1680{
1681}
1682
1683static void
1684input_handle_touch_cancel(void *data,
1685 struct wl_input_device *wl_input_device)
1686{
1687}
1688
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001689static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001690 input_handle_motion,
1691 input_handle_button,
1692 input_handle_key,
1693 input_handle_pointer_focus,
1694 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001695 input_handle_touch_down,
1696 input_handle_touch_up,
1697 input_handle_touch_motion,
1698 input_handle_touch_frame,
1699 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001700};
1701
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001702void
1703input_get_position(struct input *input, int32_t *x, int32_t *y)
1704{
1705 *x = input->sx;
1706 *y = input->sy;
1707}
1708
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001709struct wl_input_device *
1710input_get_input_device(struct input *input)
1711{
1712 return input->input_device;
1713}
1714
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001715uint32_t
1716input_get_modifiers(struct input *input)
1717{
1718 return input->modifiers;
1719}
1720
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001721struct widget *
1722input_get_focus_widget(struct input *input)
1723{
1724 return input->focus_widget;
1725}
1726
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001727struct data_offer {
1728 struct wl_data_offer *offer;
1729 struct input *input;
1730 struct wl_array types;
1731 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001732
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001733 struct task io_task;
1734 int fd;
1735 data_func_t func;
1736 int32_t x, y;
1737 void *user_data;
1738};
1739
1740static void
1741data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1742{
1743 struct data_offer *offer = data;
1744 char **p;
1745
1746 p = wl_array_add(&offer->types, sizeof *p);
1747 *p = strdup(type);
1748}
1749
1750static const struct wl_data_offer_listener data_offer_listener = {
1751 data_offer_offer,
1752};
1753
1754static void
1755data_offer_destroy(struct data_offer *offer)
1756{
1757 char **p;
1758
1759 offer->refcount--;
1760 if (offer->refcount == 0) {
1761 wl_data_offer_destroy(offer->offer);
1762 for (p = offer->types.data; *p; p++)
1763 free(*p);
1764 wl_array_release(&offer->types);
1765 free(offer);
1766 }
1767}
1768
1769static void
1770data_device_data_offer(void *data,
1771 struct wl_data_device *data_device, uint32_t id)
1772{
1773 struct data_offer *offer;
1774
1775 offer = malloc(sizeof *offer);
1776
1777 wl_array_init(&offer->types);
1778 offer->refcount = 1;
1779 offer->input = data;
1780
1781 /* FIXME: Generate typesafe wrappers for this */
1782 offer->offer = (struct wl_data_offer *)
1783 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1784 id, &wl_data_offer_interface);
1785
1786 wl_data_offer_add_listener(offer->offer,
1787 &data_offer_listener, offer);
1788}
1789
1790static void
1791data_device_enter(void *data, struct wl_data_device *data_device,
1792 uint32_t time, struct wl_surface *surface,
1793 int32_t x, int32_t y, struct wl_data_offer *offer)
1794{
1795 struct input *input = data;
1796 struct window *window;
1797 char **p;
1798
1799 input->drag_offer = wl_data_offer_get_user_data(offer);
1800 window = wl_surface_get_user_data(surface);
1801 input->pointer_focus = window;
1802
1803 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1804 *p = NULL;
1805
1806 window = input->pointer_focus;
1807 if (window->data_handler)
1808 window->data_handler(window, input, time, x, y,
1809 input->drag_offer->types.data,
1810 window->user_data);
1811}
1812
1813static void
1814data_device_leave(void *data, struct wl_data_device *data_device)
1815{
1816 struct input *input = data;
1817
1818 data_offer_destroy(input->drag_offer);
1819 input->drag_offer = NULL;
1820}
1821
1822static void
1823data_device_motion(void *data, struct wl_data_device *data_device,
1824 uint32_t time, int32_t x, int32_t y)
1825{
1826 struct input *input = data;
1827 struct window *window = input->pointer_focus;
1828
1829 input->sx = x;
1830 input->sy = y;
1831
1832 if (window->data_handler)
1833 window->data_handler(window, input, time, x, y,
1834 input->drag_offer->types.data,
1835 window->user_data);
1836}
1837
1838static void
1839data_device_drop(void *data, struct wl_data_device *data_device)
1840{
1841 struct input *input = data;
1842 struct window *window = input->pointer_focus;
1843
1844 if (window->drop_handler)
1845 window->drop_handler(window, input,
1846 input->sx, input->sy, window->user_data);
1847}
1848
1849static void
1850data_device_selection(void *data,
1851 struct wl_data_device *wl_data_device,
1852 struct wl_data_offer *offer)
1853{
1854 struct input *input = data;
1855 char **p;
1856
1857 if (input->selection_offer)
1858 data_offer_destroy(input->selection_offer);
1859
1860 input->selection_offer = wl_data_offer_get_user_data(offer);
1861 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1862 *p = NULL;
1863}
1864
1865static const struct wl_data_device_listener data_device_listener = {
1866 data_device_data_offer,
1867 data_device_enter,
1868 data_device_leave,
1869 data_device_motion,
1870 data_device_drop,
1871 data_device_selection
1872};
1873
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001874void
1875input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1876{
1877 struct display *display = input->display;
1878 struct wl_buffer *buffer;
1879 cairo_surface_t *surface;
1880
1881 if (pointer == input->current_pointer_image)
1882 return;
1883
1884 input->current_pointer_image = pointer;
1885 surface = display->pointer_surfaces[pointer];
1886
1887 if (!surface)
1888 return;
1889
1890 buffer = display_get_buffer_for_surface(display, surface);
1891 wl_input_device_attach(input->input_device, time, buffer,
1892 pointer_images[pointer].hotspot_x,
1893 pointer_images[pointer].hotspot_y);
1894}
1895
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001896struct wl_data_device *
1897input_get_data_device(struct input *input)
1898{
1899 return input->data_device;
1900}
1901
1902void
1903input_set_selection(struct input *input,
1904 struct wl_data_source *source, uint32_t time)
1905{
1906 wl_data_device_set_selection(input->data_device, source, time);
1907}
1908
1909void
1910input_accept(struct input *input, uint32_t time, const char *type)
1911{
1912 wl_data_offer_accept(input->drag_offer->offer, time, type);
1913}
1914
1915static void
1916offer_io_func(struct task *task, uint32_t events)
1917{
1918 struct data_offer *offer =
1919 container_of(task, struct data_offer, io_task);
1920 unsigned int len;
1921 char buffer[4096];
1922
1923 len = read(offer->fd, buffer, sizeof buffer);
1924 offer->func(buffer, len,
1925 offer->x, offer->y, offer->user_data);
1926
1927 if (len == 0) {
1928 close(offer->fd);
1929 data_offer_destroy(offer);
1930 }
1931}
1932
1933static void
1934data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1935 data_func_t func, void *user_data)
1936{
1937 int p[2];
1938
1939 pipe2(p, O_CLOEXEC);
1940 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1941 close(p[1]);
1942
1943 offer->io_task.run = offer_io_func;
1944 offer->fd = p[0];
1945 offer->func = func;
1946 offer->refcount++;
1947 offer->user_data = user_data;
1948
1949 display_watch_fd(offer->input->display,
1950 offer->fd, EPOLLIN, &offer->io_task);
1951}
1952
1953void
1954input_receive_drag_data(struct input *input, const char *mime_type,
1955 data_func_t func, void *data)
1956{
1957 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1958 input->drag_offer->x = input->sx;
1959 input->drag_offer->y = input->sy;
1960}
1961
1962int
1963input_receive_selection_data(struct input *input, const char *mime_type,
1964 data_func_t func, void *data)
1965{
1966 char **p;
1967
1968 if (input->selection_offer == NULL)
1969 return -1;
1970
1971 for (p = input->selection_offer->types.data; *p; p++)
1972 if (strcmp(mime_type, *p) == 0)
1973 break;
1974
1975 if (*p == NULL)
1976 return -1;
1977
1978 data_offer_receive_data(input->selection_offer,
1979 mime_type, func, data);
1980 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001981}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001982
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001983int
1984input_receive_selection_data_to_fd(struct input *input,
1985 const char *mime_type, int fd)
1986{
1987 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1988
1989 return 0;
1990}
1991
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001992void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001993window_move(struct window *window, struct input *input, uint32_t time)
1994{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001995 if (!window->shell_surface)
1996 return;
1997
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001998 wl_shell_surface_move(window->shell_surface,
1999 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002000}
2001
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002002static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002003window_resize(struct window *window, int32_t width, int32_t height)
2004{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002005 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002006 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002007
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002008 allocation.x = 0;
2009 allocation.y = 0;
2010 allocation.width = width;
2011 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002012
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002013 widget = window->widget;
2014 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002015 allocation.width, allocation.height);
2016
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002017 if (widget->resize_handler)
2018 widget->resize_handler(widget,
2019 allocation.width,
2020 allocation.height,
2021 widget->user_data);
2022
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002023 window->allocation = widget->allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002024
2025 window_schedule_redraw(window);
2026}
2027
2028static void
2029idle_resize(struct task *task, uint32_t events)
2030{
2031 struct window *window =
2032 container_of(task, struct window, resize_task);
2033
2034 window_resize(window,
2035 window->allocation.width, window->allocation.height);
2036 window->resize_scheduled = 0;
2037}
2038
2039void
2040window_schedule_resize(struct window *window, int width, int height)
2041{
2042 if (!window->resize_scheduled) {
2043 window->allocation.width = width;
2044 window->allocation.height = height;
2045 window->resize_task.run = idle_resize;
2046 display_defer(window->display, &window->resize_task);
2047 window->resize_scheduled = 1;
2048 }
2049}
2050
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002051void
2052widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2053{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002054 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002055}
2056
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002057static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002058handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002059 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002060 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002061{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002062 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002063
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002064 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002065 return;
2066
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002067 window->resize_edges = edges;
2068
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002069 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002070}
2071
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002072static void
2073handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2074{
2075 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002076 struct menu *menu = window->widget->user_data;
2077
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002078 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002079 * device. Or just use wl_callback. And this really needs to
2080 * be a window vfunc that the menu can set. And we need the
2081 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002083 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002084 input_ungrab(menu->input, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002085 window_destroy(window);
2086}
2087
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002089 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002090 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002091};
2092
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002093void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002094window_get_allocation(struct window *window,
2095 struct rectangle *allocation)
2096{
2097 *allocation = window->allocation;
2098}
2099
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002100static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002101widget_redraw(struct widget *widget)
2102{
2103 struct widget *child;
2104
2105 if (widget->redraw_handler)
2106 widget->redraw_handler(widget, widget->user_data);
2107 wl_list_for_each(child, &widget->child_list, link)
2108 widget_redraw(child);
2109}
2110
2111static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002112idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002113{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002114 struct window *window =
2115 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002116
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002117 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002118 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002119 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002121}
2122
2123void
2124window_schedule_redraw(struct window *window)
2125{
2126 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002127 window->redraw_task.run = idle_redraw;
2128 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002129 window->redraw_scheduled = 1;
2130 }
2131}
2132
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002133void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002134window_set_custom(struct window *window)
2135{
2136 window->type = TYPE_CUSTOM;
2137}
2138
2139void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002140window_set_fullscreen(struct window *window, int fullscreen)
2141{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002142 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002143 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002144
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002145 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002146 return;
2147
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002148 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002149 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002150 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002151 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002152 width = output->allocation.width;
2153 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002154 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002155 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002156 width = window->saved_allocation.width;
2157 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002158 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002159
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002160 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002161}
2162
2163void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002164window_set_user_data(struct window *window, void *data)
2165{
2166 window->user_data = data;
2167}
2168
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002169void *
2170window_get_user_data(struct window *window)
2171{
2172 return window->user_data;
2173}
2174
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002175void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002176window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002177 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002178{
2179 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002180}
2181
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002182void
2183window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002184 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002185{
2186 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002187}
2188
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002189void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002190window_set_data_handler(struct window *window, window_data_handler_t handler)
2191{
2192 window->data_handler = handler;
2193}
2194
2195void
2196window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2197{
2198 window->drop_handler = handler;
2199}
2200
2201void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002202window_set_close_handler(struct window *window,
2203 window_close_handler_t handler)
2204{
2205 window->close_handler = handler;
2206}
2207
2208void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002209window_set_transparent(struct window *window, int transparent)
2210{
2211 window->transparent = transparent;
2212}
2213
2214void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002215window_set_title(struct window *window, const char *title)
2216{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002217 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002218 window->title = strdup(title);
2219}
2220
2221const char *
2222window_get_title(struct window *window)
2223{
2224 return window->title;
2225}
2226
2227void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002228display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2229 int32_t x, int32_t y, int32_t width, int32_t height)
2230{
2231 struct wl_buffer *buffer;
2232
2233 buffer = display_get_buffer_for_surface(display, cairo_surface);
2234
2235 wl_buffer_damage(buffer, x, y, width, height);
2236}
2237
2238void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002239window_damage(struct window *window, int32_t x, int32_t y,
2240 int32_t width, int32_t height)
2241{
2242 wl_surface_damage(window->surface, x, y, width, height);
2243}
2244
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002245static struct window *
2246window_create_internal(struct display *display, struct window *parent,
2247 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002248{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002249 struct window *window;
2250
2251 window = malloc(sizeof *window);
2252 if (window == NULL)
2253 return NULL;
2254
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002255 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002256 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002257 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002258 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002259 if (display->shell) {
2260 window->shell_surface =
2261 wl_shell_get_shell_surface(display->shell,
2262 window->surface);
2263 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002264 window->allocation.x = 0;
2265 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002266 window->allocation.width = width;
2267 window->allocation.height = height;
2268 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002269 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002270
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002271 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002272#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002273 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2274 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002275#else
2276 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2277#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002278 else
2279 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002280
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002281 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002282 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002283
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002284 if (window->shell_surface) {
2285 wl_shell_surface_set_user_data(window->shell_surface, window);
2286 wl_shell_surface_add_listener(window->shell_surface,
2287 &shell_surface_listener, window);
2288 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002289
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002290 return window;
2291}
2292
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002293struct window *
2294window_create(struct display *display, int32_t width, int32_t height)
2295{
2296 struct window *window;
2297
2298 window = window_create_internal(display, NULL, width, height);
2299 if (!window)
2300 return NULL;
2301
2302 return window;
2303}
2304
2305struct window *
2306window_create_transient(struct display *display, struct window *parent,
2307 int32_t x, int32_t y, int32_t width, int32_t height)
2308{
2309 struct window *window;
2310
2311 window = window_create_internal(parent->display,
2312 parent, width, height);
2313 if (!window)
2314 return NULL;
2315
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002316 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002317 window->x = x;
2318 window->y = y;
2319
2320 return window;
2321}
2322
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002323static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002324menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002325{
2326 int next;
2327
2328 next = (sy - 8) / 20;
2329 if (menu->current != next) {
2330 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002331 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002333}
2334
2335static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002336menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002337 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002338 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002339{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002340 struct menu *menu = data;
2341
2342 if (widget == menu->widget)
2343 menu_set_item(data, y);
2344
2345 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346}
2347
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002348static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002349menu_enter_handler(struct widget *widget,
2350 struct input *input, uint32_t time,
2351 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002352{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002353 struct menu *menu = data;
2354
2355 if (widget == menu->widget)
2356 menu_set_item(data, y);
2357
2358 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002359}
2360
2361static void
2362menu_leave_handler(struct widget *widget, struct input *input, void *data)
2363{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002364 struct menu *menu = data;
2365
2366 if (widget == menu->widget)
2367 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002368}
2369
2370static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002371menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002372 struct input *input, uint32_t time,
2373 int button, int state, void *data)
2374
2375{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002376 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002377
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002378 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002379 /* Either relase after press-drag-release or
2380 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002381 menu->func(menu->window->parent,
2382 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002383 input_ungrab(input, time);
2384 window_destroy(menu->widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002385 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002386}
2387
2388static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002389menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002390{
2391 cairo_t *cr;
2392 const int32_t r = 3, margin = 3;
2393 struct menu *menu = data;
2394 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002395 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002396
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397 cr = cairo_create(window->cairo_surface);
2398 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2399 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2400 cairo_paint(cr);
2401
2402 width = window->allocation.width;
2403 height = window->allocation.height;
2404 rounded_rect(cr, 0, 0, width, height, r);
2405 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2406 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2407 cairo_fill(cr);
2408
2409 for (i = 0; i < menu->count; i++) {
2410 if (i == menu->current) {
2411 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2412 cairo_rectangle(cr, margin, i * 20 + margin,
2413 width - 2 * margin, 20);
2414 cairo_fill(cr);
2415 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2416 cairo_move_to(cr, 10, i * 20 + 16);
2417 cairo_show_text(cr, menu->entries[i]);
2418 } else {
2419 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2420 cairo_move_to(cr, 10, i * 20 + 16);
2421 cairo_show_text(cr, menu->entries[i]);
2422 }
2423 }
2424
2425 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002426}
2427
2428struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002429window_create_menu(struct display *display,
2430 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002431 int32_t x, int32_t y,
2432 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433{
2434 struct window *window;
2435 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002436 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002437
2438 menu = malloc(sizeof *menu);
2439 if (!menu)
2440 return NULL;
2441
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002442 window = window_create_internal(parent->display, parent,
2443 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002444 if (!window)
2445 return NULL;
2446
2447 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002448 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002449 menu->entries = entries;
2450 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002451 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002452 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002453 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002454 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002455 window->type = TYPE_MENU;
2456 window->x = x;
2457 window->y = y;
2458
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002459 wl_shell_surface_set_popup(window->shell_surface,
2460 input->input_device, time,
2461 window->parent->shell_surface,
2462 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002464 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002465 widget_set_enter_handler(menu->widget, menu_enter_handler);
2466 widget_set_leave_handler(menu->widget, menu_leave_handler);
2467 widget_set_motion_handler(menu->widget, menu_motion_handler);
2468 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002469
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002470 input_grab(input, menu->widget, 0);
2471
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002472 return window;
2473}
2474
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002475void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002476window_set_buffer_type(struct window *window, enum window_buffer_type type)
2477{
2478 window->buffer_type = type;
2479}
2480
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002481
2482static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002483display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002484 struct wl_output *wl_output,
2485 int x, int y,
2486 int physical_width,
2487 int physical_height,
2488 int subpixel,
2489 const char *make,
2490 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002491{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002492 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002493
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002494 output->allocation.x = x;
2495 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002496}
2497
2498static void
2499display_handle_mode(void *data,
2500 struct wl_output *wl_output,
2501 uint32_t flags,
2502 int width,
2503 int height,
2504 int refresh)
2505{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002506 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002507 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002508
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002509 if (flags & WL_OUTPUT_MODE_CURRENT) {
2510 output->allocation.width = width;
2511 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002512 if (display->output_configure_handler)
2513 (*display->output_configure_handler)(
2514 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002515 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002516}
2517
2518static const struct wl_output_listener output_listener = {
2519 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002520 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002521};
2522
2523static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002524display_add_output(struct display *d, uint32_t id)
2525{
2526 struct output *output;
2527
2528 output = malloc(sizeof *output);
2529 if (output == NULL)
2530 return;
2531
2532 memset(output, 0, sizeof *output);
2533 output->display = d;
2534 output->output =
2535 wl_display_bind(d->display, id, &wl_output_interface);
2536 wl_list_insert(d->output_list.prev, &output->link);
2537
2538 wl_output_add_listener(output->output, &output_listener, output);
2539}
2540
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002541static void
2542output_destroy(struct output *output)
2543{
2544 if (output->destroy_handler)
2545 (*output->destroy_handler)(output, output->user_data);
2546
2547 wl_output_destroy(output->output);
2548 wl_list_remove(&output->link);
2549 free(output);
2550}
2551
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002552void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002553display_set_output_configure_handler(struct display *display,
2554 display_output_handler_t handler)
2555{
2556 struct output *output;
2557
2558 display->output_configure_handler = handler;
2559 if (!handler)
2560 return;
2561
2562 wl_list_for_each(output, &display->output_list, link)
2563 (*display->output_configure_handler)(output,
2564 display->user_data);
2565}
2566
2567void
2568output_set_user_data(struct output *output, void *data)
2569{
2570 output->user_data = data;
2571}
2572
2573void *
2574output_get_user_data(struct output *output)
2575{
2576 return output->user_data;
2577}
2578
2579void
2580output_set_destroy_handler(struct output *output,
2581 display_output_handler_t handler)
2582{
2583 output->destroy_handler = handler;
2584 /* FIXME: implement this, once we have way to remove outputs */
2585}
2586
2587void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002588output_get_allocation(struct output *output, struct rectangle *allocation)
2589{
2590 *allocation = output->allocation;
2591}
2592
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002593struct wl_output *
2594output_get_wl_output(struct output *output)
2595{
2596 return output->output;
2597}
2598
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002599static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002600display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002601{
2602 struct input *input;
2603
2604 input = malloc(sizeof *input);
2605 if (input == NULL)
2606 return;
2607
2608 memset(input, 0, sizeof *input);
2609 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002610 input->input_device =
2611 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002612 input->pointer_focus = NULL;
2613 input->keyboard_focus = NULL;
2614 wl_list_insert(d->input_list.prev, &input->link);
2615
2616 wl_input_device_add_listener(input->input_device,
2617 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002618 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002619
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002620 input->data_device =
2621 wl_data_device_manager_get_data_device(d->data_device_manager,
2622 input->input_device);
2623 wl_data_device_add_listener(input->data_device,
2624 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002625}
2626
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002627static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002628input_destroy(struct input *input)
2629{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002630 input_remove_keyboard_focus(input);
2631 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002632
2633 if (input->drag_offer)
2634 data_offer_destroy(input->drag_offer);
2635
2636 if (input->selection_offer)
2637 data_offer_destroy(input->selection_offer);
2638
2639 wl_data_device_destroy(input->data_device);
2640 wl_list_remove(&input->link);
2641 wl_input_device_destroy(input->input_device);
2642 free(input);
2643}
2644
2645static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002646display_handle_global(struct wl_display *display, uint32_t id,
2647 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002648{
2649 struct display *d = data;
2650
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002651 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002652 d->compositor =
2653 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002654 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002655 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002656 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002657 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002658 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002659 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002660 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002661 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002662 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2663 d->data_device_manager =
2664 wl_display_bind(display, id,
2665 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002666 }
2667}
2668
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002669static void
2670display_render_frame(struct display *d)
2671{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002672 int radius = 8;
2673 cairo_t *cr;
2674
2675 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2676 cr = cairo_create(d->shadow);
2677 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2678 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2679 rounded_rect(cr, 16, 16, 112, 112, radius);
2680 cairo_fill(cr);
2681 cairo_destroy(cr);
2682 blur_surface(d->shadow, 64);
2683
2684 d->active_frame =
2685 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2686 cr = cairo_create(d->active_frame);
2687 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2688 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2689 rounded_rect(cr, 16, 16, 112, 112, radius);
2690 cairo_fill(cr);
2691 cairo_destroy(cr);
2692
2693 d->inactive_frame =
2694 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2695 cr = cairo_create(d->inactive_frame);
2696 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2697 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2698 rounded_rect(cr, 16, 16, 112, 112, radius);
2699 cairo_fill(cr);
2700 cairo_destroy(cr);
2701}
2702
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002703static void
2704init_xkb(struct display *d)
2705{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002706 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002707
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002708 names.rules = "evdev";
2709 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002710 names.layout = option_xkb_layout;
2711 names.variant = option_xkb_variant;
2712 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002713
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002714 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002715 if (!d->xkb) {
2716 fprintf(stderr, "Failed to compile keymap\n");
2717 exit(1);
2718 }
2719}
2720
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002721static void
2722fini_xkb(struct display *display)
2723{
2724 xkb_free_keymap(display->xkb);
2725}
2726
Yuval Fledel45568f62010-12-06 09:18:12 -05002727static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002728init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002729{
2730 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002731 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002732
2733 static const EGLint premul_argb_cfg_attribs[] = {
2734 EGL_SURFACE_TYPE,
2735 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2736 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002737 EGL_RED_SIZE, 1,
2738 EGL_GREEN_SIZE, 1,
2739 EGL_BLUE_SIZE, 1,
2740 EGL_ALPHA_SIZE, 1,
2741 EGL_DEPTH_SIZE, 1,
2742 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2743 EGL_NONE
2744 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002745
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002746 static const EGLint rgb_cfg_attribs[] = {
2747 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2748 EGL_RED_SIZE, 1,
2749 EGL_GREEN_SIZE, 1,
2750 EGL_BLUE_SIZE, 1,
2751 EGL_ALPHA_SIZE, 0,
2752 EGL_DEPTH_SIZE, 1,
2753 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2754 EGL_NONE
2755 };
2756
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002757 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002758 if (!eglInitialize(d->dpy, &major, &minor)) {
2759 fprintf(stderr, "failed to initialize display\n");
2760 return -1;
2761 }
2762
2763 if (!eglBindAPI(EGL_OPENGL_API)) {
2764 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2765 return -1;
2766 }
2767
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002768 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002769 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002770 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002771 return -1;
2772 }
2773
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002774 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2775 &d->rgb_config, 1, &n) || n != 1) {
2776 fprintf(stderr, "failed to choose rgb config\n");
2777 return -1;
2778 }
2779
Benjamin Franzke0c991632011-09-27 21:57:31 +02002780 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2781 if (d->rgb_ctx == NULL) {
2782 fprintf(stderr, "failed to create context\n");
2783 return -1;
2784 }
2785 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2786 EGL_NO_CONTEXT, NULL);
2787 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002788 fprintf(stderr, "failed to create context\n");
2789 return -1;
2790 }
2791
Benjamin Franzke0c991632011-09-27 21:57:31 +02002792 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002793 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002794 return -1;
2795 }
2796
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002797#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002798 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2799 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002800 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002801 return -1;
2802 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002803 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2804 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2805 fprintf(stderr, "failed to get cairo egl argb device\n");
2806 return -1;
2807 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002808#endif
2809
2810 return 0;
2811}
2812
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002813static void
2814fini_egl(struct display *display)
2815{
2816#ifdef HAVE_CAIRO_EGL
2817 cairo_device_destroy(display->argb_device);
2818 cairo_device_destroy(display->rgb_device);
2819#endif
2820
2821 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2822 EGL_NO_CONTEXT);
2823
2824 eglTerminate(display->dpy);
2825 eglReleaseThread();
2826}
2827
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002828static int
2829event_mask_update(uint32_t mask, void *data)
2830{
2831 struct display *d = data;
2832
2833 d->mask = mask;
2834
2835 return 0;
2836}
2837
2838static void
2839handle_display_data(struct task *task, uint32_t events)
2840{
2841 struct display *display =
2842 container_of(task, struct display, display_task);
2843
2844 wl_display_iterate(display->display, display->mask);
2845}
2846
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002847struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002848display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002849{
2850 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002851 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002852 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002853 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002854
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002855 g_type_init();
2856
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002857 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002858 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002859 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002860
2861 xkb_option_group = g_option_group_new("xkb",
2862 "Keyboard options",
2863 "Show all XKB options",
2864 NULL, NULL);
2865 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2866 g_option_context_add_group (context, xkb_option_group);
2867
2868 if (!g_option_context_parse(context, argc, argv, &error)) {
2869 fprintf(stderr, "option parsing failed: %s\n", error->message);
2870 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002871 }
2872
Tim Wiederhake748f6722011-01-23 23:25:25 +01002873 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002874
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002875 d = malloc(sizeof *d);
2876 if (d == NULL)
2877 return NULL;
2878
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002879 memset(d, 0, sizeof *d);
2880
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002881 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002882 if (d->display == NULL) {
2883 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002884 return NULL;
2885 }
2886
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002887 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2888 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2889 d->display_task.run = handle_display_data;
2890 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2891
2892 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002893 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002894 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002895
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002896 /* Set up listener so we'll catch all events. */
2897 wl_display_add_global_listener(d->display,
2898 display_handle_global, d);
2899
2900 /* Process connection events. */
2901 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002902 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002903 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002904
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002905 d->image_target_texture_2d =
2906 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2907 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2908 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2909
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002910 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002911
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002912 display_render_frame(d);
2913
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002914 wl_list_init(&d->window_list);
2915
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002916 init_xkb(d);
2917
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002918 return d;
2919}
2920
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002921static void
2922display_destroy_outputs(struct display *display)
2923{
2924 struct output *tmp;
2925 struct output *output;
2926
2927 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2928 output_destroy(output);
2929}
2930
Pekka Paalanene1207c72011-12-16 12:02:09 +02002931static void
2932display_destroy_inputs(struct display *display)
2933{
2934 struct input *tmp;
2935 struct input *input;
2936
2937 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2938 input_destroy(input);
2939}
2940
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002941void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002942display_destroy(struct display *display)
2943{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002944 if (!wl_list_empty(&display->window_list))
2945 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2946
2947 if (!wl_list_empty(&display->deferred_list))
2948 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2949
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002950 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002951 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002952
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002953 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002954
2955 cairo_surface_destroy(display->active_frame);
2956 cairo_surface_destroy(display->inactive_frame);
2957 cairo_surface_destroy(display->shadow);
2958 destroy_pointer_surfaces(display);
2959
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002960 fini_egl(display);
2961
Pekka Paalanenc2052982011-12-16 11:41:32 +02002962 if (display->shell)
2963 wl_shell_destroy(display->shell);
2964
2965 if (display->shm)
2966 wl_shm_destroy(display->shm);
2967
2968 if (display->data_device_manager)
2969 wl_data_device_manager_destroy(display->data_device_manager);
2970
2971 wl_compositor_destroy(display->compositor);
2972
2973 close(display->epoll_fd);
2974
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002975 wl_display_flush(display->display);
2976 wl_display_destroy(display->display);
2977 free(display);
2978}
2979
2980void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002981display_set_user_data(struct display *display, void *data)
2982{
2983 display->user_data = data;
2984}
2985
2986void *
2987display_get_user_data(struct display *display)
2988{
2989 return display->user_data;
2990}
2991
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002992struct wl_display *
2993display_get_display(struct display *display)
2994{
2995 return display->display;
2996}
2997
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002998struct output *
2999display_get_output(struct display *display)
3000{
3001 return container_of(display->output_list.next, struct output, link);
3002}
3003
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003004struct wl_compositor *
3005display_get_compositor(struct display *display)
3006{
3007 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003008}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003009
3010EGLDisplay
3011display_get_egl_display(struct display *d)
3012{
3013 return d->dpy;
3014}
3015
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003016struct wl_data_source *
3017display_create_data_source(struct display *display)
3018{
3019 return wl_data_device_manager_create_data_source(display->data_device_manager);
3020}
3021
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003022EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003023display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003024{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003025 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003026}
3027
Benjamin Franzke0c991632011-09-27 21:57:31 +02003028EGLConfig
3029display_get_argb_egl_config(struct display *d)
3030{
3031 return d->premultiplied_argb_config;
3032}
3033
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003034struct wl_shell *
3035display_get_shell(struct display *display)
3036{
3037 return display->shell;
3038}
3039
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003040int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003041display_acquire_window_surface(struct display *display,
3042 struct window *window,
3043 EGLContext ctx)
3044{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003045#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003046 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003047 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003048
3049 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003050 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003051 device = cairo_surface_get_device(window->cairo_surface);
3052 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003053 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003054
Benjamin Franzke0c991632011-09-27 21:57:31 +02003055 if (!ctx) {
3056 if (device == display->rgb_device)
3057 ctx = display->rgb_ctx;
3058 else if (device == display->argb_device)
3059 ctx = display->argb_ctx;
3060 else
3061 assert(0);
3062 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003063
3064 data = cairo_surface_get_user_data(window->cairo_surface,
3065 &surface_data_key);
3066
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003067 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003068 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003069 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3070 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003071
3072 return 0;
3073#else
3074 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003075#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003076}
3077
3078void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003079display_release_window_surface(struct display *display,
3080 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003081{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003082#ifdef HAVE_CAIRO_EGL
3083 cairo_device_t *device;
3084
3085 device = cairo_surface_get_device(window->cairo_surface);
3086 if (!device)
3087 return;
3088
3089 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003090 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003091 cairo_device_release(device);
3092#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003093}
3094
3095void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003096display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003097{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003098 wl_list_insert(&display->deferred_list, &task->link);
3099}
3100
3101void
3102display_watch_fd(struct display *display,
3103 int fd, uint32_t events, struct task *task)
3104{
3105 struct epoll_event ep;
3106
3107 ep.events = events;
3108 ep.data.ptr = task;
3109 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3110}
3111
3112void
3113display_run(struct display *display)
3114{
3115 struct task *task;
3116 struct epoll_event ep[16];
3117 int i, count;
3118
Pekka Paalanen826d7952011-12-15 10:14:07 +02003119 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003120 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003121 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003122
3123 while (!wl_list_empty(&display->deferred_list)) {
3124 task = container_of(display->deferred_list.next,
3125 struct task, link);
3126 wl_list_remove(&task->link);
3127 task->run(task, 0);
3128 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003129
3130 if (!display->running)
3131 break;
3132
3133 wl_display_flush(display->display);
3134
3135 count = epoll_wait(display->epoll_fd,
3136 ep, ARRAY_LENGTH(ep), -1);
3137 for (i = 0; i < count; i++) {
3138 task = ep[i].data.ptr;
3139 task->run(task, ep[i].events);
3140 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003141 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003142}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003143
3144void
3145display_exit(struct display *display)
3146{
3147 display->running = 0;
3148}