blob: bd9107c5d731250440941234e86a90ee400ab823 [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øgsbergc51f7992012-01-08 15:09:53 -0500136 struct widget *focus_widget;
137 uint32_t widget_grab_button;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400138
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500139 struct window *menu;
140
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500141 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400142 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500143};
144
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500145struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500146 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500147 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400148 struct wl_list link;
149 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500150 widget_resize_handler_t resize_handler;
151 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500152 widget_enter_handler_t enter_handler;
153 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500154 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500155 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 void *user_data;
157};
158
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400159struct input {
160 struct display *display;
161 struct wl_input_device *input_device;
162 struct window *pointer_focus;
163 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400164 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400165 uint32_t modifiers;
166 int32_t x, y, sx, sy;
167 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168
169 struct wl_data_device *data_device;
170 struct data_offer *drag_offer;
171 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400172};
173
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500174struct output {
175 struct display *display;
176 struct wl_output *output;
177 struct rectangle allocation;
178 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200179
180 display_output_handler_t destroy_handler;
181 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500182};
183
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500184struct frame {
185 struct widget *widget;
186 struct widget *child;
187 int margin;
188};
189
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500190struct menu {
191 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500192 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500193 const char **entries;
194 uint32_t time;
195 int current;
196 int count;
197 menu_func_t func;
198};
199
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400200enum {
201 POINTER_DEFAULT = 100,
202 POINTER_UNSET
203};
204
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500205enum window_location {
206 WINDOW_INTERIOR = 0,
207 WINDOW_RESIZING_TOP = 1,
208 WINDOW_RESIZING_BOTTOM = 2,
209 WINDOW_RESIZING_LEFT = 4,
210 WINDOW_RESIZING_TOP_LEFT = 5,
211 WINDOW_RESIZING_BOTTOM_LEFT = 6,
212 WINDOW_RESIZING_RIGHT = 8,
213 WINDOW_RESIZING_TOP_RIGHT = 9,
214 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
215 WINDOW_RESIZING_MASK = 15,
216 WINDOW_EXTERIOR = 16,
217 WINDOW_TITLEBAR = 17,
218 WINDOW_CLIENT_AREA = 18,
219};
220
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400221const char *option_xkb_layout = "us";
222const char *option_xkb_variant = "";
223const char *option_xkb_options = "";
224
225static const GOptionEntry xkb_option_entries[] = {
226 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_layout, "XKB Layout" },
228 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
229 &option_xkb_variant, "XKB Variant" },
230 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_options, "XKB Options" },
232 { NULL }
233};
234
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400235static const cairo_user_data_key_t surface_data_key;
236struct surface_data {
237 struct wl_buffer *buffer;
238};
239
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500240#define MULT(_d,c,a,t) \
241 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
242
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500243#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245struct egl_window_surface_data {
246 struct display *display;
247 struct wl_surface *surface;
248 struct wl_egl_window *window;
249 EGLSurface surf;
250};
251
252static void
253egl_window_surface_data_destroy(void *p)
254{
255 struct egl_window_surface_data *data = p;
256 struct display *d = data->display;
257
258 eglDestroySurface(d->dpy, data->surf);
259 wl_egl_window_destroy(data->window);
260 data->surface = NULL;
261
262 free(p);
263}
264
265static cairo_surface_t *
266display_create_egl_window_surface(struct display *display,
267 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269 struct rectangle *rectangle)
270{
271 cairo_surface_t *cairo_surface;
272 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 EGLConfig config;
274 const EGLint *attribs;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200275 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400277 static const EGLint premul_attribs[] = {
278 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
279 EGL_NONE
280 };
281
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100282 data = malloc(sizeof *data);
283 if (data == NULL)
284 return NULL;
285
286 data->display = display;
287 data->surface = surface;
288
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 if (flags & SURFACE_OPAQUE) {
290 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200291 device = display->rgb_device;
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200292 attribs = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 } else {
294 config = display->premultiplied_argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200295 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 attribs = premul_attribs;
297 }
298
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400299 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400301 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100302
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400303 data->surf = eglCreateWindowSurface(display->dpy, config,
304 data->window, attribs);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100305
Benjamin Franzke0c991632011-09-27 21:57:31 +0200306 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100307 data->surf,
308 rectangle->width,
309 rectangle->height);
310
311 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
312 data, egl_window_surface_data_destroy);
313
314 return cairo_surface;
315}
316
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500317struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400318 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200319 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400320 EGLImageKHR image;
321 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500323 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324};
325
326static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500327egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400328{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500329 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800330 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400331
Benjamin Franzke0c991632011-09-27 21:57:31 +0200332 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200334 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800335
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500336 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500338 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400340}
341
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500342EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500343display_get_image_for_egl_image_surface(struct display *display,
344 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500345{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500347
348 data = cairo_surface_get_user_data (surface, &surface_data_key);
349
350 return data->image;
351}
352
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500353static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500354display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400355 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500356 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500358 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400359 EGLDisplay dpy = display->dpy;
360 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200361 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400362
363 data = malloc(sizeof *data);
364 if (data == NULL)
365 return NULL;
366
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800367 data->display = display;
368
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400369 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400370 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500371 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000372 free(data);
373 return NULL;
374 }
375
Benjamin Franzke0c991632011-09-27 21:57:31 +0200376 if (flags & SURFACE_OPAQUE) {
377 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200378 content = CAIRO_CONTENT_COLOR;
379 } else {
380 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200381 content = CAIRO_CONTENT_COLOR_ALPHA;
382 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400383
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500384 data->image = display->create_image(dpy, NULL,
385 EGL_NATIVE_PIXMAP_KHR,
386 (EGLClientBuffer) data->pixmap,
387 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500389 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390 free(data);
391 return NULL;
392 }
393
394 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400395 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500400 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200401 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
Benjamin Franzke0c991632011-09-27 21:57:31 +0200403 surface = cairo_gl_surface_create_for_texture(data->device,
404 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405 data->texture,
406 rectangle->width,
407 rectangle->height);
408
409 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
412 return surface;
413}
414
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500415static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500416display_create_egl_image_surface_from_file(struct display *display,
417 const char *filename,
418 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400419{
420 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421 GdkPixbuf *pixbuf;
422 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400423 int stride, i;
424 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500425 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400426
427 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400429 FALSE, &error);
430 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400432
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400433 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
434 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500435 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400438
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439
440 stride = gdk_pixbuf_get_rowstride(pixbuf);
441 pixels = gdk_pixbuf_get_pixels(pixbuf);
442
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400443 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400444 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400445 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400446 while (p < end) {
447 unsigned int t;
448
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400449 MULT(p[0], p[0], p[3], t);
450 MULT(p[1], p[1], p[3], t);
451 MULT(p[2], p[2], p[3], t);
452 p += 4;
453
454 }
455 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400456
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200457 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000458 if (surface == NULL) {
459 g_object_unref(pixbuf);
460 return NULL;
461 }
462
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800463 data = cairo_surface_get_user_data(surface, &surface_data_key);
464
Benjamin Franzke0c991632011-09-27 21:57:31 +0200465 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800466 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800467 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
468 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200469 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400470
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500471 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400472
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400473 return surface;
474}
475
476#endif
477
478struct wl_buffer *
479display_get_buffer_for_surface(struct display *display,
480 cairo_surface_t *surface)
481{
482 struct surface_data *data;
483
484 data = cairo_surface_get_user_data (surface, &surface_data_key);
485
486 return data->buffer;
487}
488
489struct shm_surface_data {
490 struct surface_data data;
491 void *map;
492 size_t length;
493};
494
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500495static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400496shm_surface_data_destroy(void *p)
497{
498 struct shm_surface_data *data = p;
499
500 wl_buffer_destroy(data->data.buffer);
501 munmap(data->map, data->length);
502}
503
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500504static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400506 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400507{
508 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400510 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800511 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400512 char filename[] = "/tmp/wayland-shm-XXXXXX";
513
514 data = malloc(sizeof *data);
515 if (data == NULL)
516 return NULL;
517
518 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
519 rectangle->width);
520 data->length = stride * rectangle->height;
521 fd = mkstemp(filename);
522 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000523 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400524 return NULL;
525 }
526 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000527 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528 close(fd);
529 return NULL;
530 }
531
532 data->map = mmap(NULL, data->length,
533 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
534 unlink(filename);
535
536 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000537 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400538 close(fd);
539 return NULL;
540 }
541
542 surface = cairo_image_surface_create_for_data (data->map,
543 CAIRO_FORMAT_ARGB32,
544 rectangle->width,
545 rectangle->height,
546 stride);
547
548 cairo_surface_set_user_data (surface, &surface_data_key,
549 data, shm_surface_data_destroy);
550
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400551 if (flags & SURFACE_OPAQUE)
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400552 format = WL_SHM_FORMAT_XRGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400553 else
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400554 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400555
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400556 data->data.buffer = wl_shm_create_buffer(display->shm,
557 fd,
558 rectangle->width,
559 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400560 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561
562 close(fd);
563
564 return surface;
565}
566
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500567static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400568display_create_shm_surface_from_file(struct display *display,
569 const char *filename,
570 struct rectangle *rect)
571{
572 cairo_surface_t *surface;
573 GdkPixbuf *pixbuf;
574 GError *error = NULL;
575 int stride, i;
576 unsigned char *pixels, *p, *end, *dest_data;
577 int dest_stride;
578 uint32_t *d;
579
580 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
581 rect->width, rect->height,
582 FALSE, &error);
583 if (error != NULL)
584 return NULL;
585
586 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
587 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500588 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400589 return NULL;
590 }
591
592 stride = gdk_pixbuf_get_rowstride(pixbuf);
593 pixels = gdk_pixbuf_get_pixels(pixbuf);
594
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400595 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000596 if (surface == NULL) {
597 g_object_unref(pixbuf);
598 return NULL;
599 }
600
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400601 dest_data = cairo_image_surface_get_data (surface);
602 dest_stride = cairo_image_surface_get_stride (surface);
603
604 for (i = 0; i < rect->height; i++) {
605 d = (uint32_t *) (dest_data + i * dest_stride);
606 p = pixels + i * stride;
607 end = p + rect->width * 4;
608 while (p < end) {
609 unsigned int t;
610 unsigned char a, r, g, b;
611
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612 a = p[3];
613 MULT(r, p[0], a, t);
614 MULT(g, p[1], a, t);
615 MULT(b, p[2], a, t);
616 p += 4;
617 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
618 }
619 }
620
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500621 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400622
623 return surface;
624}
625
nobled7b87cb02011-02-01 18:51:47 +0000626static int
627check_size(struct rectangle *rect)
628{
629 if (rect->width && rect->height)
630 return 0;
631
632 fprintf(stderr, "tried to create surface of "
633 "width: %d, height: %d\n", rect->width, rect->height);
634 return -1;
635}
636
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400637cairo_surface_t *
638display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400640 struct rectangle *rectangle,
641 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400642{
nobled7b87cb02011-02-01 18:51:47 +0000643 if (check_size(rectangle) < 0)
644 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500645#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500646 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 if (surface)
648 return display_create_egl_window_surface(display,
649 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400650 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100651 rectangle);
652 else
653 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400654 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500656 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400658 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400659}
660
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500661static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662display_create_surface_from_file(struct display *display,
663 const char *filename,
664 struct rectangle *rectangle)
665{
nobled7b87cb02011-02-01 18:51:47 +0000666 if (check_size(rectangle) < 0)
667 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500668#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500669 if (display->dpy) {
670 return display_create_egl_image_surface_from_file(display,
671 filename,
672 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500673 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500675 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400676}
Yuval Fledel45568f62010-12-06 09:18:12 -0500677 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400678 const char *filename;
679 int hotspot_x, hotspot_y;
680} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500681 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
682 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
683 { DATADIR "/weston/bottom_side.png", 16, 20 },
684 { DATADIR "/weston/grabbing.png", 20, 17 },
685 { DATADIR "/weston/left_ptr.png", 10, 5 },
686 { DATADIR "/weston/left_side.png", 10, 20 },
687 { DATADIR "/weston/right_side.png", 30, 19 },
688 { DATADIR "/weston/top_left_corner.png", 8, 8 },
689 { DATADIR "/weston/top_right_corner.png", 26, 8 },
690 { DATADIR "/weston/top_side.png", 18, 8 },
691 { DATADIR "/weston/xterm.png", 15, 15 },
692 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693};
694
695static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400697{
698 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400702 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 display->pointer_surfaces =
704 malloc(count * sizeof *display->pointer_surfaces);
705 rect.width = width;
706 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400707 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400708 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400709 display_create_surface_from_file(display,
710 pointer_images[i].filename,
711 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100712 if (!display->pointer_surfaces[i]) {
713 fprintf(stderr, "Error loading pointer image: %s\n",
714 pointer_images[i].filename);
715 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400716 }
717
718}
719
Pekka Paalanen325bb602011-12-19 10:31:45 +0200720static void
721destroy_pointer_surfaces(struct display *display)
722{
723 int i, count;
724
725 count = ARRAY_LENGTH(pointer_images);
726 for (i = 0; i < count; ++i) {
727 if (display->pointer_surfaces[i])
728 cairo_surface_destroy(display->pointer_surfaces[i]);
729 }
730 free(display->pointer_surfaces);
731}
732
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733cairo_surface_t *
734display_get_pointer_surface(struct display *display, int pointer,
735 int *width, int *height,
736 int *hotspot_x, int *hotspot_y)
737{
738 cairo_surface_t *surface;
739
740 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500741#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400742 *width = cairo_gl_surface_get_width(surface);
743 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000744#else
745 *width = cairo_image_surface_get_width(surface);
746 *height = cairo_image_surface_get_height(surface);
747#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400748 *hotspot_x = pointer_images[pointer].hotspot_x;
749 *hotspot_y = pointer_images[pointer].hotspot_y;
750
751 return cairo_surface_reference(surface);
752}
753
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400754static void
755window_attach_surface(struct window *window);
756
757static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400758free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400759{
760 struct window *window = data;
761
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400763 cairo_surface_destroy(window->pending_surface);
764 window->pending_surface = NULL;
765 if (window->cairo_surface)
766 window_attach_surface(window);
767}
768
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400769static const struct wl_callback_listener free_surface_listener = {
770 free_surface
771};
772
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500773static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200774window_get_resize_dx_dy(struct window *window, int *x, int *y)
775{
776 if (window->resize_edges & WINDOW_RESIZING_LEFT)
777 *x = window->server_allocation.width - window->allocation.width;
778 else
779 *x = 0;
780
781 if (window->resize_edges & WINDOW_RESIZING_TOP)
782 *y = window->server_allocation.height -
783 window->allocation.height;
784 else
785 *y = 0;
786
787 window->resize_edges = 0;
788}
789
790static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791window_set_type(struct window *window)
792{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200793 if (!window->shell_surface)
794 return;
795
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400796 switch (window->type) {
797 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200798 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 break;
800 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200801 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 break;
803 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200804 wl_shell_surface_set_transient(window->shell_surface,
805 window->parent->shell_surface,
806 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400807 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500808 case TYPE_MENU:
809 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400810 case TYPE_CUSTOM:
811 break;
812 }
813}
814
815static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500816window_attach_surface(struct window *window)
817{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400818 struct display *display = window->display;
819 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400820 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000821#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500824 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400826 if (display->shell)
827 window_set_type(window);
828
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100831 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
832 data = cairo_surface_get_user_data(window->cairo_surface,
833 &surface_data_key);
834
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 cairo_gl_surface_swapbuffers(window->cairo_surface);
836 wl_egl_window_get_attached_size(data->window,
837 &window->server_allocation.width,
838 &window->server_allocation.height);
839 break;
840 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000841#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100842 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200843 window_get_resize_dx_dy(window, &x, &y);
844
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100845 if (window->pending_surface != NULL)
846 return;
847
848 window->pending_surface = window->cairo_surface;
849 window->cairo_surface = NULL;
850
851 buffer =
852 display_get_buffer_for_surface(display,
853 window->pending_surface);
854
855 wl_surface_attach(window->surface, buffer, x, y);
856 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400857 cb = wl_display_sync(display->display);
858 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100859 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000860 default:
861 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100862 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500863
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500864 wl_surface_damage(window->surface, 0, 0,
865 window->allocation.width,
866 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500867}
868
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500869void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400870window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500871{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100872 if (window->cairo_surface) {
873 switch (window->buffer_type) {
874 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
875 case WINDOW_BUFFER_TYPE_SHM:
876 display_surface_damage(window->display,
877 window->cairo_surface,
878 0, 0,
879 window->allocation.width,
880 window->allocation.height);
881 break;
882 default:
883 break;
884 }
885 window_attach_surface(window);
886 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500887}
888
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400889void
890window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400891{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500892 cairo_surface_reference(surface);
893
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400894 if (window->cairo_surface != NULL)
895 cairo_surface_destroy(window->cairo_surface);
896
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500897 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400898}
899
Benjamin Franzke22d54812011-07-16 19:50:32 +0000900#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100901static void
902window_resize_cairo_window_surface(struct window *window)
903{
904 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100906
907 data = cairo_surface_get_user_data(window->cairo_surface,
908 &surface_data_key);
909
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200910 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 wl_egl_window_resize(data->window,
912 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200913 window->allocation.height,
914 x,y);
915
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100916 cairo_gl_surface_set_size(window->cairo_surface,
917 window->allocation.width,
918 window->allocation.height);
919}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000920#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100921
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400922struct display *
923window_get_display(struct window *window)
924{
925 return window->display;
926}
927
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928void
929window_create_surface(struct window *window)
930{
931 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400932 uint32_t flags = 0;
933
934 if (!window->transparent)
935 flags = SURFACE_OPAQUE;
936
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400937 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500938#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
940 if (window->cairo_surface) {
941 window_resize_cairo_window_surface(window);
942 return;
943 }
944 surface = display_create_surface(window->display,
945 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400946 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100947 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500948 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400949 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100950 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400951 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400952 break;
953#endif
954 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400956 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800958 default:
959 surface = NULL;
960 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400961 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400962
963 window_set_surface(window, surface);
964 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400965}
966
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400967void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500968window_destroy(struct window *window)
969{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200970 struct display *display = window->display;
971 struct input *input;
972
973 if (window->redraw_scheduled)
974 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500975 if (window->resize_scheduled)
976 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200977
978 wl_list_for_each(input, &display->input_list, link) {
979 if (input->pointer_focus == window)
980 input->pointer_focus = NULL;
981 if (input->keyboard_focus == window)
982 input->keyboard_focus = NULL;
983 }
984
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200985 if (window->shell_surface)
986 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500987 wl_surface_destroy(window->surface);
988 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200989
990 if (window->cairo_surface != NULL)
991 cairo_surface_destroy(window->cairo_surface);
992 if (window->pending_surface != NULL)
993 cairo_surface_destroy(window->pending_surface);
994
995 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500996 free(window);
997}
998
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500999static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001000widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001002 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001003
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004 wl_list_for_each(child, &widget->child_list, link) {
1005 target = widget_find_widget(child, x, y);
1006 if (target)
1007 return target;
1008 }
1009
1010 if (widget->allocation.x <= x &&
1011 x < widget->allocation.x + widget->allocation.width &&
1012 widget->allocation.y <= y &&
1013 y < widget->allocation.y + widget->allocation.height) {
1014 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001015 }
1016
1017 return NULL;
1018}
1019
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001020static struct widget *
1021widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001022{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001024
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001025 widget = malloc(sizeof *widget);
1026 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001027 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001028 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001029 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001030 wl_list_init(&widget->child_list);
1031
1032 return widget;
1033}
1034
1035struct widget *
1036window_add_widget(struct window *window, void *data)
1037{
1038 window->widget = widget_create(window, data);
1039 wl_list_init(&window->widget->link);
1040
1041 return window->widget;
1042}
1043
1044struct widget *
1045widget_add_widget(struct widget *parent, void *data)
1046{
1047 struct widget *widget;
1048
1049 widget = widget_create(parent->window, data);
1050 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001051
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001052 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001053}
1054
1055void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001056widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001058 wl_list_remove(&widget->link);
1059 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001060}
1061
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001062struct widget *
1063window_get_focus_widget(struct window *window)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001064{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001065 return window->focus_widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001066}
1067
1068void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001069widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072}
1073
1074void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001075widget_set_size(struct widget *widget, int32_t width, int32_t height)
1076{
1077 struct window *window = widget->window;
1078
1079 widget->allocation.width = width;
1080 widget->allocation.height = height;
1081
1082 window->allocation.x = 0;
1083 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001084 window->allocation.width = width;
1085 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001086}
1087
1088void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089widget_set_allocation(struct widget *widget,
1090 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001092 widget->allocation.x = x;
1093 widget->allocation.y = y;
1094 widget->allocation.width = width;
1095 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001096}
1097
1098void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001099widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001100{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001101 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001102}
1103
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001104void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001105widget_set_resize_handler(struct widget *widget,
1106 widget_resize_handler_t handler)
1107{
1108 widget->resize_handler = handler;
1109}
1110
1111void
1112widget_set_redraw_handler(struct widget *widget,
1113 widget_redraw_handler_t handler)
1114{
1115 widget->redraw_handler = handler;
1116}
1117
1118void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001119widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001120{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001121 widget->enter_handler = handler;
1122}
1123
1124void
1125widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1126{
1127 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001128}
1129
1130void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001131widget_set_motion_handler(struct widget *widget,
1132 widget_motion_handler_t handler)
1133{
1134 widget->motion_handler = handler;
1135}
1136
1137void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001138widget_set_button_handler(struct widget *widget,
1139 widget_button_handler_t handler)
1140{
1141 widget->button_handler = handler;
1142}
1143
1144void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001145widget_schedule_redraw(struct widget *widget)
1146{
1147 window_schedule_redraw(widget->window);
1148}
1149
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001150cairo_surface_t *
1151window_get_surface(struct window *window)
1152{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001153 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001154}
1155
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001156struct wl_surface *
1157window_get_wl_surface(struct window *window)
1158{
1159 return window->surface;
1160}
1161
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001162struct wl_shell_surface *
1163window_get_wl_shell_surface(struct window *window)
1164{
1165 return window->shell_surface;
1166}
1167
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001168static void
1169frame_resize_handler(struct widget *widget,
1170 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001172 struct frame *frame = data;
1173 struct widget *child = frame->child;
1174 struct rectangle allocation;
1175 int decoration_width, decoration_height;
1176
1177 decoration_width = 20 + frame->margin * 2;
1178 decoration_height = 60 + frame->margin * 2;
1179
1180 allocation.x = 10 + frame->margin;
1181 allocation.y = 50 + frame->margin;
1182 allocation.width = width - decoration_width;
1183 allocation.height = height - decoration_height;
1184
1185 widget_set_allocation(child, allocation.x, allocation.y,
1186 allocation.width, allocation.height);
1187
1188 if (child->resize_handler)
1189 child->resize_handler(child,
1190 allocation.width,
1191 allocation.height,
1192 child->user_data);
1193
1194 widget_set_allocation(widget, 0, 0,
1195 child->allocation.width + decoration_width,
1196 child->allocation.height + decoration_height);
1197}
1198
1199static void
1200frame_redraw_handler(struct widget *widget, void *data)
1201{
1202 struct frame *frame = data;
1203 cairo_t *cr;
1204 cairo_text_extents_t extents;
1205 cairo_surface_t *source;
1206 int width, height, shadow_dx = 3, shadow_dy = 3;
1207 struct window *window = widget->window;
1208
1209 width = widget->allocation.width;
1210 height = widget->allocation.height;
1211
1212 cr = cairo_create(window->cairo_surface);
1213
1214 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1215 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1216 cairo_paint(cr);
1217
1218 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1219 tile_mask(cr, window->display->shadow,
1220 shadow_dx, shadow_dy, width, height,
1221 frame->margin + 10 - shadow_dx,
1222 frame->margin + 10 - shadow_dy);
1223
1224 if (window->keyboard_device)
1225 source = window->display->active_frame;
1226 else
1227 source = window->display->inactive_frame;
1228
1229 tile_source(cr, source, 0, 0, width, height,
1230 frame->margin + 10, frame->margin + 50);
1231
1232 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1233 cairo_set_font_size(cr, 14);
1234 cairo_text_extents(cr, window->title, &extents);
1235 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1236 if (window->keyboard_device)
1237 cairo_set_source_rgb(cr, 0, 0, 0);
1238 else
1239 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1240 cairo_show_text(cr, window->title);
1241
1242 cairo_destroy(cr);
1243}
1244
1245static int
1246frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1247{
1248 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001249 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 const int grip_size = 8;
1251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001257 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 hlocation = WINDOW_RESIZING_RIGHT;
1260 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001263 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001264 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001265 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001266 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001267 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001268 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001269 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270 vlocation = WINDOW_RESIZING_BOTTOM;
1271 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001272 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001273
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001274 location = vlocation | hlocation;
1275 if (location & WINDOW_EXTERIOR)
1276 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278 location = WINDOW_TITLEBAR;
1279 else if (location == WINDOW_INTERIOR)
1280 location = WINDOW_CLIENT_AREA;
1281
1282 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283}
1284
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001285static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001288 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 switch (location) {
1292 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001293 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001295 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001296 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001297 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001298 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001299 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001300 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001301 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001302 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001303 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001304 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001305 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001306 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001307 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001308 case WINDOW_EXTERIOR:
1309 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001311 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001312 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001313}
1314
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001315static void
1316frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001317{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001318 switch (index) {
1319 case 0: /* close */
1320 if (window->close_handler)
1321 window->close_handler(window->parent,
1322 window->user_data);
1323 else
1324 display_exit(window->display);
1325 break;
1326 case 1: /* fullscreen */
1327 /* we don't have a way to get out of fullscreen for now */
1328 window_set_fullscreen(window, 1);
1329 break;
1330 case 2: /* rotate */
1331 case 3: /* scale */
1332 break;
1333 }
1334}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001336static int
1337frame_enter_handler(struct widget *widget,
1338 struct input *input, uint32_t time,
1339 int32_t x, int32_t y, void *data)
1340{
1341 return frame_get_pointer_image_for_location(data, input);
1342}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001344static int
1345frame_motion_handler(struct widget *widget,
1346 struct input *input, uint32_t time,
1347 int32_t x, int32_t y, void *data)
1348{
1349 return frame_get_pointer_image_for_location(data, input);
1350}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001352static void
1353break_grab(struct window *window)
1354{
1355 window->focus_widget = NULL;
1356 window->widget_grab_button = 0;
1357}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001358
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001359static void
1360frame_button_handler(struct widget *widget,
1361 struct input *input, uint32_t time,
1362 int button, int state, void *data)
1363
1364{
1365 struct frame *frame = data;
1366 struct window *window = widget->window;
1367 int location;
1368 int32_t x, y;
1369 static const char *entries[] = {
1370 "Close", "Fullscreen", "Rotate", "Scale"
1371 };
1372
1373 location = frame_get_pointer_location(frame, input->sx, input->sy);
1374
1375 if (window->display->shell && button == BTN_LEFT && state == 1) {
1376 switch (location) {
1377 case WINDOW_TITLEBAR:
1378 if (!window->shell_surface)
1379 break;
1380 input_set_pointer_image(input, time, POINTER_DRAGGING);
1381 break_grab(window);
1382 wl_shell_surface_move(window->shell_surface,
1383 input_get_input_device(input),
1384 time);
1385 break;
1386 case WINDOW_RESIZING_TOP:
1387 case WINDOW_RESIZING_BOTTOM:
1388 case WINDOW_RESIZING_LEFT:
1389 case WINDOW_RESIZING_RIGHT:
1390 case WINDOW_RESIZING_TOP_LEFT:
1391 case WINDOW_RESIZING_TOP_RIGHT:
1392 case WINDOW_RESIZING_BOTTOM_LEFT:
1393 case WINDOW_RESIZING_BOTTOM_RIGHT:
1394 if (!window->shell_surface)
1395 break;
1396 break_grab(window);
1397 wl_shell_surface_resize(window->shell_surface,
1398 input_get_input_device(input),
1399 time, location);
1400 break;
1401 }
1402 } else if (button == BTN_RIGHT && state == 1) {
1403 input_get_position(input, &x, &y);
1404 window->menu = window_create_menu(window->display,
1405 input, time,
1406 window,
1407 x - 10, y - 10,
1408 frame_menu_func,
1409 entries, 4);
1410 window_schedule_redraw(window->menu);
1411 }
1412}
1413
1414struct widget *
1415frame_create(struct window *window, void *data)
1416{
1417 struct frame *frame;
1418
1419 frame = malloc(sizeof *frame);
1420 memset(frame, 0, sizeof *frame);
1421
1422 frame->widget = window_add_widget(window, frame);
1423 frame->child = widget_add_widget(frame->widget, data);
1424 frame->margin = 16;
1425
1426 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1427 widget_set_resize_handler(frame->widget, frame_resize_handler);
1428 widget_set_enter_handler(frame->widget, frame_enter_handler);
1429 widget_set_motion_handler(frame->widget, frame_motion_handler);
1430 widget_set_button_handler(frame->widget, frame_button_handler);
1431
1432 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001433}
1434
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001435static void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001436window_set_focus_widget(struct window *window, struct widget *focus,
1437 struct input *input, uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001438{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001439 struct widget *old;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001440 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001442 if (focus == window->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001443 return;
1444
Kristian Høgsbergee143232012-01-09 08:42:24 -05001445 old = window->focus_widget;
1446 if (old) {
1447 if (old->leave_handler)
1448 old->leave_handler(old, input, old->user_data);
1449 window->focus_widget = NULL;
1450 }
1451
1452 if (focus) {
1453 if (focus->enter_handler)
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001454 pointer = focus->enter_handler(focus, input, time,
1455 x, y, focus->user_data);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001456 window->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001457
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001458 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001459 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001460}
1461
1462static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001463input_handle_motion(void *data, struct wl_input_device *input_device,
1464 uint32_t time,
1465 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001466{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001467 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001468 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001469 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001470 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001471
1472 input->x = x;
1473 input->y = y;
1474 input->sx = sx;
1475 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001476
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001477 if (!window->focus_widget || !window->widget_grab_button) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001478 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001479 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001480 }
1481
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001482 widget = window->focus_widget;
1483 if (widget && widget->motion_handler)
1484 pointer = widget->motion_handler(widget, input, time, sx, sy,
1485 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001486
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001487 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001488}
1489
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001490static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001491input_handle_button(void *data,
1492 struct wl_input_device *input_device,
1493 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001494{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001495 struct input *input = data;
1496 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001497 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001498
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001499 if (window->focus_widget && window->widget_grab_button == 0 && state)
1500 window->widget_grab_button = button;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001501
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001502 widget = window->focus_widget;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001503 if (widget && widget->button_handler)
1504 (*widget->button_handler)(widget,
1505 input, time,
1506 button, state,
1507 widget->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001508
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001509 if (window->focus_widget &&
1510 window->widget_grab_button == button && !state) {
1511 window->widget_grab_button = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001512 widget = widget_find_widget(window->widget,
1513 input->sx, input->sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001514 window_set_focus_widget(window, widget, input, time,
1515 input->sx, input->sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001516 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001517}
1518
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001519static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001520input_handle_key(void *data, struct wl_input_device *input_device,
1521 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001522{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001523 struct input *input = data;
1524 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001525 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001526 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001527
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001528 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001529 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001530 return;
1531
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001532 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001533 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001534 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1535 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001536
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001537 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1538
1539 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001540 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001541 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001542 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001543
1544 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001545 (*window->key_handler)(window, input, time, key, sym, state,
1546 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001547}
1548
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001549static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001550input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001551{
1552 struct window *window = input->pointer_focus;
1553
1554 if (!window)
1555 return;
1556
Kristian Høgsbergee143232012-01-09 08:42:24 -05001557 window_set_focus_widget(window, NULL, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001558
Pekka Paalanene1207c72011-12-16 12:02:09 +02001559 input->pointer_focus = NULL;
1560 input->current_pointer_image = POINTER_UNSET;
1561}
1562
1563static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001564input_handle_pointer_focus(void *data,
1565 struct wl_input_device *input_device,
1566 uint32_t time, struct wl_surface *surface,
1567 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001568{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001569 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001570 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001571 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001572
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001573 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001575 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001576
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001577 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001578 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001579 window = input->pointer_focus;
1580
Kristian Høgsberg59826582011-01-20 11:56:57 -05001581 input->x = x;
1582 input->y = y;
1583 input->sx = sx;
1584 input->sy = sy;
1585
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001587 window_set_focus_widget(window, widget, input, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001588 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001589}
1590
1591static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001592input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001593{
1594 struct window *window = input->keyboard_focus;
1595
1596 if (!window)
1597 return;
1598
1599 window->keyboard_device = NULL;
1600 if (window->keyboard_focus_handler)
1601 (*window->keyboard_focus_handler)(window, NULL,
1602 window->user_data);
1603
1604 input->keyboard_focus = NULL;
1605}
1606
1607static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001608input_handle_keyboard_focus(void *data,
1609 struct wl_input_device *input_device,
1610 uint32_t time,
1611 struct wl_surface *surface,
1612 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001613{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001614 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001615 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001616 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001617 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001618
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001619 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001620
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001621 if (surface)
1622 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001623
1624 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001625 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001626 for (k = keys->data; k < end; k++)
1627 input->modifiers |= d->xkb->map->modmap[*k];
1628
1629 window = input->keyboard_focus;
1630 if (window) {
1631 window->keyboard_device = input;
1632 if (window->keyboard_focus_handler)
1633 (*window->keyboard_focus_handler)(window,
1634 window->keyboard_device,
1635 window->user_data);
1636 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001637}
1638
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001639static void
1640input_handle_touch_down(void *data,
1641 struct wl_input_device *wl_input_device,
1642 uint32_t time, struct wl_surface *surface,
1643 int32_t id, int32_t x, int32_t y)
1644{
1645}
1646
1647static void
1648input_handle_touch_up(void *data,
1649 struct wl_input_device *wl_input_device,
1650 uint32_t time, int32_t id)
1651{
1652}
1653
1654static void
1655input_handle_touch_motion(void *data,
1656 struct wl_input_device *wl_input_device,
1657 uint32_t time, int32_t id, int32_t x, int32_t y)
1658{
1659}
1660
1661static void
1662input_handle_touch_frame(void *data,
1663 struct wl_input_device *wl_input_device)
1664{
1665}
1666
1667static void
1668input_handle_touch_cancel(void *data,
1669 struct wl_input_device *wl_input_device)
1670{
1671}
1672
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001673static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001674 input_handle_motion,
1675 input_handle_button,
1676 input_handle_key,
1677 input_handle_pointer_focus,
1678 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001679 input_handle_touch_down,
1680 input_handle_touch_up,
1681 input_handle_touch_motion,
1682 input_handle_touch_frame,
1683 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001684};
1685
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001686void
1687input_get_position(struct input *input, int32_t *x, int32_t *y)
1688{
1689 *x = input->sx;
1690 *y = input->sy;
1691}
1692
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001693struct wl_input_device *
1694input_get_input_device(struct input *input)
1695{
1696 return input->input_device;
1697}
1698
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001699uint32_t
1700input_get_modifiers(struct input *input)
1701{
1702 return input->modifiers;
1703}
1704
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001705struct data_offer {
1706 struct wl_data_offer *offer;
1707 struct input *input;
1708 struct wl_array types;
1709 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001710
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001711 struct task io_task;
1712 int fd;
1713 data_func_t func;
1714 int32_t x, y;
1715 void *user_data;
1716};
1717
1718static void
1719data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1720{
1721 struct data_offer *offer = data;
1722 char **p;
1723
1724 p = wl_array_add(&offer->types, sizeof *p);
1725 *p = strdup(type);
1726}
1727
1728static const struct wl_data_offer_listener data_offer_listener = {
1729 data_offer_offer,
1730};
1731
1732static void
1733data_offer_destroy(struct data_offer *offer)
1734{
1735 char **p;
1736
1737 offer->refcount--;
1738 if (offer->refcount == 0) {
1739 wl_data_offer_destroy(offer->offer);
1740 for (p = offer->types.data; *p; p++)
1741 free(*p);
1742 wl_array_release(&offer->types);
1743 free(offer);
1744 }
1745}
1746
1747static void
1748data_device_data_offer(void *data,
1749 struct wl_data_device *data_device, uint32_t id)
1750{
1751 struct data_offer *offer;
1752
1753 offer = malloc(sizeof *offer);
1754
1755 wl_array_init(&offer->types);
1756 offer->refcount = 1;
1757 offer->input = data;
1758
1759 /* FIXME: Generate typesafe wrappers for this */
1760 offer->offer = (struct wl_data_offer *)
1761 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1762 id, &wl_data_offer_interface);
1763
1764 wl_data_offer_add_listener(offer->offer,
1765 &data_offer_listener, offer);
1766}
1767
1768static void
1769data_device_enter(void *data, struct wl_data_device *data_device,
1770 uint32_t time, struct wl_surface *surface,
1771 int32_t x, int32_t y, struct wl_data_offer *offer)
1772{
1773 struct input *input = data;
1774 struct window *window;
1775 char **p;
1776
1777 input->drag_offer = wl_data_offer_get_user_data(offer);
1778 window = wl_surface_get_user_data(surface);
1779 input->pointer_focus = window;
1780
1781 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1782 *p = NULL;
1783
1784 window = input->pointer_focus;
1785 if (window->data_handler)
1786 window->data_handler(window, input, time, x, y,
1787 input->drag_offer->types.data,
1788 window->user_data);
1789}
1790
1791static void
1792data_device_leave(void *data, struct wl_data_device *data_device)
1793{
1794 struct input *input = data;
1795
1796 data_offer_destroy(input->drag_offer);
1797 input->drag_offer = NULL;
1798}
1799
1800static void
1801data_device_motion(void *data, struct wl_data_device *data_device,
1802 uint32_t time, int32_t x, int32_t y)
1803{
1804 struct input *input = data;
1805 struct window *window = input->pointer_focus;
1806
1807 input->sx = x;
1808 input->sy = y;
1809
1810 if (window->data_handler)
1811 window->data_handler(window, input, time, x, y,
1812 input->drag_offer->types.data,
1813 window->user_data);
1814}
1815
1816static void
1817data_device_drop(void *data, struct wl_data_device *data_device)
1818{
1819 struct input *input = data;
1820 struct window *window = input->pointer_focus;
1821
1822 if (window->drop_handler)
1823 window->drop_handler(window, input,
1824 input->sx, input->sy, window->user_data);
1825}
1826
1827static void
1828data_device_selection(void *data,
1829 struct wl_data_device *wl_data_device,
1830 struct wl_data_offer *offer)
1831{
1832 struct input *input = data;
1833 char **p;
1834
1835 if (input->selection_offer)
1836 data_offer_destroy(input->selection_offer);
1837
1838 input->selection_offer = wl_data_offer_get_user_data(offer);
1839 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1840 *p = NULL;
1841}
1842
1843static const struct wl_data_device_listener data_device_listener = {
1844 data_device_data_offer,
1845 data_device_enter,
1846 data_device_leave,
1847 data_device_motion,
1848 data_device_drop,
1849 data_device_selection
1850};
1851
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001852void
1853input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1854{
1855 struct display *display = input->display;
1856 struct wl_buffer *buffer;
1857 cairo_surface_t *surface;
1858
1859 if (pointer == input->current_pointer_image)
1860 return;
1861
1862 input->current_pointer_image = pointer;
1863 surface = display->pointer_surfaces[pointer];
1864
1865 if (!surface)
1866 return;
1867
1868 buffer = display_get_buffer_for_surface(display, surface);
1869 wl_input_device_attach(input->input_device, time, buffer,
1870 pointer_images[pointer].hotspot_x,
1871 pointer_images[pointer].hotspot_y);
1872}
1873
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001874struct wl_data_device *
1875input_get_data_device(struct input *input)
1876{
1877 return input->data_device;
1878}
1879
1880void
1881input_set_selection(struct input *input,
1882 struct wl_data_source *source, uint32_t time)
1883{
1884 wl_data_device_set_selection(input->data_device, source, time);
1885}
1886
1887void
1888input_accept(struct input *input, uint32_t time, const char *type)
1889{
1890 wl_data_offer_accept(input->drag_offer->offer, time, type);
1891}
1892
1893static void
1894offer_io_func(struct task *task, uint32_t events)
1895{
1896 struct data_offer *offer =
1897 container_of(task, struct data_offer, io_task);
1898 unsigned int len;
1899 char buffer[4096];
1900
1901 len = read(offer->fd, buffer, sizeof buffer);
1902 offer->func(buffer, len,
1903 offer->x, offer->y, offer->user_data);
1904
1905 if (len == 0) {
1906 close(offer->fd);
1907 data_offer_destroy(offer);
1908 }
1909}
1910
1911static void
1912data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1913 data_func_t func, void *user_data)
1914{
1915 int p[2];
1916
1917 pipe2(p, O_CLOEXEC);
1918 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1919 close(p[1]);
1920
1921 offer->io_task.run = offer_io_func;
1922 offer->fd = p[0];
1923 offer->func = func;
1924 offer->refcount++;
1925 offer->user_data = user_data;
1926
1927 display_watch_fd(offer->input->display,
1928 offer->fd, EPOLLIN, &offer->io_task);
1929}
1930
1931void
1932input_receive_drag_data(struct input *input, const char *mime_type,
1933 data_func_t func, void *data)
1934{
1935 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1936 input->drag_offer->x = input->sx;
1937 input->drag_offer->y = input->sy;
1938}
1939
1940int
1941input_receive_selection_data(struct input *input, const char *mime_type,
1942 data_func_t func, void *data)
1943{
1944 char **p;
1945
1946 if (input->selection_offer == NULL)
1947 return -1;
1948
1949 for (p = input->selection_offer->types.data; *p; p++)
1950 if (strcmp(mime_type, *p) == 0)
1951 break;
1952
1953 if (*p == NULL)
1954 return -1;
1955
1956 data_offer_receive_data(input->selection_offer,
1957 mime_type, func, data);
1958 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001959}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001960
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001961int
1962input_receive_selection_data_to_fd(struct input *input,
1963 const char *mime_type, int fd)
1964{
1965 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1966
1967 return 0;
1968}
1969
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001970void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001971window_move(struct window *window, struct input *input, uint32_t time)
1972{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001973 if (!window->shell_surface)
1974 return;
1975
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001976 wl_shell_surface_move(window->shell_surface,
1977 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001978}
1979
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001980static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001981window_resize(struct window *window, int32_t width, int32_t height)
1982{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001983 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001984 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001985
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001986 allocation.x = 0;
1987 allocation.y = 0;
1988 allocation.width = width;
1989 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001990
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001991 widget = window->widget;
1992 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001993 allocation.width, allocation.height);
1994
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001995 if (widget->resize_handler)
1996 widget->resize_handler(widget,
1997 allocation.width,
1998 allocation.height,
1999 widget->user_data);
2000
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002001 window->allocation = widget->allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002002
2003 window_schedule_redraw(window);
2004}
2005
2006static void
2007idle_resize(struct task *task, uint32_t events)
2008{
2009 struct window *window =
2010 container_of(task, struct window, resize_task);
2011
2012 window_resize(window,
2013 window->allocation.width, window->allocation.height);
2014 window->resize_scheduled = 0;
2015}
2016
2017void
2018window_schedule_resize(struct window *window, int width, int height)
2019{
2020 if (!window->resize_scheduled) {
2021 window->allocation.width = width;
2022 window->allocation.height = height;
2023 window->resize_task.run = idle_resize;
2024 display_defer(window->display, &window->resize_task);
2025 window->resize_scheduled = 1;
2026 }
2027}
2028
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002029void
2030widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2031{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002033}
2034
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002035static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002036handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002037 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002038 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002039{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002040 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002041
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002042 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002043 return;
2044
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002045 window->resize_edges = edges;
2046
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002047 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002048}
2049
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002050static void
2051handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2052{
2053 struct window *window = data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002054 struct menu *menu = window_get_user_data(window);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002055 /* FIXME: Need more context in this event, at least the input
2056 * device. Or just use wl_callback. */
2057
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002058 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002059 window_destroy(window);
2060}
2061
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002062static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002063 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002065};
2066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002067void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002068window_get_allocation(struct window *window,
2069 struct rectangle *allocation)
2070{
2071 *allocation = window->allocation;
2072}
2073
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002074static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002075widget_redraw(struct widget *widget)
2076{
2077 struct widget *child;
2078
2079 if (widget->redraw_handler)
2080 widget->redraw_handler(widget, widget->user_data);
2081 wl_list_for_each(child, &widget->child_list, link)
2082 widget_redraw(child);
2083}
2084
2085static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002086idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002087{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002088 struct window *window =
2089 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002090
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002091 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002092 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002093 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002094 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002095}
2096
2097void
2098window_schedule_redraw(struct window *window)
2099{
2100 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002101 window->redraw_task.run = idle_redraw;
2102 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002103 window->redraw_scheduled = 1;
2104 }
2105}
2106
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002107void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002108window_set_custom(struct window *window)
2109{
2110 window->type = TYPE_CUSTOM;
2111}
2112
2113void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002114window_set_fullscreen(struct window *window, int fullscreen)
2115{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002116 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002117 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002118
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002119 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002120 return;
2121
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002122 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002123 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002124 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002125 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002126 width = output->allocation.width;
2127 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002128 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002129 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002130 width = window->saved_allocation.width;
2131 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002132 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002133
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002134 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002135}
2136
2137void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002138window_set_user_data(struct window *window, void *data)
2139{
2140 window->user_data = data;
2141}
2142
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002143void *
2144window_get_user_data(struct window *window)
2145{
2146 return window->user_data;
2147}
2148
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002149void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002150window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002151 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002152{
2153 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002154}
2155
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002156void
2157window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002158 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002159{
2160 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002161}
2162
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002163void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002164window_set_data_handler(struct window *window, window_data_handler_t handler)
2165{
2166 window->data_handler = handler;
2167}
2168
2169void
2170window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2171{
2172 window->drop_handler = handler;
2173}
2174
2175void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002176window_set_close_handler(struct window *window,
2177 window_close_handler_t handler)
2178{
2179 window->close_handler = handler;
2180}
2181
2182void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002183window_set_transparent(struct window *window, int transparent)
2184{
2185 window->transparent = transparent;
2186}
2187
2188void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002189window_set_title(struct window *window, const char *title)
2190{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002191 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002192 window->title = strdup(title);
2193}
2194
2195const char *
2196window_get_title(struct window *window)
2197{
2198 return window->title;
2199}
2200
2201void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002202display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2203 int32_t x, int32_t y, int32_t width, int32_t height)
2204{
2205 struct wl_buffer *buffer;
2206
2207 buffer = display_get_buffer_for_surface(display, cairo_surface);
2208
2209 wl_buffer_damage(buffer, x, y, width, height);
2210}
2211
2212void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002213window_damage(struct window *window, int32_t x, int32_t y,
2214 int32_t width, int32_t height)
2215{
2216 wl_surface_damage(window->surface, x, y, width, height);
2217}
2218
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002219static struct window *
2220window_create_internal(struct display *display, struct window *parent,
2221 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002222{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002223 struct window *window;
2224
2225 window = malloc(sizeof *window);
2226 if (window == NULL)
2227 return NULL;
2228
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002229 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002230 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002231 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002232 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002233 if (display->shell) {
2234 window->shell_surface =
2235 wl_shell_get_shell_surface(display->shell,
2236 window->surface);
2237 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002238 window->allocation.x = 0;
2239 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002240 window->allocation.width = width;
2241 window->allocation.height = height;
2242 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002243 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002244
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002245 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002246#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002247 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2248 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002249#else
2250 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2251#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002252 else
2253 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002254
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002255 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002256 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002257
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002258 if (window->shell_surface) {
2259 wl_shell_surface_set_user_data(window->shell_surface, window);
2260 wl_shell_surface_add_listener(window->shell_surface,
2261 &shell_surface_listener, window);
2262 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002263
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002264 return window;
2265}
2266
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002267struct window *
2268window_create(struct display *display, int32_t width, int32_t height)
2269{
2270 struct window *window;
2271
2272 window = window_create_internal(display, NULL, width, height);
2273 if (!window)
2274 return NULL;
2275
2276 return window;
2277}
2278
2279struct window *
2280window_create_transient(struct display *display, struct window *parent,
2281 int32_t x, int32_t y, int32_t width, int32_t height)
2282{
2283 struct window *window;
2284
2285 window = window_create_internal(parent->display,
2286 parent, width, height);
2287 if (!window)
2288 return NULL;
2289
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002290 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002291 window->x = x;
2292 window->y = y;
2293
2294 return window;
2295}
2296
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002297static int
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002298menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002299{
2300 int next;
2301
2302 next = (sy - 8) / 20;
2303 if (menu->current != next) {
2304 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002305 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002306 }
2307
2308 return POINTER_LEFT_PTR;
2309}
2310
2311static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002312menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002313 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002314 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002315{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002316 return menu_set_item(data, y);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002317}
2318
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002319static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002320menu_enter_handler(struct widget *widget,
2321 struct input *input, uint32_t time,
2322 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002323{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002324 return menu_set_item(data, y);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002325}
2326
2327static void
2328menu_leave_handler(struct widget *widget, struct input *input, void *data)
2329{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002330 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002331}
2332
2333static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002334menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002335 struct input *input, uint32_t time,
2336 int button, int state, void *data)
2337
2338{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002339 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002340
2341 /* Either relase after press-drag-release or click-motion-click. */
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002342 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002343 menu->func(menu->window->parent,
2344 menu->current, menu->window->parent->user_data);
2345 window_destroy(widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002346 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002347}
2348
2349static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002350menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351{
2352 cairo_t *cr;
2353 const int32_t r = 3, margin = 3;
2354 struct menu *menu = data;
2355 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002356 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002357
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002358 cr = cairo_create(window->cairo_surface);
2359 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2360 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2361 cairo_paint(cr);
2362
2363 width = window->allocation.width;
2364 height = window->allocation.height;
2365 rounded_rect(cr, 0, 0, width, height, r);
2366 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2367 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2368 cairo_fill(cr);
2369
2370 for (i = 0; i < menu->count; i++) {
2371 if (i == menu->current) {
2372 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2373 cairo_rectangle(cr, margin, i * 20 + margin,
2374 width - 2 * margin, 20);
2375 cairo_fill(cr);
2376 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2377 cairo_move_to(cr, 10, i * 20 + 16);
2378 cairo_show_text(cr, menu->entries[i]);
2379 } else {
2380 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2381 cairo_move_to(cr, 10, i * 20 + 16);
2382 cairo_show_text(cr, menu->entries[i]);
2383 }
2384 }
2385
2386 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387}
2388
2389struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002390window_create_menu(struct display *display,
2391 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002392 int32_t x, int32_t y,
2393 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002394{
2395 struct window *window;
2396 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002397 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002398
2399 menu = malloc(sizeof *menu);
2400 if (!menu)
2401 return NULL;
2402
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002403 window = window_create_internal(parent->display, parent,
2404 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002405 if (!window)
2406 return NULL;
2407
2408 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002409 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002410 menu->entries = entries;
2411 menu->count = count;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002412 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002413 menu->func = func;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002414 window->type = TYPE_MENU;
2415 window->x = x;
2416 window->y = y;
2417
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002418 wl_shell_surface_set_popup(window->shell_surface,
2419 input->input_device, time,
2420 window->parent->shell_surface,
2421 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002422
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002423 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002424 widget_set_enter_handler(menu->widget, menu_enter_handler);
2425 widget_set_leave_handler(menu->widget, menu_leave_handler);
2426 widget_set_motion_handler(menu->widget, menu_motion_handler);
2427 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002428
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002429 return window;
2430}
2431
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002432void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002433window_set_buffer_type(struct window *window, enum window_buffer_type type)
2434{
2435 window->buffer_type = type;
2436}
2437
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002438
2439static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002440display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002441 struct wl_output *wl_output,
2442 int x, int y,
2443 int physical_width,
2444 int physical_height,
2445 int subpixel,
2446 const char *make,
2447 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002448{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002449 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002450
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002451 output->allocation.x = x;
2452 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002453}
2454
2455static void
2456display_handle_mode(void *data,
2457 struct wl_output *wl_output,
2458 uint32_t flags,
2459 int width,
2460 int height,
2461 int refresh)
2462{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002463 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002464 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002465
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002466 if (flags & WL_OUTPUT_MODE_CURRENT) {
2467 output->allocation.width = width;
2468 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002469 if (display->output_configure_handler)
2470 (*display->output_configure_handler)(
2471 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002472 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002473}
2474
2475static const struct wl_output_listener output_listener = {
2476 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002477 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002478};
2479
2480static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002481display_add_output(struct display *d, uint32_t id)
2482{
2483 struct output *output;
2484
2485 output = malloc(sizeof *output);
2486 if (output == NULL)
2487 return;
2488
2489 memset(output, 0, sizeof *output);
2490 output->display = d;
2491 output->output =
2492 wl_display_bind(d->display, id, &wl_output_interface);
2493 wl_list_insert(d->output_list.prev, &output->link);
2494
2495 wl_output_add_listener(output->output, &output_listener, output);
2496}
2497
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002498static void
2499output_destroy(struct output *output)
2500{
2501 if (output->destroy_handler)
2502 (*output->destroy_handler)(output, output->user_data);
2503
2504 wl_output_destroy(output->output);
2505 wl_list_remove(&output->link);
2506 free(output);
2507}
2508
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002509void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002510display_set_output_configure_handler(struct display *display,
2511 display_output_handler_t handler)
2512{
2513 struct output *output;
2514
2515 display->output_configure_handler = handler;
2516 if (!handler)
2517 return;
2518
2519 wl_list_for_each(output, &display->output_list, link)
2520 (*display->output_configure_handler)(output,
2521 display->user_data);
2522}
2523
2524void
2525output_set_user_data(struct output *output, void *data)
2526{
2527 output->user_data = data;
2528}
2529
2530void *
2531output_get_user_data(struct output *output)
2532{
2533 return output->user_data;
2534}
2535
2536void
2537output_set_destroy_handler(struct output *output,
2538 display_output_handler_t handler)
2539{
2540 output->destroy_handler = handler;
2541 /* FIXME: implement this, once we have way to remove outputs */
2542}
2543
2544void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002545output_get_allocation(struct output *output, struct rectangle *allocation)
2546{
2547 *allocation = output->allocation;
2548}
2549
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002550struct wl_output *
2551output_get_wl_output(struct output *output)
2552{
2553 return output->output;
2554}
2555
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002556static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002557display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002558{
2559 struct input *input;
2560
2561 input = malloc(sizeof *input);
2562 if (input == NULL)
2563 return;
2564
2565 memset(input, 0, sizeof *input);
2566 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002567 input->input_device =
2568 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002569 input->pointer_focus = NULL;
2570 input->keyboard_focus = NULL;
2571 wl_list_insert(d->input_list.prev, &input->link);
2572
2573 wl_input_device_add_listener(input->input_device,
2574 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002575 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002576
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002577 input->data_device =
2578 wl_data_device_manager_get_data_device(d->data_device_manager,
2579 input->input_device);
2580 wl_data_device_add_listener(input->data_device,
2581 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002582}
2583
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002584static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002585input_destroy(struct input *input)
2586{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002587 input_remove_keyboard_focus(input);
2588 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002589
2590 if (input->drag_offer)
2591 data_offer_destroy(input->drag_offer);
2592
2593 if (input->selection_offer)
2594 data_offer_destroy(input->selection_offer);
2595
2596 wl_data_device_destroy(input->data_device);
2597 wl_list_remove(&input->link);
2598 wl_input_device_destroy(input->input_device);
2599 free(input);
2600}
2601
2602static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002603display_handle_global(struct wl_display *display, uint32_t id,
2604 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002605{
2606 struct display *d = data;
2607
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002608 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002609 d->compositor =
2610 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002611 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002612 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002613 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002614 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002615 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002616 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002617 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002618 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002619 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2620 d->data_device_manager =
2621 wl_display_bind(display, id,
2622 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002623 }
2624}
2625
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002626static void
2627display_render_frame(struct display *d)
2628{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002629 int radius = 8;
2630 cairo_t *cr;
2631
2632 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2633 cr = cairo_create(d->shadow);
2634 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2635 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2636 rounded_rect(cr, 16, 16, 112, 112, radius);
2637 cairo_fill(cr);
2638 cairo_destroy(cr);
2639 blur_surface(d->shadow, 64);
2640
2641 d->active_frame =
2642 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2643 cr = cairo_create(d->active_frame);
2644 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2645 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2646 rounded_rect(cr, 16, 16, 112, 112, radius);
2647 cairo_fill(cr);
2648 cairo_destroy(cr);
2649
2650 d->inactive_frame =
2651 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2652 cr = cairo_create(d->inactive_frame);
2653 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2654 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2655 rounded_rect(cr, 16, 16, 112, 112, radius);
2656 cairo_fill(cr);
2657 cairo_destroy(cr);
2658}
2659
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002660static void
2661init_xkb(struct display *d)
2662{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002663 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002664
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002665 names.rules = "evdev";
2666 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002667 names.layout = option_xkb_layout;
2668 names.variant = option_xkb_variant;
2669 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002670
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002671 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002672 if (!d->xkb) {
2673 fprintf(stderr, "Failed to compile keymap\n");
2674 exit(1);
2675 }
2676}
2677
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002678static void
2679fini_xkb(struct display *display)
2680{
2681 xkb_free_keymap(display->xkb);
2682}
2683
Yuval Fledel45568f62010-12-06 09:18:12 -05002684static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002685init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002686{
2687 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002688 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002689
2690 static const EGLint premul_argb_cfg_attribs[] = {
2691 EGL_SURFACE_TYPE,
2692 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2693 EGL_VG_ALPHA_FORMAT_PRE_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002694 EGL_RED_SIZE, 1,
2695 EGL_GREEN_SIZE, 1,
2696 EGL_BLUE_SIZE, 1,
2697 EGL_ALPHA_SIZE, 1,
2698 EGL_DEPTH_SIZE, 1,
2699 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2700 EGL_NONE
2701 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002702
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002703 static const EGLint rgb_cfg_attribs[] = {
2704 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2705 EGL_RED_SIZE, 1,
2706 EGL_GREEN_SIZE, 1,
2707 EGL_BLUE_SIZE, 1,
2708 EGL_ALPHA_SIZE, 0,
2709 EGL_DEPTH_SIZE, 1,
2710 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2711 EGL_NONE
2712 };
2713
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002714 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002715 if (!eglInitialize(d->dpy, &major, &minor)) {
2716 fprintf(stderr, "failed to initialize display\n");
2717 return -1;
2718 }
2719
2720 if (!eglBindAPI(EGL_OPENGL_API)) {
2721 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2722 return -1;
2723 }
2724
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002725 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
Benjamin Franzke4b87a132011-09-01 10:36:16 +02002726 &d->premultiplied_argb_config, 1, &n) || n != 1) {
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002727 fprintf(stderr, "failed to choose premul argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002728 return -1;
2729 }
2730
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002731 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2732 &d->rgb_config, 1, &n) || n != 1) {
2733 fprintf(stderr, "failed to choose rgb config\n");
2734 return -1;
2735 }
2736
Benjamin Franzke0c991632011-09-27 21:57:31 +02002737 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2738 if (d->rgb_ctx == NULL) {
2739 fprintf(stderr, "failed to create context\n");
2740 return -1;
2741 }
2742 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2743 EGL_NO_CONTEXT, NULL);
2744 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002745 fprintf(stderr, "failed to create context\n");
2746 return -1;
2747 }
2748
Benjamin Franzke0c991632011-09-27 21:57:31 +02002749 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002750 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002751 return -1;
2752 }
2753
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002754#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002755 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2756 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002757 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002758 return -1;
2759 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002760 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2761 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2762 fprintf(stderr, "failed to get cairo egl argb device\n");
2763 return -1;
2764 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002765#endif
2766
2767 return 0;
2768}
2769
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002770static void
2771fini_egl(struct display *display)
2772{
2773#ifdef HAVE_CAIRO_EGL
2774 cairo_device_destroy(display->argb_device);
2775 cairo_device_destroy(display->rgb_device);
2776#endif
2777
2778 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2779 EGL_NO_CONTEXT);
2780
2781 eglTerminate(display->dpy);
2782 eglReleaseThread();
2783}
2784
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002785static int
2786event_mask_update(uint32_t mask, void *data)
2787{
2788 struct display *d = data;
2789
2790 d->mask = mask;
2791
2792 return 0;
2793}
2794
2795static void
2796handle_display_data(struct task *task, uint32_t events)
2797{
2798 struct display *display =
2799 container_of(task, struct display, display_task);
2800
2801 wl_display_iterate(display->display, display->mask);
2802}
2803
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002804struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002805display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002806{
2807 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002808 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002809 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002810 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002811
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002812 g_type_init();
2813
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002814 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002815 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002816 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002817
2818 xkb_option_group = g_option_group_new("xkb",
2819 "Keyboard options",
2820 "Show all XKB options",
2821 NULL, NULL);
2822 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2823 g_option_context_add_group (context, xkb_option_group);
2824
2825 if (!g_option_context_parse(context, argc, argv, &error)) {
2826 fprintf(stderr, "option parsing failed: %s\n", error->message);
2827 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002828 }
2829
Tim Wiederhake748f6722011-01-23 23:25:25 +01002830 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002831
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002832 d = malloc(sizeof *d);
2833 if (d == NULL)
2834 return NULL;
2835
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002836 memset(d, 0, sizeof *d);
2837
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002838 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002839 if (d->display == NULL) {
2840 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002841 return NULL;
2842 }
2843
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002844 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2845 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2846 d->display_task.run = handle_display_data;
2847 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2848
2849 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002850 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002851 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002852
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002853 /* Set up listener so we'll catch all events. */
2854 wl_display_add_global_listener(d->display,
2855 display_handle_global, d);
2856
2857 /* Process connection events. */
2858 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002859 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002860 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002861
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002862 d->image_target_texture_2d =
2863 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2864 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2865 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2866
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002867 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002868
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002869 display_render_frame(d);
2870
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002871 wl_list_init(&d->window_list);
2872
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002873 init_xkb(d);
2874
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002875 return d;
2876}
2877
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002878static void
2879display_destroy_outputs(struct display *display)
2880{
2881 struct output *tmp;
2882 struct output *output;
2883
2884 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2885 output_destroy(output);
2886}
2887
Pekka Paalanene1207c72011-12-16 12:02:09 +02002888static void
2889display_destroy_inputs(struct display *display)
2890{
2891 struct input *tmp;
2892 struct input *input;
2893
2894 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2895 input_destroy(input);
2896}
2897
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002898void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002899display_destroy(struct display *display)
2900{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002901 if (!wl_list_empty(&display->window_list))
2902 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2903
2904 if (!wl_list_empty(&display->deferred_list))
2905 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2906
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002907 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002908 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002909
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002910 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002911
2912 cairo_surface_destroy(display->active_frame);
2913 cairo_surface_destroy(display->inactive_frame);
2914 cairo_surface_destroy(display->shadow);
2915 destroy_pointer_surfaces(display);
2916
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002917 fini_egl(display);
2918
Pekka Paalanenc2052982011-12-16 11:41:32 +02002919 if (display->shell)
2920 wl_shell_destroy(display->shell);
2921
2922 if (display->shm)
2923 wl_shm_destroy(display->shm);
2924
2925 if (display->data_device_manager)
2926 wl_data_device_manager_destroy(display->data_device_manager);
2927
2928 wl_compositor_destroy(display->compositor);
2929
2930 close(display->epoll_fd);
2931
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002932 wl_display_flush(display->display);
2933 wl_display_destroy(display->display);
2934 free(display);
2935}
2936
2937void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002938display_set_user_data(struct display *display, void *data)
2939{
2940 display->user_data = data;
2941}
2942
2943void *
2944display_get_user_data(struct display *display)
2945{
2946 return display->user_data;
2947}
2948
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002949struct wl_display *
2950display_get_display(struct display *display)
2951{
2952 return display->display;
2953}
2954
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002955struct output *
2956display_get_output(struct display *display)
2957{
2958 return container_of(display->output_list.next, struct output, link);
2959}
2960
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002961struct wl_compositor *
2962display_get_compositor(struct display *display)
2963{
2964 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002965}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002966
2967EGLDisplay
2968display_get_egl_display(struct display *d)
2969{
2970 return d->dpy;
2971}
2972
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002973struct wl_data_source *
2974display_create_data_source(struct display *display)
2975{
2976 return wl_data_device_manager_create_data_source(display->data_device_manager);
2977}
2978
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002979EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02002980display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002981{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002982 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002983}
2984
Benjamin Franzke0c991632011-09-27 21:57:31 +02002985EGLConfig
2986display_get_argb_egl_config(struct display *d)
2987{
2988 return d->premultiplied_argb_config;
2989}
2990
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002991struct wl_shell *
2992display_get_shell(struct display *display)
2993{
2994 return display->shell;
2995}
2996
Benjamin Franzke1a89f282011-10-07 09:33:06 +02002997int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002998display_acquire_window_surface(struct display *display,
2999 struct window *window,
3000 EGLContext ctx)
3001{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003002#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003003 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003004 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003005
3006 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003007 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003008 device = cairo_surface_get_device(window->cairo_surface);
3009 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003010 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003011
Benjamin Franzke0c991632011-09-27 21:57:31 +02003012 if (!ctx) {
3013 if (device == display->rgb_device)
3014 ctx = display->rgb_ctx;
3015 else if (device == display->argb_device)
3016 ctx = display->argb_ctx;
3017 else
3018 assert(0);
3019 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003020
3021 data = cairo_surface_get_user_data(window->cairo_surface,
3022 &surface_data_key);
3023
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003024 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003025 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003026 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3027 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003028
3029 return 0;
3030#else
3031 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003032#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003033}
3034
3035void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003036display_release_window_surface(struct display *display,
3037 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003038{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003039#ifdef HAVE_CAIRO_EGL
3040 cairo_device_t *device;
3041
3042 device = cairo_surface_get_device(window->cairo_surface);
3043 if (!device)
3044 return;
3045
3046 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003048 cairo_device_release(device);
3049#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003050}
3051
3052void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003053display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003054{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003055 wl_list_insert(&display->deferred_list, &task->link);
3056}
3057
3058void
3059display_watch_fd(struct display *display,
3060 int fd, uint32_t events, struct task *task)
3061{
3062 struct epoll_event ep;
3063
3064 ep.events = events;
3065 ep.data.ptr = task;
3066 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3067}
3068
3069void
3070display_run(struct display *display)
3071{
3072 struct task *task;
3073 struct epoll_event ep[16];
3074 int i, count;
3075
Pekka Paalanen826d7952011-12-15 10:14:07 +02003076 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003077 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003078 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003079
3080 while (!wl_list_empty(&display->deferred_list)) {
3081 task = container_of(display->deferred_list.next,
3082 struct task, link);
3083 wl_list_remove(&task->link);
3084 task->run(task, 0);
3085 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003086
3087 if (!display->running)
3088 break;
3089
3090 wl_display_flush(display->display);
3091
3092 count = epoll_wait(display->epoll_fd,
3093 ep, ARRAY_LENGTH(ep), -1);
3094 for (i = 0; i < count; i++) {
3095 task = ep[i].data.ptr;
3096 task->run(task, ep[i].events);
3097 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003098 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003099}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003100
3101void
3102display_exit(struct display *display)
3103{
3104 display->running = 0;
3105}