blob: deb7a9754b24b2df439303d0a5498381de1deb27 [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øgsberg248c1b62011-01-21 18:03:15 -0500114 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400115 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400116 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118 int resize_scheduled;
119 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400121 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400122 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400123 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500124
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500125 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500126
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500127 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500128 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400129 window_data_handler_t data_handler;
130 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500131 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400132
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200133 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500134 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500135 struct window *menu;
136
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:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200836 window_get_resize_dx_dy(window, &x, &y);
837
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 if (window->pending_surface != NULL)
839 return;
840
841 window->pending_surface = window->cairo_surface;
842 window->cairo_surface = NULL;
843
844 buffer =
845 display_get_buffer_for_surface(display,
846 window->pending_surface);
847
848 wl_surface_attach(window->surface, buffer, x, y);
849 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400850 cb = wl_display_sync(display->display);
851 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100852 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000853 default:
854 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100855 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500857 wl_surface_damage(window->surface, 0, 0,
858 window->allocation.width,
859 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500860}
861
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500862void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400863window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500864{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100865 if (window->cairo_surface) {
866 switch (window->buffer_type) {
867 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
868 case WINDOW_BUFFER_TYPE_SHM:
869 display_surface_damage(window->display,
870 window->cairo_surface,
871 0, 0,
872 window->allocation.width,
873 window->allocation.height);
874 break;
875 default:
876 break;
877 }
878 window_attach_surface(window);
879 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500880}
881
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882void
883window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500885 cairo_surface_reference(surface);
886
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400887 if (window->cairo_surface != NULL)
888 cairo_surface_destroy(window->cairo_surface);
889
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500890 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400891}
892
Benjamin Franzke22d54812011-07-16 19:50:32 +0000893#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894static void
895window_resize_cairo_window_surface(struct window *window)
896{
897 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200898 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899
900 data = cairo_surface_get_user_data(window->cairo_surface,
901 &surface_data_key);
902
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200903 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100904 wl_egl_window_resize(data->window,
905 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200906 window->allocation.height,
907 x,y);
908
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100909 cairo_gl_surface_set_size(window->cairo_surface,
910 window->allocation.width,
911 window->allocation.height);
912}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000913#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100914
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400915struct display *
916window_get_display(struct window *window)
917{
918 return window->display;
919}
920
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400921void
922window_create_surface(struct window *window)
923{
924 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400925 uint32_t flags = 0;
926
927 if (!window->transparent)
928 flags = SURFACE_OPAQUE;
929
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400930 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500931#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100932 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
933 if (window->cairo_surface) {
934 window_resize_cairo_window_surface(window);
935 return;
936 }
937 surface = display_create_surface(window->display,
938 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400939 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100940 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500941 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400942 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100943 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400944 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945 break;
946#endif
947 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400948 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400949 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400950 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800951 default:
952 surface = NULL;
953 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400954 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400955
956 window_set_surface(window, surface);
957 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400958}
959
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200960static void frame_destroy(struct frame *frame);
961
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400962void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500963window_destroy(struct window *window)
964{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200965 struct display *display = window->display;
966 struct input *input;
967
968 if (window->redraw_scheduled)
969 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500970 if (window->resize_scheduled)
971 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200972
973 wl_list_for_each(input, &display->input_list, link) {
974 if (input->pointer_focus == window)
975 input->pointer_focus = NULL;
Kristian Høgsberg62444802012-01-11 11:18:14 -0500976 if (input->focus_widget &&
977 input->focus_widget->window == window)
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500978 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200979 if (input->keyboard_focus == window)
980 input->keyboard_focus = NULL;
981 }
982
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200983 if (window->frame)
984 frame_destroy(window->frame);
985
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200986 if (window->shell_surface)
987 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500988 wl_surface_destroy(window->surface);
989 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200990
991 if (window->cairo_surface != NULL)
992 cairo_surface_destroy(window->cairo_surface);
993 if (window->pending_surface != NULL)
994 cairo_surface_destroy(window->pending_surface);
995
996 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500997 free(window);
998}
999
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001000static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001001widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001002{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001003 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001004
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001005 wl_list_for_each(child, &widget->child_list, link) {
1006 target = widget_find_widget(child, x, y);
1007 if (target)
1008 return target;
1009 }
1010
1011 if (widget->allocation.x <= x &&
1012 x < widget->allocation.x + widget->allocation.width &&
1013 widget->allocation.y <= y &&
1014 y < widget->allocation.y + widget->allocation.height) {
1015 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001016 }
1017
1018 return NULL;
1019}
1020
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001021static struct widget *
1022widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001023{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001025
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001026 widget = malloc(sizeof *widget);
1027 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001028 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001029 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001030 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001031 wl_list_init(&widget->child_list);
1032
1033 return widget;
1034}
1035
1036struct widget *
1037window_add_widget(struct window *window, void *data)
1038{
1039 window->widget = widget_create(window, data);
1040 wl_list_init(&window->widget->link);
1041
1042 return window->widget;
1043}
1044
1045struct widget *
1046widget_add_widget(struct widget *parent, void *data)
1047{
1048 struct widget *widget;
1049
1050 widget = widget_create(parent->window, data);
1051 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001052
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001053 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001054}
1055
1056void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001057widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001058{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001059 wl_list_remove(&widget->link);
1060 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061}
1062
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001063void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001064widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001065{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001066 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001067}
1068
1069void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001070widget_set_size(struct widget *widget, int32_t width, int32_t height)
1071{
1072 struct window *window = widget->window;
1073
1074 widget->allocation.width = width;
1075 widget->allocation.height = height;
1076
1077 window->allocation.x = 0;
1078 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001079 window->allocation.width = width;
1080 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001081}
1082
1083void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001084widget_set_allocation(struct widget *widget,
1085 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001086{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001087 widget->allocation.x = x;
1088 widget->allocation.y = y;
1089 widget->allocation.width = width;
1090 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091}
1092
1093void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001094widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001095{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001096 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001097}
1098
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001099void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001100widget_set_resize_handler(struct widget *widget,
1101 widget_resize_handler_t handler)
1102{
1103 widget->resize_handler = handler;
1104}
1105
1106void
1107widget_set_redraw_handler(struct widget *widget,
1108 widget_redraw_handler_t handler)
1109{
1110 widget->redraw_handler = handler;
1111}
1112
1113void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001114widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001115{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001116 widget->enter_handler = handler;
1117}
1118
1119void
1120widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1121{
1122 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001123}
1124
1125void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001126widget_set_motion_handler(struct widget *widget,
1127 widget_motion_handler_t handler)
1128{
1129 widget->motion_handler = handler;
1130}
1131
1132void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001133widget_set_button_handler(struct widget *widget,
1134 widget_button_handler_t handler)
1135{
1136 widget->button_handler = handler;
1137}
1138
1139void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001140widget_schedule_redraw(struct widget *widget)
1141{
1142 window_schedule_redraw(widget->window);
1143}
1144
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001145cairo_surface_t *
1146window_get_surface(struct window *window)
1147{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001148 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001149}
1150
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001151struct wl_surface *
1152window_get_wl_surface(struct window *window)
1153{
1154 return window->surface;
1155}
1156
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001157struct wl_shell_surface *
1158window_get_wl_shell_surface(struct window *window)
1159{
1160 return window->shell_surface;
1161}
1162
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001163static void
1164frame_resize_handler(struct widget *widget,
1165 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001166{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001167 struct frame *frame = data;
1168 struct widget *child = frame->child;
1169 struct rectangle allocation;
1170 int decoration_width, decoration_height;
1171
1172 decoration_width = 20 + frame->margin * 2;
1173 decoration_height = 60 + frame->margin * 2;
1174
1175 allocation.x = 10 + frame->margin;
1176 allocation.y = 50 + frame->margin;
1177 allocation.width = width - decoration_width;
1178 allocation.height = height - decoration_height;
1179
1180 widget_set_allocation(child, allocation.x, allocation.y,
1181 allocation.width, allocation.height);
1182
1183 if (child->resize_handler)
1184 child->resize_handler(child,
1185 allocation.width,
1186 allocation.height,
1187 child->user_data);
1188
1189 widget_set_allocation(widget, 0, 0,
1190 child->allocation.width + decoration_width,
1191 child->allocation.height + decoration_height);
1192}
1193
1194static void
1195frame_redraw_handler(struct widget *widget, void *data)
1196{
1197 struct frame *frame = data;
1198 cairo_t *cr;
1199 cairo_text_extents_t extents;
1200 cairo_surface_t *source;
1201 int width, height, shadow_dx = 3, shadow_dy = 3;
1202 struct window *window = widget->window;
1203
1204 width = widget->allocation.width;
1205 height = widget->allocation.height;
1206
1207 cr = cairo_create(window->cairo_surface);
1208
1209 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1210 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1211 cairo_paint(cr);
1212
1213 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1214 tile_mask(cr, window->display->shadow,
1215 shadow_dx, shadow_dy, width, height,
1216 frame->margin + 10 - shadow_dx,
1217 frame->margin + 10 - shadow_dy);
1218
1219 if (window->keyboard_device)
1220 source = window->display->active_frame;
1221 else
1222 source = window->display->inactive_frame;
1223
1224 tile_source(cr, source, 0, 0, width, height,
1225 frame->margin + 10, frame->margin + 50);
1226
1227 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1228 cairo_set_font_size(cr, 14);
1229 cairo_text_extents(cr, window->title, &extents);
1230 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1231 if (window->keyboard_device)
1232 cairo_set_source_rgb(cr, 0, 0, 0);
1233 else
1234 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1235 cairo_show_text(cr, window->title);
1236
1237 cairo_destroy(cr);
1238}
1239
1240static int
1241frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1242{
1243 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001244 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001245 const int grip_size = 8;
1246
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001248 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001249 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001250 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001251 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001252 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001253 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001254 hlocation = WINDOW_RESIZING_RIGHT;
1255 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001256 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001260 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001261 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001262 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001265 vlocation = WINDOW_RESIZING_BOTTOM;
1266 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001267 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001268
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001269 location = vlocation | hlocation;
1270 if (location & WINDOW_EXTERIOR)
1271 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001272 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001273 location = WINDOW_TITLEBAR;
1274 else if (location == WINDOW_INTERIOR)
1275 location = WINDOW_CLIENT_AREA;
1276
1277 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278}
1279
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001280static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001281frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001282{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001283 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001284
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001285 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001286 switch (location) {
1287 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001288 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001298 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001303 case WINDOW_EXTERIOR:
1304 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001305 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001306 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001307 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001308}
1309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001310static void
1311frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001312{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001313 switch (index) {
1314 case 0: /* close */
1315 if (window->close_handler)
1316 window->close_handler(window->parent,
1317 window->user_data);
1318 else
1319 display_exit(window->display);
1320 break;
1321 case 1: /* fullscreen */
1322 /* we don't have a way to get out of fullscreen for now */
1323 window_set_fullscreen(window, 1);
1324 break;
1325 case 2: /* rotate */
1326 case 3: /* scale */
1327 break;
1328 }
1329}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001330
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001331static int
1332frame_enter_handler(struct widget *widget,
1333 struct input *input, uint32_t time,
1334 int32_t x, int32_t y, void *data)
1335{
1336 return frame_get_pointer_image_for_location(data, input);
1337}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001338
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001339static int
1340frame_motion_handler(struct widget *widget,
1341 struct input *input, uint32_t time,
1342 int32_t x, int32_t y, void *data)
1343{
1344 return frame_get_pointer_image_for_location(data, input);
1345}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001346
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001347static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001348frame_button_handler(struct widget *widget,
1349 struct input *input, uint32_t time,
1350 int button, int state, void *data)
1351
1352{
1353 struct frame *frame = data;
1354 struct window *window = widget->window;
1355 int location;
1356 int32_t x, y;
1357 static const char *entries[] = {
1358 "Close", "Fullscreen", "Rotate", "Scale"
1359 };
1360
1361 location = frame_get_pointer_location(frame, input->sx, input->sy);
1362
1363 if (window->display->shell && button == BTN_LEFT && state == 1) {
1364 switch (location) {
1365 case WINDOW_TITLEBAR:
1366 if (!window->shell_surface)
1367 break;
1368 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001369 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001370 wl_shell_surface_move(window->shell_surface,
1371 input_get_input_device(input),
1372 time);
1373 break;
1374 case WINDOW_RESIZING_TOP:
1375 case WINDOW_RESIZING_BOTTOM:
1376 case WINDOW_RESIZING_LEFT:
1377 case WINDOW_RESIZING_RIGHT:
1378 case WINDOW_RESIZING_TOP_LEFT:
1379 case WINDOW_RESIZING_TOP_RIGHT:
1380 case WINDOW_RESIZING_BOTTOM_LEFT:
1381 case WINDOW_RESIZING_BOTTOM_RIGHT:
1382 if (!window->shell_surface)
1383 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001384 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001385 wl_shell_surface_resize(window->shell_surface,
1386 input_get_input_device(input),
1387 time, location);
1388 break;
1389 }
1390 } else if (button == BTN_RIGHT && state == 1) {
1391 input_get_position(input, &x, &y);
1392 window->menu = window_create_menu(window->display,
1393 input, time,
1394 window,
1395 x - 10, y - 10,
1396 frame_menu_func,
1397 entries, 4);
1398 window_schedule_redraw(window->menu);
1399 }
1400}
1401
1402struct widget *
1403frame_create(struct window *window, void *data)
1404{
1405 struct frame *frame;
1406
1407 frame = malloc(sizeof *frame);
1408 memset(frame, 0, sizeof *frame);
1409
1410 frame->widget = window_add_widget(window, frame);
1411 frame->child = widget_add_widget(frame->widget, data);
1412 frame->margin = 16;
1413
1414 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1415 widget_set_resize_handler(frame->widget, frame_resize_handler);
1416 widget_set_enter_handler(frame->widget, frame_enter_handler);
1417 widget_set_motion_handler(frame->widget, frame_motion_handler);
1418 widget_set_button_handler(frame->widget, frame_button_handler);
1419
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001420 window->frame = frame;
1421
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001422 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001423}
1424
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001425static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001426frame_destroy(struct frame *frame)
1427{
1428 /* frame->child must be destroyed by the application */
1429 widget_destroy(frame->widget);
1430 free(frame);
1431}
1432
1433static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001434input_set_focus_widget(struct input *input, struct widget *focus,
1435 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001436{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001437 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001438 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001439
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001440 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001441 return;
1442
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001443 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001444 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001445 widget = old;
1446 if (input->grab)
1447 widget = input->grab;
1448 if (widget->leave_handler)
1449 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001450 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001451 }
1452
1453 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001454 widget = focus;
1455 if (input->grab)
1456 widget = input->grab;
1457 if (widget->enter_handler)
1458 pointer = widget->enter_handler(focus, input, time,
1459 x, y,
1460 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001461 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001462
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001463 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001464 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001465}
1466
1467static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001468input_handle_motion(void *data, struct wl_input_device *input_device,
1469 uint32_t time,
1470 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001471{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001472 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001473 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001474 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001475 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001476
1477 input->x = x;
1478 input->y = y;
1479 input->sx = sx;
1480 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001481
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001482 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001483 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001484 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001485 }
1486
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001487 if (input->grab)
1488 widget = input->grab;
1489 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001490 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001491 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001492 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001493 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001494 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001495
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001496 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001497}
1498
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001499void
1500input_grab(struct input *input, struct widget *widget, uint32_t button)
1501{
1502 input->grab = widget;
1503 input->grab_button = button;
1504}
1505
1506void
1507input_ungrab(struct input *input, uint32_t time)
1508{
1509 struct widget *widget;
1510
1511 input->grab = NULL;
1512 if (input->pointer_focus) {
1513 widget = widget_find_widget(input->pointer_focus->widget,
1514 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001515 input_set_focus_widget(input, widget,
1516 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001517 }
1518}
1519
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001520static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001521input_handle_button(void *data,
1522 struct wl_input_device *input_device,
1523 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001524{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001525 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001526 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001527
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001528 if (input->focus_widget && input->grab == NULL && state)
1529 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001530
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001531 widget = input->focus_widget;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001532 if (widget && widget->button_handler)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001533 (*input->grab->button_handler)(widget,
1534 input, time,
1535 button, state,
1536 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001537
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001538 if (input->grab && input->grab_button == button && !state)
1539 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001540}
1541
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001542static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001543input_handle_key(void *data, struct wl_input_device *input_device,
1544 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001545{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001546 struct input *input = data;
1547 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001548 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001549 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001550
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001551 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001552 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001553 return;
1554
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001555 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001556 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001557 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1558 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001559
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001560 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1561
1562 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001563 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001564 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001565 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001566
1567 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001568 (*window->key_handler)(window, input, time, key, sym, state,
1569 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001570}
1571
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001572static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001573input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001574{
1575 struct window *window = input->pointer_focus;
1576
1577 if (!window)
1578 return;
1579
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001580 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001581
Pekka Paalanene1207c72011-12-16 12:02:09 +02001582 input->pointer_focus = NULL;
1583 input->current_pointer_image = POINTER_UNSET;
1584}
1585
1586static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001587input_handle_pointer_focus(void *data,
1588 struct wl_input_device *input_device,
1589 uint32_t time, struct wl_surface *surface,
1590 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001591{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001592 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001593 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001594 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001595
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001596 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001597 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001598 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001599
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001600 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001601 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001602 window = input->pointer_focus;
1603
Kristian Høgsberg59826582011-01-20 11:56:57 -05001604 input->x = x;
1605 input->y = y;
1606 input->sx = sx;
1607 input->sy = sy;
1608
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001609 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001610 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001611 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001612}
1613
1614static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001615input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001616{
1617 struct window *window = input->keyboard_focus;
1618
1619 if (!window)
1620 return;
1621
1622 window->keyboard_device = NULL;
1623 if (window->keyboard_focus_handler)
1624 (*window->keyboard_focus_handler)(window, NULL,
1625 window->user_data);
1626
1627 input->keyboard_focus = NULL;
1628}
1629
1630static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001631input_handle_keyboard_focus(void *data,
1632 struct wl_input_device *input_device,
1633 uint32_t time,
1634 struct wl_surface *surface,
1635 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001636{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001637 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001638 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001639 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001640 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001641
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001642 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001643
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001644 if (surface)
1645 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001646
1647 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001648 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001649 for (k = keys->data; k < end; k++)
1650 input->modifiers |= d->xkb->map->modmap[*k];
1651
1652 window = input->keyboard_focus;
1653 if (window) {
1654 window->keyboard_device = input;
1655 if (window->keyboard_focus_handler)
1656 (*window->keyboard_focus_handler)(window,
1657 window->keyboard_device,
1658 window->user_data);
1659 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001660}
1661
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001662static void
1663input_handle_touch_down(void *data,
1664 struct wl_input_device *wl_input_device,
1665 uint32_t time, struct wl_surface *surface,
1666 int32_t id, int32_t x, int32_t y)
1667{
1668}
1669
1670static void
1671input_handle_touch_up(void *data,
1672 struct wl_input_device *wl_input_device,
1673 uint32_t time, int32_t id)
1674{
1675}
1676
1677static void
1678input_handle_touch_motion(void *data,
1679 struct wl_input_device *wl_input_device,
1680 uint32_t time, int32_t id, int32_t x, int32_t y)
1681{
1682}
1683
1684static void
1685input_handle_touch_frame(void *data,
1686 struct wl_input_device *wl_input_device)
1687{
1688}
1689
1690static void
1691input_handle_touch_cancel(void *data,
1692 struct wl_input_device *wl_input_device)
1693{
1694}
1695
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001696static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001697 input_handle_motion,
1698 input_handle_button,
1699 input_handle_key,
1700 input_handle_pointer_focus,
1701 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001702 input_handle_touch_down,
1703 input_handle_touch_up,
1704 input_handle_touch_motion,
1705 input_handle_touch_frame,
1706 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001707};
1708
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001709void
1710input_get_position(struct input *input, int32_t *x, int32_t *y)
1711{
1712 *x = input->sx;
1713 *y = input->sy;
1714}
1715
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001716struct wl_input_device *
1717input_get_input_device(struct input *input)
1718{
1719 return input->input_device;
1720}
1721
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001722uint32_t
1723input_get_modifiers(struct input *input)
1724{
1725 return input->modifiers;
1726}
1727
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001728struct widget *
1729input_get_focus_widget(struct input *input)
1730{
1731 return input->focus_widget;
1732}
1733
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001734struct data_offer {
1735 struct wl_data_offer *offer;
1736 struct input *input;
1737 struct wl_array types;
1738 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001739
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001740 struct task io_task;
1741 int fd;
1742 data_func_t func;
1743 int32_t x, y;
1744 void *user_data;
1745};
1746
1747static void
1748data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1749{
1750 struct data_offer *offer = data;
1751 char **p;
1752
1753 p = wl_array_add(&offer->types, sizeof *p);
1754 *p = strdup(type);
1755}
1756
1757static const struct wl_data_offer_listener data_offer_listener = {
1758 data_offer_offer,
1759};
1760
1761static void
1762data_offer_destroy(struct data_offer *offer)
1763{
1764 char **p;
1765
1766 offer->refcount--;
1767 if (offer->refcount == 0) {
1768 wl_data_offer_destroy(offer->offer);
1769 for (p = offer->types.data; *p; p++)
1770 free(*p);
1771 wl_array_release(&offer->types);
1772 free(offer);
1773 }
1774}
1775
1776static void
1777data_device_data_offer(void *data,
1778 struct wl_data_device *data_device, uint32_t id)
1779{
1780 struct data_offer *offer;
1781
1782 offer = malloc(sizeof *offer);
1783
1784 wl_array_init(&offer->types);
1785 offer->refcount = 1;
1786 offer->input = data;
1787
1788 /* FIXME: Generate typesafe wrappers for this */
1789 offer->offer = (struct wl_data_offer *)
1790 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1791 id, &wl_data_offer_interface);
1792
1793 wl_data_offer_add_listener(offer->offer,
1794 &data_offer_listener, offer);
1795}
1796
1797static void
1798data_device_enter(void *data, struct wl_data_device *data_device,
1799 uint32_t time, struct wl_surface *surface,
1800 int32_t x, int32_t y, struct wl_data_offer *offer)
1801{
1802 struct input *input = data;
1803 struct window *window;
1804 char **p;
1805
1806 input->drag_offer = wl_data_offer_get_user_data(offer);
1807 window = wl_surface_get_user_data(surface);
1808 input->pointer_focus = window;
1809
1810 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1811 *p = NULL;
1812
1813 window = input->pointer_focus;
1814 if (window->data_handler)
1815 window->data_handler(window, input, time, x, y,
1816 input->drag_offer->types.data,
1817 window->user_data);
1818}
1819
1820static void
1821data_device_leave(void *data, struct wl_data_device *data_device)
1822{
1823 struct input *input = data;
1824
1825 data_offer_destroy(input->drag_offer);
1826 input->drag_offer = NULL;
1827}
1828
1829static void
1830data_device_motion(void *data, struct wl_data_device *data_device,
1831 uint32_t time, int32_t x, int32_t y)
1832{
1833 struct input *input = data;
1834 struct window *window = input->pointer_focus;
1835
1836 input->sx = x;
1837 input->sy = y;
1838
1839 if (window->data_handler)
1840 window->data_handler(window, input, time, x, y,
1841 input->drag_offer->types.data,
1842 window->user_data);
1843}
1844
1845static void
1846data_device_drop(void *data, struct wl_data_device *data_device)
1847{
1848 struct input *input = data;
1849 struct window *window = input->pointer_focus;
1850
1851 if (window->drop_handler)
1852 window->drop_handler(window, input,
1853 input->sx, input->sy, window->user_data);
1854}
1855
1856static void
1857data_device_selection(void *data,
1858 struct wl_data_device *wl_data_device,
1859 struct wl_data_offer *offer)
1860{
1861 struct input *input = data;
1862 char **p;
1863
1864 if (input->selection_offer)
1865 data_offer_destroy(input->selection_offer);
1866
1867 input->selection_offer = wl_data_offer_get_user_data(offer);
1868 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1869 *p = NULL;
1870}
1871
1872static const struct wl_data_device_listener data_device_listener = {
1873 data_device_data_offer,
1874 data_device_enter,
1875 data_device_leave,
1876 data_device_motion,
1877 data_device_drop,
1878 data_device_selection
1879};
1880
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001881void
1882input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1883{
1884 struct display *display = input->display;
1885 struct wl_buffer *buffer;
1886 cairo_surface_t *surface;
1887
1888 if (pointer == input->current_pointer_image)
1889 return;
1890
1891 input->current_pointer_image = pointer;
1892 surface = display->pointer_surfaces[pointer];
1893
1894 if (!surface)
1895 return;
1896
1897 buffer = display_get_buffer_for_surface(display, surface);
1898 wl_input_device_attach(input->input_device, time, buffer,
1899 pointer_images[pointer].hotspot_x,
1900 pointer_images[pointer].hotspot_y);
1901}
1902
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001903struct wl_data_device *
1904input_get_data_device(struct input *input)
1905{
1906 return input->data_device;
1907}
1908
1909void
1910input_set_selection(struct input *input,
1911 struct wl_data_source *source, uint32_t time)
1912{
1913 wl_data_device_set_selection(input->data_device, source, time);
1914}
1915
1916void
1917input_accept(struct input *input, uint32_t time, const char *type)
1918{
1919 wl_data_offer_accept(input->drag_offer->offer, time, type);
1920}
1921
1922static void
1923offer_io_func(struct task *task, uint32_t events)
1924{
1925 struct data_offer *offer =
1926 container_of(task, struct data_offer, io_task);
1927 unsigned int len;
1928 char buffer[4096];
1929
1930 len = read(offer->fd, buffer, sizeof buffer);
1931 offer->func(buffer, len,
1932 offer->x, offer->y, offer->user_data);
1933
1934 if (len == 0) {
1935 close(offer->fd);
1936 data_offer_destroy(offer);
1937 }
1938}
1939
1940static void
1941data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1942 data_func_t func, void *user_data)
1943{
1944 int p[2];
1945
1946 pipe2(p, O_CLOEXEC);
1947 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1948 close(p[1]);
1949
1950 offer->io_task.run = offer_io_func;
1951 offer->fd = p[0];
1952 offer->func = func;
1953 offer->refcount++;
1954 offer->user_data = user_data;
1955
1956 display_watch_fd(offer->input->display,
1957 offer->fd, EPOLLIN, &offer->io_task);
1958}
1959
1960void
1961input_receive_drag_data(struct input *input, const char *mime_type,
1962 data_func_t func, void *data)
1963{
1964 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1965 input->drag_offer->x = input->sx;
1966 input->drag_offer->y = input->sy;
1967}
1968
1969int
1970input_receive_selection_data(struct input *input, const char *mime_type,
1971 data_func_t func, void *data)
1972{
1973 char **p;
1974
1975 if (input->selection_offer == NULL)
1976 return -1;
1977
1978 for (p = input->selection_offer->types.data; *p; p++)
1979 if (strcmp(mime_type, *p) == 0)
1980 break;
1981
1982 if (*p == NULL)
1983 return -1;
1984
1985 data_offer_receive_data(input->selection_offer,
1986 mime_type, func, data);
1987 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001988}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001989
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001990int
1991input_receive_selection_data_to_fd(struct input *input,
1992 const char *mime_type, int fd)
1993{
1994 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1995
1996 return 0;
1997}
1998
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001999void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002000window_move(struct window *window, struct input *input, uint32_t time)
2001{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002002 if (!window->shell_surface)
2003 return;
2004
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002005 wl_shell_surface_move(window->shell_surface,
2006 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002007}
2008
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002009static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002010window_resize(struct window *window, int32_t width, int32_t height)
2011{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002012 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002013 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002014
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002015 allocation.x = 0;
2016 allocation.y = 0;
2017 allocation.width = width;
2018 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002019
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002020 widget = window->widget;
2021 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002022 allocation.width, allocation.height);
2023
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002024 if (widget->resize_handler)
2025 widget->resize_handler(widget,
2026 allocation.width,
2027 allocation.height,
2028 widget->user_data);
2029
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002030 window->allocation = widget->allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002031
2032 window_schedule_redraw(window);
2033}
2034
2035static void
2036idle_resize(struct task *task, uint32_t events)
2037{
2038 struct window *window =
2039 container_of(task, struct window, resize_task);
2040
2041 window_resize(window,
2042 window->allocation.width, window->allocation.height);
2043 window->resize_scheduled = 0;
2044}
2045
2046void
2047window_schedule_resize(struct window *window, int width, int height)
2048{
2049 if (!window->resize_scheduled) {
2050 window->allocation.width = width;
2051 window->allocation.height = height;
2052 window->resize_task.run = idle_resize;
2053 display_defer(window->display, &window->resize_task);
2054 window->resize_scheduled = 1;
2055 }
2056}
2057
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002058void
2059widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2060{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002061 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002062}
2063
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002064static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002065handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002066 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002067 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002068{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002070
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002071 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002072 return;
2073
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002074 window->resize_edges = edges;
2075
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002076 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002077}
2078
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079static void
2080handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2081{
2082 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002083 struct menu *menu = window->widget->user_data;
2084
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002085 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002086 * device. Or just use wl_callback. And this really needs to
2087 * be a window vfunc that the menu can set. And we need the
2088 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002090 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002091 input_ungrab(menu->input, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002092 window_destroy(window);
2093}
2094
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002095static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002096 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002097 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002098};
2099
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002100void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002101window_get_allocation(struct window *window,
2102 struct rectangle *allocation)
2103{
2104 *allocation = window->allocation;
2105}
2106
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002107static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002108widget_redraw(struct widget *widget)
2109{
2110 struct widget *child;
2111
2112 if (widget->redraw_handler)
2113 widget->redraw_handler(widget, widget->user_data);
2114 wl_list_for_each(child, &widget->child_list, link)
2115 widget_redraw(child);
2116}
2117
2118static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002119idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002121 struct window *window =
2122 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002123
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002124 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002125 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002126 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002127 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002128}
2129
2130void
2131window_schedule_redraw(struct window *window)
2132{
2133 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002134 window->redraw_task.run = idle_redraw;
2135 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002136 window->redraw_scheduled = 1;
2137 }
2138}
2139
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002140void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002141window_set_custom(struct window *window)
2142{
2143 window->type = TYPE_CUSTOM;
2144}
2145
2146void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002147window_set_fullscreen(struct window *window, int fullscreen)
2148{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002149 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002150 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002151
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002152 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002153 return;
2154
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002155 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002156 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002157 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002158 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002159 width = output->allocation.width;
2160 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002161 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002162 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002163 width = window->saved_allocation.width;
2164 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002165 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002166
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002167 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002168}
2169
2170void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002171window_set_user_data(struct window *window, void *data)
2172{
2173 window->user_data = data;
2174}
2175
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002176void *
2177window_get_user_data(struct window *window)
2178{
2179 return window->user_data;
2180}
2181
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002182void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002183window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002184 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002185{
2186 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002187}
2188
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002189void
2190window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002191 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002192{
2193 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002194}
2195
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002196void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197window_set_data_handler(struct window *window, window_data_handler_t handler)
2198{
2199 window->data_handler = handler;
2200}
2201
2202void
2203window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2204{
2205 window->drop_handler = handler;
2206}
2207
2208void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002209window_set_close_handler(struct window *window,
2210 window_close_handler_t handler)
2211{
2212 window->close_handler = handler;
2213}
2214
2215void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002216window_set_transparent(struct window *window, int transparent)
2217{
2218 window->transparent = transparent;
2219}
2220
2221void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002222window_set_title(struct window *window, const char *title)
2223{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002224 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002225 window->title = strdup(title);
2226}
2227
2228const char *
2229window_get_title(struct window *window)
2230{
2231 return window->title;
2232}
2233
2234void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002235display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2236 int32_t x, int32_t y, int32_t width, int32_t height)
2237{
2238 struct wl_buffer *buffer;
2239
2240 buffer = display_get_buffer_for_surface(display, cairo_surface);
2241
2242 wl_buffer_damage(buffer, x, y, width, height);
2243}
2244
2245void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002246window_damage(struct window *window, int32_t x, int32_t y,
2247 int32_t width, int32_t height)
2248{
2249 wl_surface_damage(window->surface, x, y, width, height);
2250}
2251
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002252static struct window *
2253window_create_internal(struct display *display, struct window *parent,
2254 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002255{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002256 struct window *window;
2257
2258 window = malloc(sizeof *window);
2259 if (window == NULL)
2260 return NULL;
2261
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002262 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002263 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002264 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002265 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002266 if (display->shell) {
2267 window->shell_surface =
2268 wl_shell_get_shell_surface(display->shell,
2269 window->surface);
2270 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002271 window->allocation.x = 0;
2272 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002273 window->allocation.width = width;
2274 window->allocation.height = height;
2275 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002276 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002277
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002278 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002279#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002280 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2281 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002282#else
2283 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2284#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002285 else
2286 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002287
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002288 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002289 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002290
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002291 if (window->shell_surface) {
2292 wl_shell_surface_set_user_data(window->shell_surface, window);
2293 wl_shell_surface_add_listener(window->shell_surface,
2294 &shell_surface_listener, window);
2295 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002296
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002297 return window;
2298}
2299
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002300struct window *
2301window_create(struct display *display, int32_t width, int32_t height)
2302{
2303 struct window *window;
2304
2305 window = window_create_internal(display, NULL, width, height);
2306 if (!window)
2307 return NULL;
2308
2309 return window;
2310}
2311
2312struct window *
2313window_create_transient(struct display *display, struct window *parent,
2314 int32_t x, int32_t y, int32_t width, int32_t height)
2315{
2316 struct window *window;
2317
2318 window = window_create_internal(parent->display,
2319 parent, width, height);
2320 if (!window)
2321 return NULL;
2322
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002323 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002324 window->x = x;
2325 window->y = y;
2326
2327 return window;
2328}
2329
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002330static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002331menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002332{
2333 int next;
2334
2335 next = (sy - 8) / 20;
2336 if (menu->current != next) {
2337 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002338 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002339 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002340}
2341
2342static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002343menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002344 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002345 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002346{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002347 struct menu *menu = data;
2348
2349 if (widget == menu->widget)
2350 menu_set_item(data, y);
2351
2352 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002353}
2354
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002355static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002356menu_enter_handler(struct widget *widget,
2357 struct input *input, uint32_t time,
2358 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002359{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002360 struct menu *menu = data;
2361
2362 if (widget == menu->widget)
2363 menu_set_item(data, y);
2364
2365 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002366}
2367
2368static void
2369menu_leave_handler(struct widget *widget, struct input *input, void *data)
2370{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002371 struct menu *menu = data;
2372
2373 if (widget == menu->widget)
2374 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002375}
2376
2377static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002378menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002379 struct input *input, uint32_t time,
2380 int button, int state, void *data)
2381
2382{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002383 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002384
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002385 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002386 /* Either relase after press-drag-release or
2387 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002388 menu->func(menu->window->parent,
2389 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002390 input_ungrab(input, time);
2391 window_destroy(menu->widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002392 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002393}
2394
2395static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002396menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002397{
2398 cairo_t *cr;
2399 const int32_t r = 3, margin = 3;
2400 struct menu *menu = data;
2401 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002402 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002403
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002404 cr = cairo_create(window->cairo_surface);
2405 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2406 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2407 cairo_paint(cr);
2408
2409 width = window->allocation.width;
2410 height = window->allocation.height;
2411 rounded_rect(cr, 0, 0, width, height, r);
2412 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2413 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2414 cairo_fill(cr);
2415
2416 for (i = 0; i < menu->count; i++) {
2417 if (i == menu->current) {
2418 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2419 cairo_rectangle(cr, margin, i * 20 + margin,
2420 width - 2 * margin, 20);
2421 cairo_fill(cr);
2422 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2423 cairo_move_to(cr, 10, i * 20 + 16);
2424 cairo_show_text(cr, menu->entries[i]);
2425 } else {
2426 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2427 cairo_move_to(cr, 10, i * 20 + 16);
2428 cairo_show_text(cr, menu->entries[i]);
2429 }
2430 }
2431
2432 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433}
2434
2435struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002436window_create_menu(struct display *display,
2437 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002438 int32_t x, int32_t y,
2439 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002440{
2441 struct window *window;
2442 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002443 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002444
2445 menu = malloc(sizeof *menu);
2446 if (!menu)
2447 return NULL;
2448
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002449 window = window_create_internal(parent->display, parent,
2450 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002451 if (!window)
2452 return NULL;
2453
2454 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002455 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002456 menu->entries = entries;
2457 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002458 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002459 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002460 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002461 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002462 window->type = TYPE_MENU;
2463 window->x = x;
2464 window->y = y;
2465
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002466 wl_shell_surface_set_popup(window->shell_surface,
2467 input->input_device, time,
2468 window->parent->shell_surface,
2469 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002470
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002471 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002472 widget_set_enter_handler(menu->widget, menu_enter_handler);
2473 widget_set_leave_handler(menu->widget, menu_leave_handler);
2474 widget_set_motion_handler(menu->widget, menu_motion_handler);
2475 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002476
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002477 input_grab(input, menu->widget, 0);
2478
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002479 return window;
2480}
2481
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002482void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002483window_set_buffer_type(struct window *window, enum window_buffer_type type)
2484{
2485 window->buffer_type = type;
2486}
2487
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002488
2489static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002490display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002491 struct wl_output *wl_output,
2492 int x, int y,
2493 int physical_width,
2494 int physical_height,
2495 int subpixel,
2496 const char *make,
2497 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002498{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002499 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002500
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002501 output->allocation.x = x;
2502 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002503}
2504
2505static void
2506display_handle_mode(void *data,
2507 struct wl_output *wl_output,
2508 uint32_t flags,
2509 int width,
2510 int height,
2511 int refresh)
2512{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002513 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002514 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002515
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002516 if (flags & WL_OUTPUT_MODE_CURRENT) {
2517 output->allocation.width = width;
2518 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002519 if (display->output_configure_handler)
2520 (*display->output_configure_handler)(
2521 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002522 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002523}
2524
2525static const struct wl_output_listener output_listener = {
2526 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002527 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002528};
2529
2530static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002531display_add_output(struct display *d, uint32_t id)
2532{
2533 struct output *output;
2534
2535 output = malloc(sizeof *output);
2536 if (output == NULL)
2537 return;
2538
2539 memset(output, 0, sizeof *output);
2540 output->display = d;
2541 output->output =
2542 wl_display_bind(d->display, id, &wl_output_interface);
2543 wl_list_insert(d->output_list.prev, &output->link);
2544
2545 wl_output_add_listener(output->output, &output_listener, output);
2546}
2547
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002548static void
2549output_destroy(struct output *output)
2550{
2551 if (output->destroy_handler)
2552 (*output->destroy_handler)(output, output->user_data);
2553
2554 wl_output_destroy(output->output);
2555 wl_list_remove(&output->link);
2556 free(output);
2557}
2558
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002559void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002560display_set_output_configure_handler(struct display *display,
2561 display_output_handler_t handler)
2562{
2563 struct output *output;
2564
2565 display->output_configure_handler = handler;
2566 if (!handler)
2567 return;
2568
2569 wl_list_for_each(output, &display->output_list, link)
2570 (*display->output_configure_handler)(output,
2571 display->user_data);
2572}
2573
2574void
2575output_set_user_data(struct output *output, void *data)
2576{
2577 output->user_data = data;
2578}
2579
2580void *
2581output_get_user_data(struct output *output)
2582{
2583 return output->user_data;
2584}
2585
2586void
2587output_set_destroy_handler(struct output *output,
2588 display_output_handler_t handler)
2589{
2590 output->destroy_handler = handler;
2591 /* FIXME: implement this, once we have way to remove outputs */
2592}
2593
2594void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002595output_get_allocation(struct output *output, struct rectangle *allocation)
2596{
2597 *allocation = output->allocation;
2598}
2599
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002600struct wl_output *
2601output_get_wl_output(struct output *output)
2602{
2603 return output->output;
2604}
2605
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002606static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002607display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002608{
2609 struct input *input;
2610
2611 input = malloc(sizeof *input);
2612 if (input == NULL)
2613 return;
2614
2615 memset(input, 0, sizeof *input);
2616 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002617 input->input_device =
2618 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002619 input->pointer_focus = NULL;
2620 input->keyboard_focus = NULL;
2621 wl_list_insert(d->input_list.prev, &input->link);
2622
2623 wl_input_device_add_listener(input->input_device,
2624 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002625 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002626
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002627 input->data_device =
2628 wl_data_device_manager_get_data_device(d->data_device_manager,
2629 input->input_device);
2630 wl_data_device_add_listener(input->data_device,
2631 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002632}
2633
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002634static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002635input_destroy(struct input *input)
2636{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002637 input_remove_keyboard_focus(input);
2638 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002639
2640 if (input->drag_offer)
2641 data_offer_destroy(input->drag_offer);
2642
2643 if (input->selection_offer)
2644 data_offer_destroy(input->selection_offer);
2645
2646 wl_data_device_destroy(input->data_device);
2647 wl_list_remove(&input->link);
2648 wl_input_device_destroy(input->input_device);
2649 free(input);
2650}
2651
2652static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002653display_handle_global(struct wl_display *display, uint32_t id,
2654 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002655{
2656 struct display *d = data;
2657
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002658 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002659 d->compositor =
2660 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002661 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002662 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002663 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002664 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002665 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002666 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002667 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002668 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002669 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2670 d->data_device_manager =
2671 wl_display_bind(display, id,
2672 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002673 }
2674}
2675
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002676static void
2677display_render_frame(struct display *d)
2678{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002679 int radius = 8;
2680 cairo_t *cr;
2681
2682 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2683 cr = cairo_create(d->shadow);
2684 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2685 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2686 rounded_rect(cr, 16, 16, 112, 112, radius);
2687 cairo_fill(cr);
2688 cairo_destroy(cr);
2689 blur_surface(d->shadow, 64);
2690
2691 d->active_frame =
2692 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2693 cr = cairo_create(d->active_frame);
2694 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2695 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2696 rounded_rect(cr, 16, 16, 112, 112, radius);
2697 cairo_fill(cr);
2698 cairo_destroy(cr);
2699
2700 d->inactive_frame =
2701 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2702 cr = cairo_create(d->inactive_frame);
2703 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2704 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2705 rounded_rect(cr, 16, 16, 112, 112, radius);
2706 cairo_fill(cr);
2707 cairo_destroy(cr);
2708}
2709
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002710static void
2711init_xkb(struct display *d)
2712{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002713 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002714
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002715 names.rules = "evdev";
2716 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002717 names.layout = option_xkb_layout;
2718 names.variant = option_xkb_variant;
2719 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002720
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002721 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002722 if (!d->xkb) {
2723 fprintf(stderr, "Failed to compile keymap\n");
2724 exit(1);
2725 }
2726}
2727
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002728static void
2729fini_xkb(struct display *display)
2730{
2731 xkb_free_keymap(display->xkb);
2732}
2733
Yuval Fledel45568f62010-12-06 09:18:12 -05002734static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002735init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002736{
2737 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002738 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002739
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002740 static const EGLint argb_cfg_attribs[] = {
2741 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002742 EGL_RED_SIZE, 1,
2743 EGL_GREEN_SIZE, 1,
2744 EGL_BLUE_SIZE, 1,
2745 EGL_ALPHA_SIZE, 1,
2746 EGL_DEPTH_SIZE, 1,
2747 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2748 EGL_NONE
2749 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002750
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002751 static const EGLint rgb_cfg_attribs[] = {
2752 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2753 EGL_RED_SIZE, 1,
2754 EGL_GREEN_SIZE, 1,
2755 EGL_BLUE_SIZE, 1,
2756 EGL_ALPHA_SIZE, 0,
2757 EGL_DEPTH_SIZE, 1,
2758 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2759 EGL_NONE
2760 };
2761
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002762#ifdef USE_CAIRO_GLESV2
2763 static const EGLint context_attribs[] = {
2764 EGL_CONTEXT_CLIENT_VERSION, 2,
2765 EGL_NONE
2766 };
2767 EGLint api = EGL_OPENGL_ES_API;
2768#else
2769 EGLint *context_attribs = NULL;
2770 EGLint api = EGL_OPENGL_API;
2771#endif
2772
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002773 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002774 if (!eglInitialize(d->dpy, &major, &minor)) {
2775 fprintf(stderr, "failed to initialize display\n");
2776 return -1;
2777 }
2778
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002779 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002780 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2781 return -1;
2782 }
2783
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002784 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2785 &d->argb_config, 1, &n) || n != 1) {
2786 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002787 return -1;
2788 }
2789
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002790 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2791 &d->rgb_config, 1, &n) || n != 1) {
2792 fprintf(stderr, "failed to choose rgb config\n");
2793 return -1;
2794 }
2795
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002796 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2797 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002798 if (d->rgb_ctx == NULL) {
2799 fprintf(stderr, "failed to create context\n");
2800 return -1;
2801 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002802 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002803 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002804 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002805 fprintf(stderr, "failed to create context\n");
2806 return -1;
2807 }
2808
Benjamin Franzke0c991632011-09-27 21:57:31 +02002809 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002810 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002811 return -1;
2812 }
2813
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002814#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002815 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2816 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002817 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002818 return -1;
2819 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002820 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2821 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2822 fprintf(stderr, "failed to get cairo egl argb device\n");
2823 return -1;
2824 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002825#endif
2826
2827 return 0;
2828}
2829
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002830static void
2831fini_egl(struct display *display)
2832{
2833#ifdef HAVE_CAIRO_EGL
2834 cairo_device_destroy(display->argb_device);
2835 cairo_device_destroy(display->rgb_device);
2836#endif
2837
2838 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2839 EGL_NO_CONTEXT);
2840
2841 eglTerminate(display->dpy);
2842 eglReleaseThread();
2843}
2844
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002845static int
2846event_mask_update(uint32_t mask, void *data)
2847{
2848 struct display *d = data;
2849
2850 d->mask = mask;
2851
2852 return 0;
2853}
2854
2855static void
2856handle_display_data(struct task *task, uint32_t events)
2857{
2858 struct display *display =
2859 container_of(task, struct display, display_task);
2860
2861 wl_display_iterate(display->display, display->mask);
2862}
2863
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002864struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002865display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002866{
2867 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002868 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002869 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002870 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002871
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002872 g_type_init();
2873
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002874 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002875 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002876 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002877
2878 xkb_option_group = g_option_group_new("xkb",
2879 "Keyboard options",
2880 "Show all XKB options",
2881 NULL, NULL);
2882 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2883 g_option_context_add_group (context, xkb_option_group);
2884
2885 if (!g_option_context_parse(context, argc, argv, &error)) {
2886 fprintf(stderr, "option parsing failed: %s\n", error->message);
2887 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002888 }
2889
Tim Wiederhake748f6722011-01-23 23:25:25 +01002890 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002891
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002892 d = malloc(sizeof *d);
2893 if (d == NULL)
2894 return NULL;
2895
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002896 memset(d, 0, sizeof *d);
2897
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002898 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002899 if (d->display == NULL) {
2900 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002901 return NULL;
2902 }
2903
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002904 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2905 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2906 d->display_task.run = handle_display_data;
2907 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2908
2909 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002910 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002911 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002912
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002913 /* Set up listener so we'll catch all events. */
2914 wl_display_add_global_listener(d->display,
2915 display_handle_global, d);
2916
2917 /* Process connection events. */
2918 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002919 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002920 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002921
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002922 d->image_target_texture_2d =
2923 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2924 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2925 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2926
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002927 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002928
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002929 display_render_frame(d);
2930
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002931 wl_list_init(&d->window_list);
2932
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002933 init_xkb(d);
2934
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002935 return d;
2936}
2937
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002938static void
2939display_destroy_outputs(struct display *display)
2940{
2941 struct output *tmp;
2942 struct output *output;
2943
2944 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2945 output_destroy(output);
2946}
2947
Pekka Paalanene1207c72011-12-16 12:02:09 +02002948static void
2949display_destroy_inputs(struct display *display)
2950{
2951 struct input *tmp;
2952 struct input *input;
2953
2954 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2955 input_destroy(input);
2956}
2957
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002958void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002959display_destroy(struct display *display)
2960{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002961 if (!wl_list_empty(&display->window_list))
2962 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2963
2964 if (!wl_list_empty(&display->deferred_list))
2965 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2966
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002967 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002968 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002969
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002970 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002971
2972 cairo_surface_destroy(display->active_frame);
2973 cairo_surface_destroy(display->inactive_frame);
2974 cairo_surface_destroy(display->shadow);
2975 destroy_pointer_surfaces(display);
2976
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002977 fini_egl(display);
2978
Pekka Paalanenc2052982011-12-16 11:41:32 +02002979 if (display->shell)
2980 wl_shell_destroy(display->shell);
2981
2982 if (display->shm)
2983 wl_shm_destroy(display->shm);
2984
2985 if (display->data_device_manager)
2986 wl_data_device_manager_destroy(display->data_device_manager);
2987
2988 wl_compositor_destroy(display->compositor);
2989
2990 close(display->epoll_fd);
2991
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002992 wl_display_flush(display->display);
2993 wl_display_destroy(display->display);
2994 free(display);
2995}
2996
2997void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002998display_set_user_data(struct display *display, void *data)
2999{
3000 display->user_data = data;
3001}
3002
3003void *
3004display_get_user_data(struct display *display)
3005{
3006 return display->user_data;
3007}
3008
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003009struct wl_display *
3010display_get_display(struct display *display)
3011{
3012 return display->display;
3013}
3014
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003015struct output *
3016display_get_output(struct display *display)
3017{
3018 return container_of(display->output_list.next, struct output, link);
3019}
3020
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003021struct wl_compositor *
3022display_get_compositor(struct display *display)
3023{
3024 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003025}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003026
3027EGLDisplay
3028display_get_egl_display(struct display *d)
3029{
3030 return d->dpy;
3031}
3032
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003033struct wl_data_source *
3034display_create_data_source(struct display *display)
3035{
3036 return wl_data_device_manager_create_data_source(display->data_device_manager);
3037}
3038
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003039EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003040display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003041{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003042 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003043}
3044
Benjamin Franzke0c991632011-09-27 21:57:31 +02003045EGLConfig
3046display_get_argb_egl_config(struct display *d)
3047{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003048 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003049}
3050
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003051struct wl_shell *
3052display_get_shell(struct display *display)
3053{
3054 return display->shell;
3055}
3056
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003057int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003058display_acquire_window_surface(struct display *display,
3059 struct window *window,
3060 EGLContext ctx)
3061{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003062#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003063 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003064 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003065
3066 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003067 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003068 device = cairo_surface_get_device(window->cairo_surface);
3069 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003070 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003071
Benjamin Franzke0c991632011-09-27 21:57:31 +02003072 if (!ctx) {
3073 if (device == display->rgb_device)
3074 ctx = display->rgb_ctx;
3075 else if (device == display->argb_device)
3076 ctx = display->argb_ctx;
3077 else
3078 assert(0);
3079 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003080
3081 data = cairo_surface_get_user_data(window->cairo_surface,
3082 &surface_data_key);
3083
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003084 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003085 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003086 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3087 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003088
3089 return 0;
3090#else
3091 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003092#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003093}
3094
3095void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003096display_release_window_surface(struct display *display,
3097 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003098{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003099#ifdef HAVE_CAIRO_EGL
3100 cairo_device_t *device;
3101
3102 device = cairo_surface_get_device(window->cairo_surface);
3103 if (!device)
3104 return;
3105
3106 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003107 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003108 cairo_device_release(device);
3109#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003110}
3111
3112void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003113display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003114{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003115 wl_list_insert(&display->deferred_list, &task->link);
3116}
3117
3118void
3119display_watch_fd(struct display *display,
3120 int fd, uint32_t events, struct task *task)
3121{
3122 struct epoll_event ep;
3123
3124 ep.events = events;
3125 ep.data.ptr = task;
3126 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3127}
3128
3129void
3130display_run(struct display *display)
3131{
3132 struct task *task;
3133 struct epoll_event ep[16];
3134 int i, count;
3135
Pekka Paalanen826d7952011-12-15 10:14:07 +02003136 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003137 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003138 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003139
3140 while (!wl_list_empty(&display->deferred_list)) {
3141 task = container_of(display->deferred_list.next,
3142 struct task, link);
3143 wl_list_remove(&task->link);
3144 task->run(task, 0);
3145 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003146
3147 if (!display->running)
3148 break;
3149
3150 wl_display_flush(display->display);
3151
3152 count = epoll_wait(display->epoll_fd,
3153 ep, ARRAY_LENGTH(ep), -1);
3154 for (i = 0; i < count; i++) {
3155 task = ep[i].data.ptr;
3156 task->run(task, ep[i].events);
3157 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003158 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003159}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003160
3161void
3162display_exit(struct display *display)
3163{
3164 display->running = 0;
3165}