blob: 5264d878fe09ff9fcac56f6788748a317bca118d [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;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050068 EGLConfig 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øgsberg0d1c0622012-01-31 15:30:47 -0500114 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400116 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400117 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500119 int resize_scheduled;
120 struct task resize_task;
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øgsbergd0c3b9d2010-10-25 11:40:03 -0400124 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500125
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500126 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500127
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500128 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500129 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400130 window_data_handler_t data_handler;
131 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500132 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400133
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200134 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500135 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500136
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500137 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400138 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500139};
140
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500141struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500142 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500143 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400144 struct wl_list link;
145 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500146 widget_resize_handler_t resize_handler;
147 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500148 widget_enter_handler_t enter_handler;
149 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500150 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500151 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400152 void *user_data;
153};
154
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400155struct input {
156 struct display *display;
157 struct wl_input_device *input_device;
158 struct window *pointer_focus;
159 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400160 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400161 uint32_t modifiers;
162 int32_t x, y, sx, sy;
163 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400164
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500165 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500166 struct widget *grab;
167 uint32_t grab_button;
168
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400169 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øgsberg831dd522012-01-10 23:46:33 -0500193 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500194 const char **entries;
195 uint32_t time;
196 int current;
197 int count;
198 menu_func_t func;
199};
200
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400201enum {
202 POINTER_DEFAULT = 100,
203 POINTER_UNSET
204};
205
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500206enum window_location {
207 WINDOW_INTERIOR = 0,
208 WINDOW_RESIZING_TOP = 1,
209 WINDOW_RESIZING_BOTTOM = 2,
210 WINDOW_RESIZING_LEFT = 4,
211 WINDOW_RESIZING_TOP_LEFT = 5,
212 WINDOW_RESIZING_BOTTOM_LEFT = 6,
213 WINDOW_RESIZING_RIGHT = 8,
214 WINDOW_RESIZING_TOP_RIGHT = 9,
215 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
216 WINDOW_RESIZING_MASK = 15,
217 WINDOW_EXTERIOR = 16,
218 WINDOW_TITLEBAR = 17,
219 WINDOW_CLIENT_AREA = 18,
220};
221
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400222const char *option_xkb_layout = "us";
223const char *option_xkb_variant = "";
224const char *option_xkb_options = "";
225
226static const GOptionEntry xkb_option_entries[] = {
227 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
228 &option_xkb_layout, "XKB Layout" },
229 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
230 &option_xkb_variant, "XKB Variant" },
231 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
232 &option_xkb_options, "XKB Options" },
233 { NULL }
234};
235
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400236static const cairo_user_data_key_t surface_data_key;
237struct surface_data {
238 struct wl_buffer *buffer;
239};
240
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500241#define MULT(_d,c,a,t) \
242 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
243
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500244#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400245
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100246struct egl_window_surface_data {
247 struct display *display;
248 struct wl_surface *surface;
249 struct wl_egl_window *window;
250 EGLSurface surf;
251};
252
253static void
254egl_window_surface_data_destroy(void *p)
255{
256 struct egl_window_surface_data *data = p;
257 struct display *d = data->display;
258
259 eglDestroySurface(d->dpy, data->surf);
260 wl_egl_window_destroy(data->window);
261 data->surface = NULL;
262
263 free(p);
264}
265
266static cairo_surface_t *
267display_create_egl_window_surface(struct display *display,
268 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400269 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100270 struct rectangle *rectangle)
271{
272 cairo_surface_t *cairo_surface;
273 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400274 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200275 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276
277 data = malloc(sizeof *data);
278 if (data == NULL)
279 return NULL;
280
281 data->display = display;
282 data->surface = surface;
283
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400284 if (flags & SURFACE_OPAQUE) {
285 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400287 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500288 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200289 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400290 }
291
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400292 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100293 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400294 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100295
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500297 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100298
Benjamin Franzke0c991632011-09-27 21:57:31 +0200299 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100300 data->surf,
301 rectangle->width,
302 rectangle->height);
303
304 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
305 data, egl_window_surface_data_destroy);
306
307 return cairo_surface;
308}
309
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500310struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400311 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200312 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400313 EGLImageKHR image;
314 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800315 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317};
318
319static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500320egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400321{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324
Benjamin Franzke0c991632011-09-27 21:57:31 +0200325 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400326 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200327 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800328
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500329 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500331 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500332 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400333}
334
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500335EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336display_get_image_for_egl_image_surface(struct display *display,
337 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500338{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500339 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500340
341 data = cairo_surface_get_user_data (surface, &surface_data_key);
342
343 return data->image;
344}
345
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500346static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500347display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400348 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500349 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400350{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400352 EGLDisplay dpy = display->dpy;
353 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200354 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400355
356 data = malloc(sizeof *data);
357 if (data == NULL)
358 return NULL;
359
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800360 data->display = display;
361
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400362 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400363 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500364 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000365 free(data);
366 return NULL;
367 }
368
Benjamin Franzke0c991632011-09-27 21:57:31 +0200369 if (flags & SURFACE_OPAQUE) {
370 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200371 content = CAIRO_CONTENT_COLOR;
372 } else {
373 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200374 content = CAIRO_CONTENT_COLOR_ALPHA;
375 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400376
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500377 data->image = display->create_image(dpy, NULL,
378 EGL_NATIVE_PIXMAP_KHR,
379 (EGLClientBuffer) data->pixmap,
380 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500381 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500382 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 free(data);
384 return NULL;
385 }
386
387 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400388 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500389
Benjamin Franzke0c991632011-09-27 21:57:31 +0200390 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500393 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200394 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400395
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 surface = cairo_gl_surface_create_for_texture(data->device,
397 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 data->texture,
399 rectangle->width,
400 rectangle->height);
401
402 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500403 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404
405 return surface;
406}
407
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500408static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500409display_create_egl_image_surface_from_file(struct display *display,
410 const char *filename,
411 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412{
413 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400414 GdkPixbuf *pixbuf;
415 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400416 int stride, i;
417 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500418 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
420 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400421 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400422 FALSE, &error);
423 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400426 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
427 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500428 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400430 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432
433 stride = gdk_pixbuf_get_rowstride(pixbuf);
434 pixels = gdk_pixbuf_get_pixels(pixbuf);
435
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400436 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400437 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400439 while (p < end) {
440 unsigned int t;
441
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400442 MULT(p[0], p[0], p[3], t);
443 MULT(p[1], p[1], p[3], t);
444 MULT(p[2], p[2], p[3], t);
445 p += 4;
446
447 }
448 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400449
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200450 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000451 if (surface == NULL) {
452 g_object_unref(pixbuf);
453 return NULL;
454 }
455
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800456 data = cairo_surface_get_user_data(surface, &surface_data_key);
457
Benjamin Franzke0c991632011-09-27 21:57:31 +0200458 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800459 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800460 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
461 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200462 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400463
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500464 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400466 return surface;
467}
468
469#endif
470
471struct wl_buffer *
472display_get_buffer_for_surface(struct display *display,
473 cairo_surface_t *surface)
474{
475 struct surface_data *data;
476
477 data = cairo_surface_get_user_data (surface, &surface_data_key);
478
479 return data->buffer;
480}
481
482struct shm_surface_data {
483 struct surface_data data;
484 void *map;
485 size_t length;
486};
487
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500488static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489shm_surface_data_destroy(void *p)
490{
491 struct shm_surface_data *data = p;
492
493 wl_buffer_destroy(data->data.buffer);
494 munmap(data->map, data->length);
495}
496
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500497static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400499 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500{
501 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400502 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400503 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800504 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505 char filename[] = "/tmp/wayland-shm-XXXXXX";
506
507 data = malloc(sizeof *data);
508 if (data == NULL)
509 return NULL;
510
511 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
512 rectangle->width);
513 data->length = stride * rectangle->height;
514 fd = mkstemp(filename);
515 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000516 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517 return NULL;
518 }
519 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000520 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400521 close(fd);
522 return NULL;
523 }
524
525 data->map = mmap(NULL, data->length,
526 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
527 unlink(filename);
528
529 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000530 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400531 close(fd);
532 return NULL;
533 }
534
535 surface = cairo_image_surface_create_for_data (data->map,
536 CAIRO_FORMAT_ARGB32,
537 rectangle->width,
538 rectangle->height,
539 stride);
540
541 cairo_surface_set_user_data (surface, &surface_data_key,
542 data, shm_surface_data_destroy);
543
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400544 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500545 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400546 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500547 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400548
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549 data->data.buffer = wl_shm_create_buffer(display->shm,
550 fd,
551 rectangle->width,
552 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400553 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400554
555 close(fd);
556
557 return surface;
558}
559
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500560static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400561display_create_shm_surface_from_file(struct display *display,
562 const char *filename,
563 struct rectangle *rect)
564{
565 cairo_surface_t *surface;
566 GdkPixbuf *pixbuf;
567 GError *error = NULL;
568 int stride, i;
569 unsigned char *pixels, *p, *end, *dest_data;
570 int dest_stride;
571 uint32_t *d;
572
573 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
574 rect->width, rect->height,
575 FALSE, &error);
576 if (error != NULL)
577 return NULL;
578
579 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
580 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500581 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400582 return NULL;
583 }
584
585 stride = gdk_pixbuf_get_rowstride(pixbuf);
586 pixels = gdk_pixbuf_get_pixels(pixbuf);
587
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400588 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000589 if (surface == NULL) {
590 g_object_unref(pixbuf);
591 return NULL;
592 }
593
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400594 dest_data = cairo_image_surface_get_data (surface);
595 dest_stride = cairo_image_surface_get_stride (surface);
596
597 for (i = 0; i < rect->height; i++) {
598 d = (uint32_t *) (dest_data + i * dest_stride);
599 p = pixels + i * stride;
600 end = p + rect->width * 4;
601 while (p < end) {
602 unsigned int t;
603 unsigned char a, r, g, b;
604
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400605 a = p[3];
606 MULT(r, p[0], a, t);
607 MULT(g, p[1], a, t);
608 MULT(b, p[2], a, t);
609 p += 4;
610 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
611 }
612 }
613
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500614 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400615
616 return surface;
617}
618
nobled7b87cb02011-02-01 18:51:47 +0000619static int
620check_size(struct rectangle *rect)
621{
622 if (rect->width && rect->height)
623 return 0;
624
625 fprintf(stderr, "tried to create surface of "
626 "width: %d, height: %d\n", rect->width, rect->height);
627 return -1;
628}
629
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400630cairo_surface_t *
631display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400633 struct rectangle *rectangle,
634 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400635{
nobled7b87cb02011-02-01 18:51:47 +0000636 if (check_size(rectangle) < 0)
637 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500638#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500639 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640 if (surface)
641 return display_create_egl_window_surface(display,
642 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400643 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644 rectangle);
645 else
646 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400647 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500649 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400651 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400652}
653
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500654static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655display_create_surface_from_file(struct display *display,
656 const char *filename,
657 struct rectangle *rectangle)
658{
nobled7b87cb02011-02-01 18:51:47 +0000659 if (check_size(rectangle) < 0)
660 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500661#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500662 if (display->dpy) {
663 return display_create_egl_image_surface_from_file(display,
664 filename,
665 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500666 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500668 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400669}
Yuval Fledel45568f62010-12-06 09:18:12 -0500670 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 const char *filename;
672 int hotspot_x, hotspot_y;
673} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500674 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
675 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
676 { DATADIR "/weston/bottom_side.png", 16, 20 },
677 { DATADIR "/weston/grabbing.png", 20, 17 },
678 { DATADIR "/weston/left_ptr.png", 10, 5 },
679 { DATADIR "/weston/left_side.png", 10, 20 },
680 { DATADIR "/weston/right_side.png", 30, 19 },
681 { DATADIR "/weston/top_left_corner.png", 8, 8 },
682 { DATADIR "/weston/top_right_corner.png", 26, 8 },
683 { DATADIR "/weston/top_side.png", 18, 8 },
684 { DATADIR "/weston/xterm.png", 15, 15 },
685 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400686};
687
688static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400689create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400690{
691 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400693 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400695 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400696 display->pointer_surfaces =
697 malloc(count * sizeof *display->pointer_surfaces);
698 rect.width = width;
699 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400700 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400701 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702 display_create_surface_from_file(display,
703 pointer_images[i].filename,
704 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100705 if (!display->pointer_surfaces[i]) {
706 fprintf(stderr, "Error loading pointer image: %s\n",
707 pointer_images[i].filename);
708 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400709 }
710
711}
712
Pekka Paalanen325bb602011-12-19 10:31:45 +0200713static void
714destroy_pointer_surfaces(struct display *display)
715{
716 int i, count;
717
718 count = ARRAY_LENGTH(pointer_images);
719 for (i = 0; i < count; ++i) {
720 if (display->pointer_surfaces[i])
721 cairo_surface_destroy(display->pointer_surfaces[i]);
722 }
723 free(display->pointer_surfaces);
724}
725
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400726cairo_surface_t *
727display_get_pointer_surface(struct display *display, int pointer,
728 int *width, int *height,
729 int *hotspot_x, int *hotspot_y)
730{
731 cairo_surface_t *surface;
732
733 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500734#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735 *width = cairo_gl_surface_get_width(surface);
736 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000737#else
738 *width = cairo_image_surface_get_width(surface);
739 *height = cairo_image_surface_get_height(surface);
740#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400741 *hotspot_x = pointer_images[pointer].hotspot_x;
742 *hotspot_y = pointer_images[pointer].hotspot_y;
743
744 return cairo_surface_reference(surface);
745}
746
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400747static void
748window_attach_surface(struct window *window);
749
750static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400751free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400752{
753 struct window *window = data;
754
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400755 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400756 cairo_surface_destroy(window->pending_surface);
757 window->pending_surface = NULL;
758 if (window->cairo_surface)
759 window_attach_surface(window);
760}
761
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400762static const struct wl_callback_listener free_surface_listener = {
763 free_surface
764};
765
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500766static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200767window_get_resize_dx_dy(struct window *window, int *x, int *y)
768{
769 if (window->resize_edges & WINDOW_RESIZING_LEFT)
770 *x = window->server_allocation.width - window->allocation.width;
771 else
772 *x = 0;
773
774 if (window->resize_edges & WINDOW_RESIZING_TOP)
775 *y = window->server_allocation.height -
776 window->allocation.height;
777 else
778 *y = 0;
779
780 window->resize_edges = 0;
781}
782
783static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400784window_set_type(struct window *window)
785{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200786 if (!window->shell_surface)
787 return;
788
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400789 switch (window->type) {
790 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200791 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400792 break;
793 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200794 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400795 break;
796 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wl_shell_surface_set_transient(window->shell_surface,
798 window->parent->shell_surface,
799 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400800 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500801 case TYPE_MENU:
802 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400803 case TYPE_CUSTOM:
804 break;
805 }
806}
807
808static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500809window_attach_surface(struct window *window)
810{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400811 struct display *display = window->display;
812 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400813 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100815 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000816#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500817 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100818
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400819 if (display->shell)
820 window_set_type(window);
821
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100822 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000823#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100824 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
825 data = cairo_surface_get_user_data(window->cairo_surface,
826 &surface_data_key);
827
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828 cairo_gl_surface_swapbuffers(window->cairo_surface);
829 wl_egl_window_get_attached_size(data->window,
830 &window->server_allocation.width,
831 &window->server_allocation.height);
832 break;
833 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000834#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100835 case WINDOW_BUFFER_TYPE_SHM:
836 if (window->pending_surface != NULL)
837 return;
838
839 window->pending_surface = window->cairo_surface;
840 window->cairo_surface = NULL;
841
842 buffer =
843 display_get_buffer_for_surface(display,
844 window->pending_surface);
845
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200846 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100847 wl_surface_attach(window->surface, buffer, x, y);
848 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400849 cb = wl_display_sync(display->display);
850 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000852 default:
853 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500855
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856 wl_surface_damage(window->surface, 0, 0,
857 window->allocation.width,
858 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500859}
860
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500861void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400862window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100864 if (window->cairo_surface) {
865 switch (window->buffer_type) {
866 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
867 case WINDOW_BUFFER_TYPE_SHM:
868 display_surface_damage(window->display,
869 window->cairo_surface,
870 0, 0,
871 window->allocation.width,
872 window->allocation.height);
873 break;
874 default:
875 break;
876 }
877 window_attach_surface(window);
878 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500879}
880
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881void
882window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500884 cairo_surface_reference(surface);
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
888
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890}
891
Benjamin Franzke22d54812011-07-16 19:50:32 +0000892#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893static void
894window_resize_cairo_window_surface(struct window *window)
895{
896 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898
899 data = cairo_surface_get_user_data(window->cairo_surface,
900 &surface_data_key);
901
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 wl_egl_window_resize(data->window,
904 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window->allocation.height,
906 x,y);
907
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 cairo_gl_surface_set_size(window->cairo_surface,
909 window->allocation.width,
910 window->allocation.height);
911}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000912#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400914struct display *
915window_get_display(struct window *window)
916{
917 return window->display;
918}
919
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920void
921window_create_surface(struct window *window)
922{
923 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 uint32_t flags = 0;
925
926 if (!window->transparent)
927 flags = SURFACE_OPAQUE;
928
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500930#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
932 if (window->cairo_surface) {
933 window_resize_cairo_window_surface(window);
934 return;
935 }
936 surface = display_create_surface(window->display,
937 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400938 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500940 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400941 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944 break;
945#endif
946 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400947 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400948 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800950 default:
951 surface = NULL;
952 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400954
955 window_set_surface(window, surface);
956 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957}
958
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200959static void frame_destroy(struct frame *frame);
960
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400961void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500962window_destroy(struct window *window)
963{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200964 struct display *display = window->display;
965 struct input *input;
966
967 if (window->redraw_scheduled)
968 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500969 if (window->resize_scheduled)
970 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200971
972 wl_list_for_each(input, &display->input_list, link) {
973 if (input->pointer_focus == window)
974 input->pointer_focus = NULL;
975 if (input->keyboard_focus == window)
976 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500977 if (input->focus_widget &&
978 input->focus_widget->window == window)
979 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200980 }
981
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200982 if (window->frame)
983 frame_destroy(window->frame);
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{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001058 struct display *display = widget->window->display;
1059 struct input *input;
1060
1061 wl_list_for_each(input, &display->input_list, link) {
1062 if (input->focus_widget == widget)
1063 input->focus_widget = NULL;
1064 }
1065
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001066 wl_list_remove(&widget->link);
1067 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001068}
1069
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001070void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001072{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001073 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001074}
1075
1076void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001077widget_set_size(struct widget *widget, int32_t width, int32_t height)
1078{
1079 struct window *window = widget->window;
1080
1081 widget->allocation.width = width;
1082 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001083}
1084
1085void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086widget_set_allocation(struct widget *widget,
1087 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001088{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001089 widget->allocation.x = x;
1090 widget->allocation.y = y;
1091 widget->allocation.width = width;
1092 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001093}
1094
1095void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001098 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001099}
1100
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001101void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001102widget_set_resize_handler(struct widget *widget,
1103 widget_resize_handler_t handler)
1104{
1105 widget->resize_handler = handler;
1106}
1107
1108void
1109widget_set_redraw_handler(struct widget *widget,
1110 widget_redraw_handler_t handler)
1111{
1112 widget->redraw_handler = handler;
1113}
1114
1115void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001116widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001117{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001118 widget->enter_handler = handler;
1119}
1120
1121void
1122widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1123{
1124 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001125}
1126
1127void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001128widget_set_motion_handler(struct widget *widget,
1129 widget_motion_handler_t handler)
1130{
1131 widget->motion_handler = handler;
1132}
1133
1134void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001135widget_set_button_handler(struct widget *widget,
1136 widget_button_handler_t handler)
1137{
1138 widget->button_handler = handler;
1139}
1140
1141void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001142widget_schedule_redraw(struct widget *widget)
1143{
1144 window_schedule_redraw(widget->window);
1145}
1146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001147cairo_surface_t *
1148window_get_surface(struct window *window)
1149{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001150 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001151}
1152
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001153struct wl_surface *
1154window_get_wl_surface(struct window *window)
1155{
1156 return window->surface;
1157}
1158
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159struct wl_shell_surface *
1160window_get_wl_shell_surface(struct window *window)
1161{
1162 return window->shell_surface;
1163}
1164
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165static void
1166frame_resize_handler(struct widget *widget,
1167 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001168{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169 struct frame *frame = data;
1170 struct widget *child = frame->child;
1171 struct rectangle allocation;
1172 int decoration_width, decoration_height;
1173
1174 decoration_width = 20 + frame->margin * 2;
1175 decoration_height = 60 + frame->margin * 2;
1176
1177 allocation.x = 10 + frame->margin;
1178 allocation.y = 50 + frame->margin;
1179 allocation.width = width - decoration_width;
1180 allocation.height = height - decoration_height;
1181
1182 widget_set_allocation(child, allocation.x, allocation.y,
1183 allocation.width, allocation.height);
1184
1185 if (child->resize_handler)
1186 child->resize_handler(child,
1187 allocation.width,
1188 allocation.height,
1189 child->user_data);
1190
1191 widget_set_allocation(widget, 0, 0,
1192 child->allocation.width + decoration_width,
1193 child->allocation.height + decoration_height);
1194}
1195
1196static void
1197frame_redraw_handler(struct widget *widget, void *data)
1198{
1199 struct frame *frame = data;
1200 cairo_t *cr;
1201 cairo_text_extents_t extents;
1202 cairo_surface_t *source;
1203 int width, height, shadow_dx = 3, shadow_dy = 3;
1204 struct window *window = widget->window;
1205
1206 width = widget->allocation.width;
1207 height = widget->allocation.height;
1208
1209 cr = cairo_create(window->cairo_surface);
1210
1211 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1212 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1213 cairo_paint(cr);
1214
1215 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1216 tile_mask(cr, window->display->shadow,
1217 shadow_dx, shadow_dy, width, height,
1218 frame->margin + 10 - shadow_dx,
1219 frame->margin + 10 - shadow_dy);
1220
1221 if (window->keyboard_device)
1222 source = window->display->active_frame;
1223 else
1224 source = window->display->inactive_frame;
1225
1226 tile_source(cr, source, 0, 0, width, height,
1227 frame->margin + 10, frame->margin + 50);
1228
1229 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1230 cairo_set_font_size(cr, 14);
1231 cairo_text_extents(cr, window->title, &extents);
1232 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1233 if (window->keyboard_device)
1234 cairo_set_source_rgb(cr, 0, 0, 0);
1235 else
1236 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1237 cairo_show_text(cr, window->title);
1238
1239 cairo_destroy(cr);
1240}
1241
1242static int
1243frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1244{
1245 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001247 const int grip_size = 8;
1248
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001252 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001254 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001255 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001256 hlocation = WINDOW_RESIZING_RIGHT;
1257 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001258 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001262 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001263 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001267 vlocation = WINDOW_RESIZING_BOTTOM;
1268 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001271 location = vlocation | hlocation;
1272 if (location & WINDOW_EXTERIOR)
1273 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001274 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275 location = WINDOW_TITLEBAR;
1276 else if (location == WINDOW_INTERIOR)
1277 location = WINDOW_CLIENT_AREA;
1278
1279 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280}
1281
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001283frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001285 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001287 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001288 switch (location) {
1289 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001303 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001305 case WINDOW_EXTERIOR:
1306 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001308 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001309 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001310}
1311
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001312static void
1313frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001315 switch (index) {
1316 case 0: /* close */
1317 if (window->close_handler)
1318 window->close_handler(window->parent,
1319 window->user_data);
1320 else
1321 display_exit(window->display);
1322 break;
1323 case 1: /* fullscreen */
1324 /* we don't have a way to get out of fullscreen for now */
1325 window_set_fullscreen(window, 1);
1326 break;
1327 case 2: /* rotate */
1328 case 3: /* scale */
1329 break;
1330 }
1331}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001332
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001333void
1334window_show_frame_menu(struct window *window,
1335 struct input *input, uint32_t time)
1336{
1337 int32_t x, y;
1338
1339 static const char *entries[] = {
1340 "Close", "Fullscreen", "Rotate", "Scale"
1341 };
1342
1343 input_get_position(input, &x, &y);
1344 window_show_menu(window->display, input, time, window,
1345 x - 10, y - 10, frame_menu_func, entries, 4);
1346}
1347
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348static int
1349frame_enter_handler(struct widget *widget,
1350 struct input *input, uint32_t time,
1351 int32_t x, int32_t y, void *data)
1352{
1353 return frame_get_pointer_image_for_location(data, input);
1354}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001355
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001356static int
1357frame_motion_handler(struct widget *widget,
1358 struct input *input, uint32_t time,
1359 int32_t x, int32_t y, void *data)
1360{
1361 return frame_get_pointer_image_for_location(data, input);
1362}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001363
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001364static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001365frame_button_handler(struct widget *widget,
1366 struct input *input, uint32_t time,
1367 int button, int state, void *data)
1368
1369{
1370 struct frame *frame = data;
1371 struct window *window = widget->window;
1372 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001373 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);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001381 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001382 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;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001396 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001397 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) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001403 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001404 }
1405}
1406
1407struct widget *
1408frame_create(struct window *window, void *data)
1409{
1410 struct frame *frame;
1411
1412 frame = malloc(sizeof *frame);
1413 memset(frame, 0, sizeof *frame);
1414
1415 frame->widget = window_add_widget(window, frame);
1416 frame->child = widget_add_widget(frame->widget, data);
1417 frame->margin = 16;
1418
1419 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1420 widget_set_resize_handler(frame->widget, frame_resize_handler);
1421 widget_set_enter_handler(frame->widget, frame_enter_handler);
1422 widget_set_motion_handler(frame->widget, frame_motion_handler);
1423 widget_set_button_handler(frame->widget, frame_button_handler);
1424
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001425 window->frame = frame;
1426
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001427 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001428}
1429
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001430static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001431frame_destroy(struct frame *frame)
1432{
1433 /* frame->child must be destroyed by the application */
1434 widget_destroy(frame->widget);
1435 free(frame);
1436}
1437
1438static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001439input_set_focus_widget(struct input *input, struct widget *focus,
1440 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001442 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001443 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001444
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001445 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001446 return;
1447
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001448 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001449 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001450 widget = old;
1451 if (input->grab)
1452 widget = input->grab;
1453 if (widget->leave_handler)
1454 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001455 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001456 }
1457
1458 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001459 widget = focus;
1460 if (input->grab)
1461 widget = input->grab;
1462 if (widget->enter_handler)
1463 pointer = widget->enter_handler(focus, input, time,
1464 x, y,
1465 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001466 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001467
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001468 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001469 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001470}
1471
1472static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001473input_handle_motion(void *data, struct wl_input_device *input_device,
1474 uint32_t time,
1475 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001476{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001477 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001478 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001479 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001480 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001481
1482 input->x = x;
1483 input->y = y;
1484 input->sx = sx;
1485 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001486
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001487 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001488 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001489 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001490 }
1491
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001492 if (input->grab)
1493 widget = input->grab;
1494 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001495 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001496 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001497 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001498 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001499 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001500
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001501 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001502}
1503
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001504void
1505input_grab(struct input *input, struct widget *widget, uint32_t button)
1506{
1507 input->grab = widget;
1508 input->grab_button = button;
1509}
1510
1511void
1512input_ungrab(struct input *input, uint32_t time)
1513{
1514 struct widget *widget;
1515
1516 input->grab = NULL;
1517 if (input->pointer_focus) {
1518 widget = widget_find_widget(input->pointer_focus->widget,
1519 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001520 input_set_focus_widget(input, widget,
1521 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001522 }
1523}
1524
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001525static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001526input_handle_button(void *data,
1527 struct wl_input_device *input_device,
1528 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001529{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001531 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001532
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001533 if (input->focus_widget && input->grab == NULL && state)
1534 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001535
Neil Roberts6b28aad2012-01-23 19:11:18 +00001536 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001537 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001538 (*widget->button_handler)(widget,
1539 input, time,
1540 button, state,
1541 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001542
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001543 if (input->grab && input->grab_button == button && !state)
1544 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001545}
1546
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001547static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001548input_handle_key(void *data, struct wl_input_device *input_device,
1549 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001550{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 struct input *input = data;
1552 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001553 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001554 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001555
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001556 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001557 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001558 return;
1559
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001560 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001561 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001562 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1563 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001564
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001565 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1566
1567 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001568 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001569 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001570 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001571
1572 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001573 (*window->key_handler)(window, input, time, key, sym, state,
1574 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001575}
1576
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001577static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001578input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001579{
1580 struct window *window = input->pointer_focus;
1581
1582 if (!window)
1583 return;
1584
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001585 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001586
Pekka Paalanene1207c72011-12-16 12:02:09 +02001587 input->pointer_focus = NULL;
1588 input->current_pointer_image = POINTER_UNSET;
1589}
1590
1591static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001592input_handle_pointer_focus(void *data,
1593 struct wl_input_device *input_device,
1594 uint32_t time, struct wl_surface *surface,
1595 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001596{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001597 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001598 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001599 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001600
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001601 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001602 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001603 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001604
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001605 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001606 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001607 window = input->pointer_focus;
1608
Kristian Høgsberg59826582011-01-20 11:56:57 -05001609 input->x = x;
1610 input->y = y;
1611 input->sx = sx;
1612 input->sy = sy;
1613
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001615 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001616 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001617}
1618
1619static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001620input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001621{
1622 struct window *window = input->keyboard_focus;
1623
1624 if (!window)
1625 return;
1626
1627 window->keyboard_device = NULL;
1628 if (window->keyboard_focus_handler)
1629 (*window->keyboard_focus_handler)(window, NULL,
1630 window->user_data);
1631
1632 input->keyboard_focus = NULL;
1633}
1634
1635static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001636input_handle_keyboard_focus(void *data,
1637 struct wl_input_device *input_device,
1638 uint32_t time,
1639 struct wl_surface *surface,
1640 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001641{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001642 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001643 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001644 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001645 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001646
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001647 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001648
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001649 if (surface)
1650 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001651
1652 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001653 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001654 for (k = keys->data; k < end; k++)
1655 input->modifiers |= d->xkb->map->modmap[*k];
1656
1657 window = input->keyboard_focus;
1658 if (window) {
1659 window->keyboard_device = input;
1660 if (window->keyboard_focus_handler)
1661 (*window->keyboard_focus_handler)(window,
1662 window->keyboard_device,
1663 window->user_data);
1664 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001665}
1666
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001667static void
1668input_handle_touch_down(void *data,
1669 struct wl_input_device *wl_input_device,
1670 uint32_t time, struct wl_surface *surface,
1671 int32_t id, int32_t x, int32_t y)
1672{
1673}
1674
1675static void
1676input_handle_touch_up(void *data,
1677 struct wl_input_device *wl_input_device,
1678 uint32_t time, int32_t id)
1679{
1680}
1681
1682static void
1683input_handle_touch_motion(void *data,
1684 struct wl_input_device *wl_input_device,
1685 uint32_t time, int32_t id, int32_t x, int32_t y)
1686{
1687}
1688
1689static void
1690input_handle_touch_frame(void *data,
1691 struct wl_input_device *wl_input_device)
1692{
1693}
1694
1695static void
1696input_handle_touch_cancel(void *data,
1697 struct wl_input_device *wl_input_device)
1698{
1699}
1700
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001701static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001702 input_handle_motion,
1703 input_handle_button,
1704 input_handle_key,
1705 input_handle_pointer_focus,
1706 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001707 input_handle_touch_down,
1708 input_handle_touch_up,
1709 input_handle_touch_motion,
1710 input_handle_touch_frame,
1711 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001712};
1713
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001714void
1715input_get_position(struct input *input, int32_t *x, int32_t *y)
1716{
1717 *x = input->sx;
1718 *y = input->sy;
1719}
1720
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001721struct wl_input_device *
1722input_get_input_device(struct input *input)
1723{
1724 return input->input_device;
1725}
1726
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001727uint32_t
1728input_get_modifiers(struct input *input)
1729{
1730 return input->modifiers;
1731}
1732
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001733struct widget *
1734input_get_focus_widget(struct input *input)
1735{
1736 return input->focus_widget;
1737}
1738
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001739struct data_offer {
1740 struct wl_data_offer *offer;
1741 struct input *input;
1742 struct wl_array types;
1743 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001744
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001745 struct task io_task;
1746 int fd;
1747 data_func_t func;
1748 int32_t x, y;
1749 void *user_data;
1750};
1751
1752static void
1753data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1754{
1755 struct data_offer *offer = data;
1756 char **p;
1757
1758 p = wl_array_add(&offer->types, sizeof *p);
1759 *p = strdup(type);
1760}
1761
1762static const struct wl_data_offer_listener data_offer_listener = {
1763 data_offer_offer,
1764};
1765
1766static void
1767data_offer_destroy(struct data_offer *offer)
1768{
1769 char **p;
1770
1771 offer->refcount--;
1772 if (offer->refcount == 0) {
1773 wl_data_offer_destroy(offer->offer);
1774 for (p = offer->types.data; *p; p++)
1775 free(*p);
1776 wl_array_release(&offer->types);
1777 free(offer);
1778 }
1779}
1780
1781static void
1782data_device_data_offer(void *data,
1783 struct wl_data_device *data_device, uint32_t id)
1784{
1785 struct data_offer *offer;
1786
1787 offer = malloc(sizeof *offer);
1788
1789 wl_array_init(&offer->types);
1790 offer->refcount = 1;
1791 offer->input = data;
1792
1793 /* FIXME: Generate typesafe wrappers for this */
1794 offer->offer = (struct wl_data_offer *)
1795 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1796 id, &wl_data_offer_interface);
1797
1798 wl_data_offer_add_listener(offer->offer,
1799 &data_offer_listener, offer);
1800}
1801
1802static void
1803data_device_enter(void *data, struct wl_data_device *data_device,
1804 uint32_t time, struct wl_surface *surface,
1805 int32_t x, int32_t y, struct wl_data_offer *offer)
1806{
1807 struct input *input = data;
1808 struct window *window;
1809 char **p;
1810
1811 input->drag_offer = wl_data_offer_get_user_data(offer);
1812 window = wl_surface_get_user_data(surface);
1813 input->pointer_focus = window;
1814
1815 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1816 *p = NULL;
1817
1818 window = input->pointer_focus;
1819 if (window->data_handler)
1820 window->data_handler(window, input, time, x, y,
1821 input->drag_offer->types.data,
1822 window->user_data);
1823}
1824
1825static void
1826data_device_leave(void *data, struct wl_data_device *data_device)
1827{
1828 struct input *input = data;
1829
1830 data_offer_destroy(input->drag_offer);
1831 input->drag_offer = NULL;
1832}
1833
1834static void
1835data_device_motion(void *data, struct wl_data_device *data_device,
1836 uint32_t time, int32_t x, int32_t y)
1837{
1838 struct input *input = data;
1839 struct window *window = input->pointer_focus;
1840
1841 input->sx = x;
1842 input->sy = y;
1843
1844 if (window->data_handler)
1845 window->data_handler(window, input, time, x, y,
1846 input->drag_offer->types.data,
1847 window->user_data);
1848}
1849
1850static void
1851data_device_drop(void *data, struct wl_data_device *data_device)
1852{
1853 struct input *input = data;
1854 struct window *window = input->pointer_focus;
1855
1856 if (window->drop_handler)
1857 window->drop_handler(window, input,
1858 input->sx, input->sy, window->user_data);
1859}
1860
1861static void
1862data_device_selection(void *data,
1863 struct wl_data_device *wl_data_device,
1864 struct wl_data_offer *offer)
1865{
1866 struct input *input = data;
1867 char **p;
1868
1869 if (input->selection_offer)
1870 data_offer_destroy(input->selection_offer);
1871
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001872 if (offer) {
1873 input->selection_offer = wl_data_offer_get_user_data(offer);
1874 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1875 *p = NULL;
1876 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001877}
1878
1879static const struct wl_data_device_listener data_device_listener = {
1880 data_device_data_offer,
1881 data_device_enter,
1882 data_device_leave,
1883 data_device_motion,
1884 data_device_drop,
1885 data_device_selection
1886};
1887
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001888void
1889input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1890{
1891 struct display *display = input->display;
1892 struct wl_buffer *buffer;
1893 cairo_surface_t *surface;
1894
1895 if (pointer == input->current_pointer_image)
1896 return;
1897
1898 input->current_pointer_image = pointer;
1899 surface = display->pointer_surfaces[pointer];
1900
1901 if (!surface)
1902 return;
1903
1904 buffer = display_get_buffer_for_surface(display, surface);
1905 wl_input_device_attach(input->input_device, time, buffer,
1906 pointer_images[pointer].hotspot_x,
1907 pointer_images[pointer].hotspot_y);
1908}
1909
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001910struct wl_data_device *
1911input_get_data_device(struct input *input)
1912{
1913 return input->data_device;
1914}
1915
1916void
1917input_set_selection(struct input *input,
1918 struct wl_data_source *source, uint32_t time)
1919{
1920 wl_data_device_set_selection(input->data_device, source, time);
1921}
1922
1923void
1924input_accept(struct input *input, uint32_t time, const char *type)
1925{
1926 wl_data_offer_accept(input->drag_offer->offer, time, type);
1927}
1928
1929static void
1930offer_io_func(struct task *task, uint32_t events)
1931{
1932 struct data_offer *offer =
1933 container_of(task, struct data_offer, io_task);
1934 unsigned int len;
1935 char buffer[4096];
1936
1937 len = read(offer->fd, buffer, sizeof buffer);
1938 offer->func(buffer, len,
1939 offer->x, offer->y, offer->user_data);
1940
1941 if (len == 0) {
1942 close(offer->fd);
1943 data_offer_destroy(offer);
1944 }
1945}
1946
1947static void
1948data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1949 data_func_t func, void *user_data)
1950{
1951 int p[2];
1952
1953 pipe2(p, O_CLOEXEC);
1954 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1955 close(p[1]);
1956
1957 offer->io_task.run = offer_io_func;
1958 offer->fd = p[0];
1959 offer->func = func;
1960 offer->refcount++;
1961 offer->user_data = user_data;
1962
1963 display_watch_fd(offer->input->display,
1964 offer->fd, EPOLLIN, &offer->io_task);
1965}
1966
1967void
1968input_receive_drag_data(struct input *input, const char *mime_type,
1969 data_func_t func, void *data)
1970{
1971 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1972 input->drag_offer->x = input->sx;
1973 input->drag_offer->y = input->sy;
1974}
1975
1976int
1977input_receive_selection_data(struct input *input, const char *mime_type,
1978 data_func_t func, void *data)
1979{
1980 char **p;
1981
1982 if (input->selection_offer == NULL)
1983 return -1;
1984
1985 for (p = input->selection_offer->types.data; *p; p++)
1986 if (strcmp(mime_type, *p) == 0)
1987 break;
1988
1989 if (*p == NULL)
1990 return -1;
1991
1992 data_offer_receive_data(input->selection_offer,
1993 mime_type, func, data);
1994 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001995}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001996
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001997int
1998input_receive_selection_data_to_fd(struct input *input,
1999 const char *mime_type, int fd)
2000{
2001 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2002
2003 return 0;
2004}
2005
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002006void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002007window_move(struct window *window, struct input *input, uint32_t time)
2008{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002009 if (!window->shell_surface)
2010 return;
2011
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002012 wl_shell_surface_move(window->shell_surface,
2013 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002014}
2015
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002016static void
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002017idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002018{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002019 struct window *window =
2020 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002021 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002022
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002023 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002024 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002025 widget_set_allocation(widget,
2026 window->pending_allocation.x,
2027 window->pending_allocation.y,
2028 window->pending_allocation.width,
2029 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002030
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002031 if (widget->resize_handler)
2032 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002033 widget->allocation.width,
2034 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002035 widget->user_data);
2036
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002037 if (window->allocation.width != widget->allocation.width ||
2038 window->allocation.height != widget->allocation.height) {
2039 window->allocation = widget->allocation;
2040 window_schedule_redraw(window);
2041 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002042}
2043
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002044void
2045window_schedule_resize(struct window *window, int width, int height)
2046{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002047 window->pending_allocation.x = 0;
2048 window->pending_allocation.y = 0;
2049 window->pending_allocation.width = width;
2050 window->pending_allocation.height = height;
2051
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002052 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002053 window->resize_task.run = idle_resize;
2054 display_defer(window->display, &window->resize_task);
2055 window->resize_scheduled = 1;
2056 }
2057}
2058
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002059void
2060widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2061{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002062 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002063}
2064
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002065static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002066handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002067 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002068 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002069{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002070 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002071
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002072 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002073 return;
2074
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002075 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002076 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002077}
2078
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002080menu_destroy(struct menu *menu)
2081{
2082 widget_destroy(menu->widget);
2083 window_destroy(menu->window);
2084 free(menu);
2085}
2086
2087static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002088handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2089{
2090 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002091 struct menu *menu = window->widget->user_data;
2092
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002093 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002094 * device. Or just use wl_callback. And this really needs to
2095 * be a window vfunc that the menu can set. And we need the
2096 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002097
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002098 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002099 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002100 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002101}
2102
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002103static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002104 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002105 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002106};
2107
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002108void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002109window_get_allocation(struct window *window,
2110 struct rectangle *allocation)
2111{
2112 *allocation = window->allocation;
2113}
2114
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002115static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002116widget_redraw(struct widget *widget)
2117{
2118 struct widget *child;
2119
2120 if (widget->redraw_handler)
2121 widget->redraw_handler(widget, widget->user_data);
2122 wl_list_for_each(child, &widget->child_list, link)
2123 widget_redraw(child);
2124}
2125
2126static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002127idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002128{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002129 struct window *window =
2130 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002131
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002132 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002133 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002134 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002135 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002136}
2137
2138void
2139window_schedule_redraw(struct window *window)
2140{
2141 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002142 window->redraw_task.run = idle_redraw;
2143 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002144 window->redraw_scheduled = 1;
2145 }
2146}
2147
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002148void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002149window_set_custom(struct window *window)
2150{
2151 window->type = TYPE_CUSTOM;
2152}
2153
2154void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002155window_set_fullscreen(struct window *window, int fullscreen)
2156{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002157 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002158 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002159
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002160 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002161 return;
2162
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002163 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002164 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002165 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002166 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002167 width = output->allocation.width;
2168 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002169 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002170 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171 width = window->saved_allocation.width;
2172 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002173 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002174
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002176}
2177
2178void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002179window_set_user_data(struct window *window, void *data)
2180{
2181 window->user_data = data;
2182}
2183
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002184void *
2185window_get_user_data(struct window *window)
2186{
2187 return window->user_data;
2188}
2189
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002190void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002191window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002192 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002193{
2194 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002195}
2196
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002197void
2198window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002199 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002200{
2201 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002202}
2203
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002204void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002205window_set_data_handler(struct window *window, window_data_handler_t handler)
2206{
2207 window->data_handler = handler;
2208}
2209
2210void
2211window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2212{
2213 window->drop_handler = handler;
2214}
2215
2216void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002217window_set_close_handler(struct window *window,
2218 window_close_handler_t handler)
2219{
2220 window->close_handler = handler;
2221}
2222
2223void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002224window_set_transparent(struct window *window, int transparent)
2225{
2226 window->transparent = transparent;
2227}
2228
2229void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002230window_set_title(struct window *window, const char *title)
2231{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002232 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002233 window->title = strdup(title);
2234}
2235
2236const char *
2237window_get_title(struct window *window)
2238{
2239 return window->title;
2240}
2241
2242void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002243display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2244 int32_t x, int32_t y, int32_t width, int32_t height)
2245{
2246 struct wl_buffer *buffer;
2247
2248 buffer = display_get_buffer_for_surface(display, cairo_surface);
2249
2250 wl_buffer_damage(buffer, x, y, width, height);
2251}
2252
2253void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002254window_damage(struct window *window, int32_t x, int32_t y,
2255 int32_t width, int32_t height)
2256{
2257 wl_surface_damage(window->surface, x, y, width, height);
2258}
2259
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002260static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002261window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002262{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002263 struct window *window;
2264
2265 window = malloc(sizeof *window);
2266 if (window == NULL)
2267 return NULL;
2268
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002269 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002270 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002271 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002272 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002273 if (display->shell) {
2274 window->shell_surface =
2275 wl_shell_get_shell_surface(display->shell,
2276 window->surface);
2277 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002278 window->allocation.x = 0;
2279 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002280 window->allocation.width = 0;
2281 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002282 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002283 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002284 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002285
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002286 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002287#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002288 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2289 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002290#else
2291 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2292#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002293 else
2294 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002295
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002296 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002297 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002298
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002299 if (window->shell_surface) {
2300 wl_shell_surface_set_user_data(window->shell_surface, window);
2301 wl_shell_surface_add_listener(window->shell_surface,
2302 &shell_surface_listener, window);
2303 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002304
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002305 return window;
2306}
2307
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002308struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002309window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002310{
2311 struct window *window;
2312
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002313 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002314 if (!window)
2315 return NULL;
2316
2317 return window;
2318}
2319
2320struct window *
2321window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002322 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002323{
2324 struct window *window;
2325
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002326 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002327 if (!window)
2328 return NULL;
2329
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002330 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002331 window->x = x;
2332 window->y = y;
2333
2334 return window;
2335}
2336
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002337static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002338menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002339{
2340 int next;
2341
2342 next = (sy - 8) / 20;
2343 if (menu->current != next) {
2344 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002345 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002347}
2348
2349static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002350menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002351 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002352 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002353{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002354 struct menu *menu = data;
2355
2356 if (widget == menu->widget)
2357 menu_set_item(data, y);
2358
2359 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002360}
2361
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002362static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002363menu_enter_handler(struct widget *widget,
2364 struct input *input, uint32_t time,
2365 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002366{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002367 struct menu *menu = data;
2368
2369 if (widget == menu->widget)
2370 menu_set_item(data, y);
2371
2372 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002373}
2374
2375static void
2376menu_leave_handler(struct widget *widget, struct input *input, void *data)
2377{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002378 struct menu *menu = data;
2379
2380 if (widget == menu->widget)
2381 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002382}
2383
2384static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002385menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002386 struct input *input, uint32_t time,
2387 int button, int state, void *data)
2388
2389{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002390 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002391
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002392 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002393 /* Either relase after press-drag-release or
2394 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002395 menu->func(menu->window->parent,
2396 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002397 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002398 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002399 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002400}
2401
2402static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002403menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002404{
2405 cairo_t *cr;
2406 const int32_t r = 3, margin = 3;
2407 struct menu *menu = data;
2408 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002409 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002410
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002411 cr = cairo_create(window->cairo_surface);
2412 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2413 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2414 cairo_paint(cr);
2415
2416 width = window->allocation.width;
2417 height = window->allocation.height;
2418 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002419
2420 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002421 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2422 cairo_fill(cr);
2423
2424 for (i = 0; i < menu->count; i++) {
2425 if (i == menu->current) {
2426 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2427 cairo_rectangle(cr, margin, i * 20 + margin,
2428 width - 2 * margin, 20);
2429 cairo_fill(cr);
2430 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2431 cairo_move_to(cr, 10, i * 20 + 16);
2432 cairo_show_text(cr, menu->entries[i]);
2433 } else {
2434 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2435 cairo_move_to(cr, 10, i * 20 + 16);
2436 cairo_show_text(cr, menu->entries[i]);
2437 }
2438 }
2439
2440 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002441}
2442
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002443void
2444window_show_menu(struct display *display,
2445 struct input *input, uint32_t time, struct window *parent,
2446 int32_t x, int32_t y,
2447 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002448{
2449 struct window *window;
2450 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002451 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002452
2453 menu = malloc(sizeof *menu);
2454 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002455 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002456
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002457 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002458 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002459 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002460
2461 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002462 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463 menu->entries = entries;
2464 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002465 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002466 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002467 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002468 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002469 window->type = TYPE_MENU;
2470 window->x = x;
2471 window->y = y;
2472
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002473 wl_shell_surface_set_popup(window->shell_surface,
2474 input->input_device, time,
2475 window->parent->shell_surface,
2476 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002477
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002478 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002479 widget_set_enter_handler(menu->widget, menu_enter_handler);
2480 widget_set_leave_handler(menu->widget, menu_leave_handler);
2481 widget_set_motion_handler(menu->widget, menu_motion_handler);
2482 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002483
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002484 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002485 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002486}
2487
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002488void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002489window_set_buffer_type(struct window *window, enum window_buffer_type type)
2490{
2491 window->buffer_type = type;
2492}
2493
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002494
2495static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002496display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002497 struct wl_output *wl_output,
2498 int x, int y,
2499 int physical_width,
2500 int physical_height,
2501 int subpixel,
2502 const char *make,
2503 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002504{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002505 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002506
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002507 output->allocation.x = x;
2508 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002509}
2510
2511static void
2512display_handle_mode(void *data,
2513 struct wl_output *wl_output,
2514 uint32_t flags,
2515 int width,
2516 int height,
2517 int refresh)
2518{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002519 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002520 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002521
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002522 if (flags & WL_OUTPUT_MODE_CURRENT) {
2523 output->allocation.width = width;
2524 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002525 if (display->output_configure_handler)
2526 (*display->output_configure_handler)(
2527 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002528 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002529}
2530
2531static const struct wl_output_listener output_listener = {
2532 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002533 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002534};
2535
2536static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002537display_add_output(struct display *d, uint32_t id)
2538{
2539 struct output *output;
2540
2541 output = malloc(sizeof *output);
2542 if (output == NULL)
2543 return;
2544
2545 memset(output, 0, sizeof *output);
2546 output->display = d;
2547 output->output =
2548 wl_display_bind(d->display, id, &wl_output_interface);
2549 wl_list_insert(d->output_list.prev, &output->link);
2550
2551 wl_output_add_listener(output->output, &output_listener, output);
2552}
2553
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002554static void
2555output_destroy(struct output *output)
2556{
2557 if (output->destroy_handler)
2558 (*output->destroy_handler)(output, output->user_data);
2559
2560 wl_output_destroy(output->output);
2561 wl_list_remove(&output->link);
2562 free(output);
2563}
2564
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002565void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002566display_set_output_configure_handler(struct display *display,
2567 display_output_handler_t handler)
2568{
2569 struct output *output;
2570
2571 display->output_configure_handler = handler;
2572 if (!handler)
2573 return;
2574
2575 wl_list_for_each(output, &display->output_list, link)
2576 (*display->output_configure_handler)(output,
2577 display->user_data);
2578}
2579
2580void
2581output_set_user_data(struct output *output, void *data)
2582{
2583 output->user_data = data;
2584}
2585
2586void *
2587output_get_user_data(struct output *output)
2588{
2589 return output->user_data;
2590}
2591
2592void
2593output_set_destroy_handler(struct output *output,
2594 display_output_handler_t handler)
2595{
2596 output->destroy_handler = handler;
2597 /* FIXME: implement this, once we have way to remove outputs */
2598}
2599
2600void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002601output_get_allocation(struct output *output, struct rectangle *allocation)
2602{
2603 *allocation = output->allocation;
2604}
2605
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002606struct wl_output *
2607output_get_wl_output(struct output *output)
2608{
2609 return output->output;
2610}
2611
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002612static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002613display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002614{
2615 struct input *input;
2616
2617 input = malloc(sizeof *input);
2618 if (input == NULL)
2619 return;
2620
2621 memset(input, 0, sizeof *input);
2622 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002623 input->input_device =
2624 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002625 input->pointer_focus = NULL;
2626 input->keyboard_focus = NULL;
2627 wl_list_insert(d->input_list.prev, &input->link);
2628
2629 wl_input_device_add_listener(input->input_device,
2630 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002631 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002632
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002633 input->data_device =
2634 wl_data_device_manager_get_data_device(d->data_device_manager,
2635 input->input_device);
2636 wl_data_device_add_listener(input->data_device,
2637 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002638}
2639
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002640static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002641input_destroy(struct input *input)
2642{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002643 input_remove_keyboard_focus(input);
2644 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002645
2646 if (input->drag_offer)
2647 data_offer_destroy(input->drag_offer);
2648
2649 if (input->selection_offer)
2650 data_offer_destroy(input->selection_offer);
2651
2652 wl_data_device_destroy(input->data_device);
2653 wl_list_remove(&input->link);
2654 wl_input_device_destroy(input->input_device);
2655 free(input);
2656}
2657
2658static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002659display_handle_global(struct wl_display *display, uint32_t id,
2660 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002661{
2662 struct display *d = data;
2663
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002664 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002665 d->compositor =
2666 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002667 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002668 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002669 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002670 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002671 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002672 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002673 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002674 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002675 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2676 d->data_device_manager =
2677 wl_display_bind(display, id,
2678 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002679 }
2680}
2681
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002682static void
2683display_render_frame(struct display *d)
2684{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002685 int radius = 8;
2686 cairo_t *cr;
2687
2688 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2689 cr = cairo_create(d->shadow);
2690 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2691 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2692 rounded_rect(cr, 16, 16, 112, 112, radius);
2693 cairo_fill(cr);
2694 cairo_destroy(cr);
2695 blur_surface(d->shadow, 64);
2696
2697 d->active_frame =
2698 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2699 cr = cairo_create(d->active_frame);
2700 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2701 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2702 rounded_rect(cr, 16, 16, 112, 112, radius);
2703 cairo_fill(cr);
2704 cairo_destroy(cr);
2705
2706 d->inactive_frame =
2707 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2708 cr = cairo_create(d->inactive_frame);
2709 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2710 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2711 rounded_rect(cr, 16, 16, 112, 112, radius);
2712 cairo_fill(cr);
2713 cairo_destroy(cr);
2714}
2715
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002716static void
2717init_xkb(struct display *d)
2718{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002719 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002720
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002721 names.rules = "evdev";
2722 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002723 names.layout = option_xkb_layout;
2724 names.variant = option_xkb_variant;
2725 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002726
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002727 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002728 if (!d->xkb) {
2729 fprintf(stderr, "Failed to compile keymap\n");
2730 exit(1);
2731 }
2732}
2733
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002734static void
2735fini_xkb(struct display *display)
2736{
2737 xkb_free_keymap(display->xkb);
2738}
2739
Yuval Fledel45568f62010-12-06 09:18:12 -05002740static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002741init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002742{
2743 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002744 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002745
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002746 static const EGLint argb_cfg_attribs[] = {
2747 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002748 EGL_RED_SIZE, 1,
2749 EGL_GREEN_SIZE, 1,
2750 EGL_BLUE_SIZE, 1,
2751 EGL_ALPHA_SIZE, 1,
2752 EGL_DEPTH_SIZE, 1,
2753 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2754 EGL_NONE
2755 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002756
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002757 static const EGLint rgb_cfg_attribs[] = {
2758 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2759 EGL_RED_SIZE, 1,
2760 EGL_GREEN_SIZE, 1,
2761 EGL_BLUE_SIZE, 1,
2762 EGL_ALPHA_SIZE, 0,
2763 EGL_DEPTH_SIZE, 1,
2764 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2765 EGL_NONE
2766 };
2767
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002768#ifdef USE_CAIRO_GLESV2
2769 static const EGLint context_attribs[] = {
2770 EGL_CONTEXT_CLIENT_VERSION, 2,
2771 EGL_NONE
2772 };
2773 EGLint api = EGL_OPENGL_ES_API;
2774#else
2775 EGLint *context_attribs = NULL;
2776 EGLint api = EGL_OPENGL_API;
2777#endif
2778
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002779 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002780 if (!eglInitialize(d->dpy, &major, &minor)) {
2781 fprintf(stderr, "failed to initialize display\n");
2782 return -1;
2783 }
2784
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002785 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002786 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2787 return -1;
2788 }
2789
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002790 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2791 &d->argb_config, 1, &n) || n != 1) {
2792 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002793 return -1;
2794 }
2795
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002796 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2797 &d->rgb_config, 1, &n) || n != 1) {
2798 fprintf(stderr, "failed to choose rgb config\n");
2799 return -1;
2800 }
2801
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002802 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2803 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002804 if (d->rgb_ctx == NULL) {
2805 fprintf(stderr, "failed to create context\n");
2806 return -1;
2807 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002808 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002809 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002810 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002811 fprintf(stderr, "failed to create context\n");
2812 return -1;
2813 }
2814
Benjamin Franzke0c991632011-09-27 21:57:31 +02002815 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002816 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002817 return -1;
2818 }
2819
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002820#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002821 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2822 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002823 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002824 return -1;
2825 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002826 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2827 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2828 fprintf(stderr, "failed to get cairo egl argb device\n");
2829 return -1;
2830 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002831#endif
2832
2833 return 0;
2834}
2835
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002836static void
2837fini_egl(struct display *display)
2838{
2839#ifdef HAVE_CAIRO_EGL
2840 cairo_device_destroy(display->argb_device);
2841 cairo_device_destroy(display->rgb_device);
2842#endif
2843
2844 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2845 EGL_NO_CONTEXT);
2846
2847 eglTerminate(display->dpy);
2848 eglReleaseThread();
2849}
2850
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002851static int
2852event_mask_update(uint32_t mask, void *data)
2853{
2854 struct display *d = data;
2855
2856 d->mask = mask;
2857
2858 return 0;
2859}
2860
2861static void
2862handle_display_data(struct task *task, uint32_t events)
2863{
2864 struct display *display =
2865 container_of(task, struct display, display_task);
2866
2867 wl_display_iterate(display->display, display->mask);
2868}
2869
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002870struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002871display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002872{
2873 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002874 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002875 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002876 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002877
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002878 g_type_init();
2879
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002880 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002881 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002882 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002883
2884 xkb_option_group = g_option_group_new("xkb",
2885 "Keyboard options",
2886 "Show all XKB options",
2887 NULL, NULL);
2888 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2889 g_option_context_add_group (context, xkb_option_group);
2890
2891 if (!g_option_context_parse(context, argc, argv, &error)) {
2892 fprintf(stderr, "option parsing failed: %s\n", error->message);
2893 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002894 }
2895
Tim Wiederhake748f6722011-01-23 23:25:25 +01002896 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002897
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002898 d = malloc(sizeof *d);
2899 if (d == NULL)
2900 return NULL;
2901
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002902 memset(d, 0, sizeof *d);
2903
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002904 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002905 if (d->display == NULL) {
2906 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002907 return NULL;
2908 }
2909
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002910 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2911 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2912 d->display_task.run = handle_display_data;
2913 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2914
2915 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002916 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002917 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002918
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002919 /* Set up listener so we'll catch all events. */
2920 wl_display_add_global_listener(d->display,
2921 display_handle_global, d);
2922
2923 /* Process connection events. */
2924 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002925 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002926 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002927
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002928 d->image_target_texture_2d =
2929 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2930 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2931 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2932
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002933 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002934
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002935 display_render_frame(d);
2936
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002937 wl_list_init(&d->window_list);
2938
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002939 init_xkb(d);
2940
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002941 return d;
2942}
2943
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002944static void
2945display_destroy_outputs(struct display *display)
2946{
2947 struct output *tmp;
2948 struct output *output;
2949
2950 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2951 output_destroy(output);
2952}
2953
Pekka Paalanene1207c72011-12-16 12:02:09 +02002954static void
2955display_destroy_inputs(struct display *display)
2956{
2957 struct input *tmp;
2958 struct input *input;
2959
2960 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2961 input_destroy(input);
2962}
2963
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002964void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002965display_destroy(struct display *display)
2966{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002967 if (!wl_list_empty(&display->window_list))
2968 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2969
2970 if (!wl_list_empty(&display->deferred_list))
2971 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2972
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002973 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002974 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002975
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002976 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002977
2978 cairo_surface_destroy(display->active_frame);
2979 cairo_surface_destroy(display->inactive_frame);
2980 cairo_surface_destroy(display->shadow);
2981 destroy_pointer_surfaces(display);
2982
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002983 fini_egl(display);
2984
Pekka Paalanenc2052982011-12-16 11:41:32 +02002985 if (display->shell)
2986 wl_shell_destroy(display->shell);
2987
2988 if (display->shm)
2989 wl_shm_destroy(display->shm);
2990
2991 if (display->data_device_manager)
2992 wl_data_device_manager_destroy(display->data_device_manager);
2993
2994 wl_compositor_destroy(display->compositor);
2995
2996 close(display->epoll_fd);
2997
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002998 wl_display_flush(display->display);
2999 wl_display_destroy(display->display);
3000 free(display);
3001}
3002
3003void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003004display_set_user_data(struct display *display, void *data)
3005{
3006 display->user_data = data;
3007}
3008
3009void *
3010display_get_user_data(struct display *display)
3011{
3012 return display->user_data;
3013}
3014
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003015struct wl_display *
3016display_get_display(struct display *display)
3017{
3018 return display->display;
3019}
3020
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003021struct output *
3022display_get_output(struct display *display)
3023{
3024 return container_of(display->output_list.next, struct output, link);
3025}
3026
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003027struct wl_compositor *
3028display_get_compositor(struct display *display)
3029{
3030 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003031}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003032
3033EGLDisplay
3034display_get_egl_display(struct display *d)
3035{
3036 return d->dpy;
3037}
3038
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003039struct wl_data_source *
3040display_create_data_source(struct display *display)
3041{
3042 return wl_data_device_manager_create_data_source(display->data_device_manager);
3043}
3044
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003045EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003046display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003048 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003049}
3050
Benjamin Franzke0c991632011-09-27 21:57:31 +02003051EGLConfig
3052display_get_argb_egl_config(struct display *d)
3053{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003054 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003055}
3056
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003057struct wl_shell *
3058display_get_shell(struct display *display)
3059{
3060 return display->shell;
3061}
3062
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003063int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003064display_acquire_window_surface(struct display *display,
3065 struct window *window,
3066 EGLContext ctx)
3067{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003068#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003069 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003070 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003071
3072 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003073 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003074 device = cairo_surface_get_device(window->cairo_surface);
3075 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003076 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003077
Benjamin Franzke0c991632011-09-27 21:57:31 +02003078 if (!ctx) {
3079 if (device == display->rgb_device)
3080 ctx = display->rgb_ctx;
3081 else if (device == display->argb_device)
3082 ctx = display->argb_ctx;
3083 else
3084 assert(0);
3085 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003086
3087 data = cairo_surface_get_user_data(window->cairo_surface,
3088 &surface_data_key);
3089
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003090 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003091 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003092 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3093 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003094
3095 return 0;
3096#else
3097 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003098#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003099}
3100
3101void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003102display_release_window_surface(struct display *display,
3103 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003104{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003105#ifdef HAVE_CAIRO_EGL
3106 cairo_device_t *device;
3107
3108 device = cairo_surface_get_device(window->cairo_surface);
3109 if (!device)
3110 return;
3111
3112 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003113 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003114 cairo_device_release(device);
3115#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003116}
3117
3118void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003119display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003120{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003121 wl_list_insert(&display->deferred_list, &task->link);
3122}
3123
3124void
3125display_watch_fd(struct display *display,
3126 int fd, uint32_t events, struct task *task)
3127{
3128 struct epoll_event ep;
3129
3130 ep.events = events;
3131 ep.data.ptr = task;
3132 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3133}
3134
3135void
3136display_run(struct display *display)
3137{
3138 struct task *task;
3139 struct epoll_event ep[16];
3140 int i, count;
3141
Pekka Paalanen826d7952011-12-15 10:14:07 +02003142 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003143 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003144 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003145
3146 while (!wl_list_empty(&display->deferred_list)) {
3147 task = container_of(display->deferred_list.next,
3148 struct task, link);
3149 wl_list_remove(&task->link);
3150 task->run(task, 0);
3151 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003152
3153 if (!display->running)
3154 break;
3155
3156 wl_display_flush(display->display);
3157
3158 count = epoll_wait(display->epoll_fd,
3159 ep, ARRAY_LENGTH(ep), -1);
3160 for (i = 0; i < count; i++) {
3161 task = ep[i].data.ptr;
3162 task->run(task, ep[i].events);
3163 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003164 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003165}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003166
3167void
3168display_exit(struct display *display)
3169{
3170 display->running = 0;
3171}