blob: 47177110cc6e021ca9e8e53ad94509cb53356eff [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
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500133 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500134 struct window *menu;
135
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500136 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400137 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138};
139
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500140struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500141 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500142 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400143 struct wl_list link;
144 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500145 widget_resize_handler_t resize_handler;
146 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500147 widget_enter_handler_t enter_handler;
148 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500149 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500150 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400151 void *user_data;
152};
153
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400154struct input {
155 struct display *display;
156 struct wl_input_device *input_device;
157 struct window *pointer_focus;
158 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400159 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400160 uint32_t modifiers;
161 int32_t x, y, sx, sy;
162 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400163
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500164 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500165 struct widget *grab;
166 uint32_t grab_button;
167
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400168 struct wl_data_device *data_device;
169 struct data_offer *drag_offer;
170 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400171};
172
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500173struct output {
174 struct display *display;
175 struct wl_output *output;
176 struct rectangle allocation;
177 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200178
179 display_output_handler_t destroy_handler;
180 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500181};
182
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500183struct frame {
184 struct widget *widget;
185 struct widget *child;
186 int margin;
187};
188
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500189struct menu {
190 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500191 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500192 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500193 const char **entries;
194 uint32_t time;
195 int current;
196 int count;
197 menu_func_t func;
198};
199
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400200enum {
201 POINTER_DEFAULT = 100,
202 POINTER_UNSET
203};
204
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500205enum window_location {
206 WINDOW_INTERIOR = 0,
207 WINDOW_RESIZING_TOP = 1,
208 WINDOW_RESIZING_BOTTOM = 2,
209 WINDOW_RESIZING_LEFT = 4,
210 WINDOW_RESIZING_TOP_LEFT = 5,
211 WINDOW_RESIZING_BOTTOM_LEFT = 6,
212 WINDOW_RESIZING_RIGHT = 8,
213 WINDOW_RESIZING_TOP_RIGHT = 9,
214 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
215 WINDOW_RESIZING_MASK = 15,
216 WINDOW_EXTERIOR = 16,
217 WINDOW_TITLEBAR = 17,
218 WINDOW_CLIENT_AREA = 18,
219};
220
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400221const char *option_xkb_layout = "us";
222const char *option_xkb_variant = "";
223const char *option_xkb_options = "";
224
225static const GOptionEntry xkb_option_entries[] = {
226 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
227 &option_xkb_layout, "XKB Layout" },
228 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
229 &option_xkb_variant, "XKB Variant" },
230 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
231 &option_xkb_options, "XKB Options" },
232 { NULL }
233};
234
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400235static const cairo_user_data_key_t surface_data_key;
236struct surface_data {
237 struct wl_buffer *buffer;
238};
239
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500240#define MULT(_d,c,a,t) \
241 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
242
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500243#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400244
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100245struct egl_window_surface_data {
246 struct display *display;
247 struct wl_surface *surface;
248 struct wl_egl_window *window;
249 EGLSurface surf;
250};
251
252static void
253egl_window_surface_data_destroy(void *p)
254{
255 struct egl_window_surface_data *data = p;
256 struct display *d = data->display;
257
258 eglDestroySurface(d->dpy, data->surf);
259 wl_egl_window_destroy(data->window);
260 data->surface = NULL;
261
262 free(p);
263}
264
265static cairo_surface_t *
266display_create_egl_window_surface(struct display *display,
267 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400268 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100269 struct rectangle *rectangle)
270{
271 cairo_surface_t *cairo_surface;
272 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400273 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200274 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100275
276 data = malloc(sizeof *data);
277 if (data == NULL)
278 return NULL;
279
280 data->display = display;
281 data->surface = surface;
282
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400283 if (flags & SURFACE_OPAQUE) {
284 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200285 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400286 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500287 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200288 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400289 }
290
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400291 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100292 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100294
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400295 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500296 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100297
Benjamin Franzke0c991632011-09-27 21:57:31 +0200298 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 data->surf,
300 rectangle->width,
301 rectangle->height);
302
303 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
304 data, egl_window_surface_data_destroy);
305
306 return cairo_surface;
307}
308
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500309struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400310 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200311 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400312 EGLImageKHR image;
313 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800314 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500315 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400316};
317
318static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500319egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400320{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500321 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800322 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323
Benjamin Franzke0c991632011-09-27 21:57:31 +0200324 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200326 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500328 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500330 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500331 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332}
333
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500334EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500335display_get_image_for_egl_image_surface(struct display *display,
336 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500337{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500339
340 data = cairo_surface_get_user_data (surface, &surface_data_key);
341
342 return data->image;
343}
344
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500345static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500346display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400347 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500348 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400349{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500350 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400351 EGLDisplay dpy = display->dpy;
352 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200353 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354
355 data = malloc(sizeof *data);
356 if (data == NULL)
357 return NULL;
358
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800359 data->display = display;
360
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400361 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400362 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500363 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000364 free(data);
365 return NULL;
366 }
367
Benjamin Franzke0c991632011-09-27 21:57:31 +0200368 if (flags & SURFACE_OPAQUE) {
369 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200370 content = CAIRO_CONTENT_COLOR;
371 } else {
372 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200373 content = CAIRO_CONTENT_COLOR_ALPHA;
374 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400375
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500376 data->image = display->create_image(dpy, NULL,
377 EGL_NATIVE_PIXMAP_KHR,
378 (EGLClientBuffer) data->pixmap,
379 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500380 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500381 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 free(data);
383 return NULL;
384 }
385
386 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400387 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500388
Benjamin Franzke0c991632011-09-27 21:57:31 +0200389 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400390 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400391 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500392 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200393 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400394
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 surface = cairo_gl_surface_create_for_texture(data->device,
396 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 data->texture,
398 rectangle->width,
399 rectangle->height);
400
401 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500402 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
404 return surface;
405}
406
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500407static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500408display_create_egl_image_surface_from_file(struct display *display,
409 const char *filename,
410 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411{
412 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400413 GdkPixbuf *pixbuf;
414 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400415 int stride, i;
416 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500417 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400418
419 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400420 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400421 FALSE, &error);
422 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400423 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400424
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400425 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
426 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500427 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400429 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400430
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400431
432 stride = gdk_pixbuf_get_rowstride(pixbuf);
433 pixels = gdk_pixbuf_get_pixels(pixbuf);
434
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438 while (p < end) {
439 unsigned int t;
440
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441 MULT(p[0], p[0], p[3], t);
442 MULT(p[1], p[1], p[3], t);
443 MULT(p[2], p[2], p[3], t);
444 p += 4;
445
446 }
447 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400448
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200449 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000450 if (surface == NULL) {
451 g_object_unref(pixbuf);
452 return NULL;
453 }
454
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800455 data = cairo_surface_get_user_data(surface, &surface_data_key);
456
Benjamin Franzke0c991632011-09-27 21:57:31 +0200457 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800458 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800459 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
460 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200461 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400462
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500463 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400464
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400465 return surface;
466}
467
468#endif
469
470struct wl_buffer *
471display_get_buffer_for_surface(struct display *display,
472 cairo_surface_t *surface)
473{
474 struct surface_data *data;
475
476 data = cairo_surface_get_user_data (surface, &surface_data_key);
477
478 return data->buffer;
479}
480
481struct shm_surface_data {
482 struct surface_data data;
483 void *map;
484 size_t length;
485};
486
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500487static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400488shm_surface_data_destroy(void *p)
489{
490 struct shm_surface_data *data = p;
491
492 wl_buffer_destroy(data->data.buffer);
493 munmap(data->map, data->length);
494}
495
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500496static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400497display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400498 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400499{
500 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400501 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400502 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800503 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504 char filename[] = "/tmp/wayland-shm-XXXXXX";
505
506 data = malloc(sizeof *data);
507 if (data == NULL)
508 return NULL;
509
510 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
511 rectangle->width);
512 data->length = stride * rectangle->height;
513 fd = mkstemp(filename);
514 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000515 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516 return NULL;
517 }
518 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000519 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400520 close(fd);
521 return NULL;
522 }
523
524 data->map = mmap(NULL, data->length,
525 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
526 unlink(filename);
527
528 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000529 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 close(fd);
531 return NULL;
532 }
533
534 surface = cairo_image_surface_create_for_data (data->map,
535 CAIRO_FORMAT_ARGB32,
536 rectangle->width,
537 rectangle->height,
538 stride);
539
540 cairo_surface_set_user_data (surface, &surface_data_key,
541 data, shm_surface_data_destroy);
542
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400543 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500544 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400545 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500546 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400547
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548 data->data.buffer = wl_shm_create_buffer(display->shm,
549 fd,
550 rectangle->width,
551 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400552 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400553
554 close(fd);
555
556 return surface;
557}
558
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500559static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560display_create_shm_surface_from_file(struct display *display,
561 const char *filename,
562 struct rectangle *rect)
563{
564 cairo_surface_t *surface;
565 GdkPixbuf *pixbuf;
566 GError *error = NULL;
567 int stride, i;
568 unsigned char *pixels, *p, *end, *dest_data;
569 int dest_stride;
570 uint32_t *d;
571
572 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
573 rect->width, rect->height,
574 FALSE, &error);
575 if (error != NULL)
576 return NULL;
577
578 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
579 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500580 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581 return NULL;
582 }
583
584 stride = gdk_pixbuf_get_rowstride(pixbuf);
585 pixels = gdk_pixbuf_get_pixels(pixbuf);
586
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400587 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000588 if (surface == NULL) {
589 g_object_unref(pixbuf);
590 return NULL;
591 }
592
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593 dest_data = cairo_image_surface_get_data (surface);
594 dest_stride = cairo_image_surface_get_stride (surface);
595
596 for (i = 0; i < rect->height; i++) {
597 d = (uint32_t *) (dest_data + i * dest_stride);
598 p = pixels + i * stride;
599 end = p + rect->width * 4;
600 while (p < end) {
601 unsigned int t;
602 unsigned char a, r, g, b;
603
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400604 a = p[3];
605 MULT(r, p[0], a, t);
606 MULT(g, p[1], a, t);
607 MULT(b, p[2], a, t);
608 p += 4;
609 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
610 }
611 }
612
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500613 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614
615 return surface;
616}
617
nobled7b87cb02011-02-01 18:51:47 +0000618static int
619check_size(struct rectangle *rect)
620{
621 if (rect->width && rect->height)
622 return 0;
623
624 fprintf(stderr, "tried to create surface of "
625 "width: %d, height: %d\n", rect->width, rect->height);
626 return -1;
627}
628
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629cairo_surface_t *
630display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400632 struct rectangle *rectangle,
633 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400634{
nobled7b87cb02011-02-01 18:51:47 +0000635 if (check_size(rectangle) < 0)
636 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500637#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500638 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100639 if (surface)
640 return display_create_egl_window_surface(display,
641 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400642 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643 rectangle);
644 else
645 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500648 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400650 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400651}
652
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500653static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654display_create_surface_from_file(struct display *display,
655 const char *filename,
656 struct rectangle *rectangle)
657{
nobled7b87cb02011-02-01 18:51:47 +0000658 if (check_size(rectangle) < 0)
659 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500660#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500661 if (display->dpy) {
662 return display_create_egl_image_surface_from_file(display,
663 filename,
664 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500665 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400666#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500667 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668}
Yuval Fledel45568f62010-12-06 09:18:12 -0500669 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670 const char *filename;
671 int hotspot_x, hotspot_y;
672} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500673 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
674 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
675 { DATADIR "/weston/bottom_side.png", 16, 20 },
676 { DATADIR "/weston/grabbing.png", 20, 17 },
677 { DATADIR "/weston/left_ptr.png", 10, 5 },
678 { DATADIR "/weston/left_side.png", 10, 20 },
679 { DATADIR "/weston/right_side.png", 30, 19 },
680 { DATADIR "/weston/top_left_corner.png", 8, 8 },
681 { DATADIR "/weston/top_right_corner.png", 26, 8 },
682 { DATADIR "/weston/top_side.png", 18, 8 },
683 { DATADIR "/weston/xterm.png", 15, 15 },
684 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685};
686
687static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400689{
690 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400691 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400692 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400693
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400694 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695 display->pointer_surfaces =
696 malloc(count * sizeof *display->pointer_surfaces);
697 rect.width = width;
698 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400699 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400700 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701 display_create_surface_from_file(display,
702 pointer_images[i].filename,
703 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100704 if (!display->pointer_surfaces[i]) {
705 fprintf(stderr, "Error loading pointer image: %s\n",
706 pointer_images[i].filename);
707 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400708 }
709
710}
711
Pekka Paalanen325bb602011-12-19 10:31:45 +0200712static void
713destroy_pointer_surfaces(struct display *display)
714{
715 int i, count;
716
717 count = ARRAY_LENGTH(pointer_images);
718 for (i = 0; i < count; ++i) {
719 if (display->pointer_surfaces[i])
720 cairo_surface_destroy(display->pointer_surfaces[i]);
721 }
722 free(display->pointer_surfaces);
723}
724
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400725cairo_surface_t *
726display_get_pointer_surface(struct display *display, int pointer,
727 int *width, int *height,
728 int *hotspot_x, int *hotspot_y)
729{
730 cairo_surface_t *surface;
731
732 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500733#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734 *width = cairo_gl_surface_get_width(surface);
735 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000736#else
737 *width = cairo_image_surface_get_width(surface);
738 *height = cairo_image_surface_get_height(surface);
739#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400740 *hotspot_x = pointer_images[pointer].hotspot_x;
741 *hotspot_y = pointer_images[pointer].hotspot_y;
742
743 return cairo_surface_reference(surface);
744}
745
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400746static void
747window_attach_surface(struct window *window);
748
749static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400751{
752 struct window *window = data;
753
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400754 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400755 cairo_surface_destroy(window->pending_surface);
756 window->pending_surface = NULL;
757 if (window->cairo_surface)
758 window_attach_surface(window);
759}
760
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400761static const struct wl_callback_listener free_surface_listener = {
762 free_surface
763};
764
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500765static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200766window_get_resize_dx_dy(struct window *window, int *x, int *y)
767{
768 if (window->resize_edges & WINDOW_RESIZING_LEFT)
769 *x = window->server_allocation.width - window->allocation.width;
770 else
771 *x = 0;
772
773 if (window->resize_edges & WINDOW_RESIZING_TOP)
774 *y = window->server_allocation.height -
775 window->allocation.height;
776 else
777 *y = 0;
778
779 window->resize_edges = 0;
780}
781
782static void
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400783window_set_type(struct window *window)
784{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200785 if (!window->shell_surface)
786 return;
787
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400788 switch (window->type) {
789 case TYPE_FULLSCREEN:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200790 wl_shell_surface_set_fullscreen(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400791 break;
792 case TYPE_TOPLEVEL:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200793 wl_shell_surface_set_toplevel(window->shell_surface);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400794 break;
795 case TYPE_TRANSIENT:
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200796 wl_shell_surface_set_transient(window->shell_surface,
797 window->parent->shell_surface,
798 window->x, window->y, 0);
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400799 break;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500800 case TYPE_MENU:
801 break;
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400802 case TYPE_CUSTOM:
803 break;
804 }
805}
806
807static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500808window_attach_surface(struct window *window)
809{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400810 struct display *display = window->display;
811 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400812 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000813#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100814 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000815#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500816 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100817
Kristian Høgsberge4fb78d2011-09-09 18:20:52 -0400818 if (display->shell)
819 window_set_type(window);
820
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100821 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000822#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
825 &surface_data_key);
826
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
831 break;
832 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000833#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100834 case WINDOW_BUFFER_TYPE_SHM:
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200835 window_get_resize_dx_dy(window, &x, &y);
836
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100837 if (window->pending_surface != NULL)
838 return;
839
840 window->pending_surface = window->cairo_surface;
841 window->cairo_surface = NULL;
842
843 buffer =
844 display_get_buffer_for_surface(display,
845 window->pending_surface);
846
847 wl_surface_attach(window->surface, buffer, x, y);
848 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400849 cb = wl_display_sync(display->display);
850 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100851 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000852 default:
853 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100854 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500855
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500856 wl_surface_damage(window->surface, 0, 0,
857 window->allocation.width,
858 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500859}
860
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500861void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400862window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500863{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100864 if (window->cairo_surface) {
865 switch (window->buffer_type) {
866 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
867 case WINDOW_BUFFER_TYPE_SHM:
868 display_surface_damage(window->display,
869 window->cairo_surface,
870 0, 0,
871 window->allocation.width,
872 window->allocation.height);
873 break;
874 default:
875 break;
876 }
877 window_attach_surface(window);
878 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500879}
880
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400881void
882window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500884 cairo_surface_reference(surface);
885
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
888
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500889 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890}
891
Benjamin Franzke22d54812011-07-16 19:50:32 +0000892#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100893static void
894window_resize_cairo_window_surface(struct window *window)
895{
896 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200897 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898
899 data = cairo_surface_get_user_data(window->cairo_surface,
900 &surface_data_key);
901
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200902 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100903 wl_egl_window_resize(data->window,
904 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200905 window->allocation.height,
906 x,y);
907
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100908 cairo_gl_surface_set_size(window->cairo_surface,
909 window->allocation.width,
910 window->allocation.height);
911}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000912#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100913
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400914struct display *
915window_get_display(struct window *window)
916{
917 return window->display;
918}
919
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400920void
921window_create_surface(struct window *window)
922{
923 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 uint32_t flags = 0;
925
926 if (!window->transparent)
927 flags = SURFACE_OPAQUE;
928
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400929 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500930#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100931 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
932 if (window->cairo_surface) {
933 window_resize_cairo_window_surface(window);
934 return;
935 }
936 surface = display_create_surface(window->display,
937 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400938 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100939 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500940 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400941 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400944 break;
945#endif
946 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400947 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400948 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400949 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800950 default:
951 surface = NULL;
952 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400953 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400954
955 window_set_surface(window, surface);
956 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400957}
958
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400959void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500960window_destroy(struct window *window)
961{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200962 struct display *display = window->display;
963 struct input *input;
964
965 if (window->redraw_scheduled)
966 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500967 if (window->resize_scheduled)
968 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200969
970 wl_list_for_each(input, &display->input_list, link) {
971 if (input->pointer_focus == window)
972 input->pointer_focus = NULL;
Kristian Høgsberg62444802012-01-11 11:18:14 -0500973 if (input->focus_widget &&
974 input->focus_widget->window == window)
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500975 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200976 if (input->keyboard_focus == window)
977 input->keyboard_focus = NULL;
978 }
979
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200980 if (window->shell_surface)
981 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500982 wl_surface_destroy(window->surface);
983 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200984
985 if (window->cairo_surface != NULL)
986 cairo_surface_destroy(window->cairo_surface);
987 if (window->pending_surface != NULL)
988 cairo_surface_destroy(window->pending_surface);
989
990 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500991 free(window);
992}
993
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500994static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500995widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400996{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500997 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400998
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500999 wl_list_for_each(child, &widget->child_list, link) {
1000 target = widget_find_widget(child, x, y);
1001 if (target)
1002 return target;
1003 }
1004
1005 if (widget->allocation.x <= x &&
1006 x < widget->allocation.x + widget->allocation.width &&
1007 widget->allocation.y <= y &&
1008 y < widget->allocation.y + widget->allocation.height) {
1009 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010 }
1011
1012 return NULL;
1013}
1014
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001015static struct widget *
1016widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001017{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001018 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001019
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001020 widget = malloc(sizeof *widget);
1021 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001022 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001023 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001024 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001025 wl_list_init(&widget->child_list);
1026
1027 return widget;
1028}
1029
1030struct widget *
1031window_add_widget(struct window *window, void *data)
1032{
1033 window->widget = widget_create(window, data);
1034 wl_list_init(&window->widget->link);
1035
1036 return window->widget;
1037}
1038
1039struct widget *
1040widget_add_widget(struct widget *parent, void *data)
1041{
1042 struct widget *widget;
1043
1044 widget = widget_create(parent->window, data);
1045 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001046
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001047 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001048}
1049
1050void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001051widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001052{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053 wl_list_remove(&widget->link);
1054 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055}
1056
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061}
1062
1063void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001064widget_set_size(struct widget *widget, int32_t width, int32_t height)
1065{
1066 struct window *window = widget->window;
1067
1068 widget->allocation.width = width;
1069 widget->allocation.height = height;
1070
1071 window->allocation.x = 0;
1072 window->allocation.y = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001073 window->allocation.width = width;
1074 window->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001075}
1076
1077void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001078widget_set_allocation(struct widget *widget,
1079 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001080{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001081 widget->allocation.x = x;
1082 widget->allocation.y = y;
1083 widget->allocation.width = width;
1084 widget->allocation.height = height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085}
1086
1087void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001088widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001090 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001091}
1092
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001093void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001094widget_set_resize_handler(struct widget *widget,
1095 widget_resize_handler_t handler)
1096{
1097 widget->resize_handler = handler;
1098}
1099
1100void
1101widget_set_redraw_handler(struct widget *widget,
1102 widget_redraw_handler_t handler)
1103{
1104 widget->redraw_handler = handler;
1105}
1106
1107void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001108widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001109{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001110 widget->enter_handler = handler;
1111}
1112
1113void
1114widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1115{
1116 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001117}
1118
1119void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001120widget_set_motion_handler(struct widget *widget,
1121 widget_motion_handler_t handler)
1122{
1123 widget->motion_handler = handler;
1124}
1125
1126void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001127widget_set_button_handler(struct widget *widget,
1128 widget_button_handler_t handler)
1129{
1130 widget->button_handler = handler;
1131}
1132
1133void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001134widget_schedule_redraw(struct widget *widget)
1135{
1136 window_schedule_redraw(widget->window);
1137}
1138
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001139cairo_surface_t *
1140window_get_surface(struct window *window)
1141{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001142 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001143}
1144
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001145struct wl_surface *
1146window_get_wl_surface(struct window *window)
1147{
1148 return window->surface;
1149}
1150
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001151struct wl_shell_surface *
1152window_get_wl_shell_surface(struct window *window)
1153{
1154 return window->shell_surface;
1155}
1156
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001157static void
1158frame_resize_handler(struct widget *widget,
1159 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001160{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001161 struct frame *frame = data;
1162 struct widget *child = frame->child;
1163 struct rectangle allocation;
1164 int decoration_width, decoration_height;
1165
1166 decoration_width = 20 + frame->margin * 2;
1167 decoration_height = 60 + frame->margin * 2;
1168
1169 allocation.x = 10 + frame->margin;
1170 allocation.y = 50 + frame->margin;
1171 allocation.width = width - decoration_width;
1172 allocation.height = height - decoration_height;
1173
1174 widget_set_allocation(child, allocation.x, allocation.y,
1175 allocation.width, allocation.height);
1176
1177 if (child->resize_handler)
1178 child->resize_handler(child,
1179 allocation.width,
1180 allocation.height,
1181 child->user_data);
1182
1183 widget_set_allocation(widget, 0, 0,
1184 child->allocation.width + decoration_width,
1185 child->allocation.height + decoration_height);
1186}
1187
1188static void
1189frame_redraw_handler(struct widget *widget, void *data)
1190{
1191 struct frame *frame = data;
1192 cairo_t *cr;
1193 cairo_text_extents_t extents;
1194 cairo_surface_t *source;
1195 int width, height, shadow_dx = 3, shadow_dy = 3;
1196 struct window *window = widget->window;
1197
1198 width = widget->allocation.width;
1199 height = widget->allocation.height;
1200
1201 cr = cairo_create(window->cairo_surface);
1202
1203 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1204 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1205 cairo_paint(cr);
1206
1207 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1208 tile_mask(cr, window->display->shadow,
1209 shadow_dx, shadow_dy, width, height,
1210 frame->margin + 10 - shadow_dx,
1211 frame->margin + 10 - shadow_dy);
1212
1213 if (window->keyboard_device)
1214 source = window->display->active_frame;
1215 else
1216 source = window->display->inactive_frame;
1217
1218 tile_source(cr, source, 0, 0, width, height,
1219 frame->margin + 10, frame->margin + 50);
1220
1221 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1222 cairo_set_font_size(cr, 14);
1223 cairo_text_extents(cr, window->title, &extents);
1224 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1225 if (window->keyboard_device)
1226 cairo_set_source_rgb(cr, 0, 0, 0);
1227 else
1228 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1229 cairo_show_text(cr, window->title);
1230
1231 cairo_destroy(cr);
1232}
1233
1234static int
1235frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1236{
1237 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001238 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001239 const int grip_size = 8;
1240
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001242 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001244 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001246 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001247 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001248 hlocation = WINDOW_RESIZING_RIGHT;
1249 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001250 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001251
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001253 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001255 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001257 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001258 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001259 vlocation = WINDOW_RESIZING_BOTTOM;
1260 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001262
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001263 location = vlocation | hlocation;
1264 if (location & WINDOW_EXTERIOR)
1265 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001266 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001267 location = WINDOW_TITLEBAR;
1268 else if (location == WINDOW_INTERIOR)
1269 location = WINDOW_CLIENT_AREA;
1270
1271 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001272}
1273
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001274static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001277 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001279 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 switch (location) {
1281 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001288 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001295 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001296 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001297 case WINDOW_EXTERIOR:
1298 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001300 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001301 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001302}
1303
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001304static void
1305frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001307 switch (index) {
1308 case 0: /* close */
1309 if (window->close_handler)
1310 window->close_handler(window->parent,
1311 window->user_data);
1312 else
1313 display_exit(window->display);
1314 break;
1315 case 1: /* fullscreen */
1316 /* we don't have a way to get out of fullscreen for now */
1317 window_set_fullscreen(window, 1);
1318 break;
1319 case 2: /* rotate */
1320 case 3: /* scale */
1321 break;
1322 }
1323}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001325static int
1326frame_enter_handler(struct widget *widget,
1327 struct input *input, uint32_t time,
1328 int32_t x, int32_t y, void *data)
1329{
1330 return frame_get_pointer_image_for_location(data, input);
1331}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001332
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001333static int
1334frame_motion_handler(struct widget *widget,
1335 struct input *input, uint32_t time,
1336 int32_t x, int32_t y, void *data)
1337{
1338 return frame_get_pointer_image_for_location(data, input);
1339}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001340
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001341static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001342frame_button_handler(struct widget *widget,
1343 struct input *input, uint32_t time,
1344 int button, int state, void *data)
1345
1346{
1347 struct frame *frame = data;
1348 struct window *window = widget->window;
1349 int location;
1350 int32_t x, y;
1351 static const char *entries[] = {
1352 "Close", "Fullscreen", "Rotate", "Scale"
1353 };
1354
1355 location = frame_get_pointer_location(frame, input->sx, input->sy);
1356
1357 if (window->display->shell && button == BTN_LEFT && state == 1) {
1358 switch (location) {
1359 case WINDOW_TITLEBAR:
1360 if (!window->shell_surface)
1361 break;
1362 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001363 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001364 wl_shell_surface_move(window->shell_surface,
1365 input_get_input_device(input),
1366 time);
1367 break;
1368 case WINDOW_RESIZING_TOP:
1369 case WINDOW_RESIZING_BOTTOM:
1370 case WINDOW_RESIZING_LEFT:
1371 case WINDOW_RESIZING_RIGHT:
1372 case WINDOW_RESIZING_TOP_LEFT:
1373 case WINDOW_RESIZING_TOP_RIGHT:
1374 case WINDOW_RESIZING_BOTTOM_LEFT:
1375 case WINDOW_RESIZING_BOTTOM_RIGHT:
1376 if (!window->shell_surface)
1377 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001378 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001379 wl_shell_surface_resize(window->shell_surface,
1380 input_get_input_device(input),
1381 time, location);
1382 break;
1383 }
1384 } else if (button == BTN_RIGHT && state == 1) {
1385 input_get_position(input, &x, &y);
1386 window->menu = window_create_menu(window->display,
1387 input, time,
1388 window,
1389 x - 10, y - 10,
1390 frame_menu_func,
1391 entries, 4);
1392 window_schedule_redraw(window->menu);
1393 }
1394}
1395
1396struct widget *
1397frame_create(struct window *window, void *data)
1398{
1399 struct frame *frame;
1400
1401 frame = malloc(sizeof *frame);
1402 memset(frame, 0, sizeof *frame);
1403
1404 frame->widget = window_add_widget(window, frame);
1405 frame->child = widget_add_widget(frame->widget, data);
1406 frame->margin = 16;
1407
1408 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1409 widget_set_resize_handler(frame->widget, frame_resize_handler);
1410 widget_set_enter_handler(frame->widget, frame_enter_handler);
1411 widget_set_motion_handler(frame->widget, frame_motion_handler);
1412 widget_set_button_handler(frame->widget, frame_button_handler);
1413
1414 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001415}
1416
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001417static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001418input_set_focus_widget(struct input *input, struct widget *focus,
1419 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001420{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001421 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001422 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001423
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001424 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001425 return;
1426
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001427 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001428 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001429 widget = old;
1430 if (input->grab)
1431 widget = input->grab;
1432 if (widget->leave_handler)
1433 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001434 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001435 }
1436
1437 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001438 widget = focus;
1439 if (input->grab)
1440 widget = input->grab;
1441 if (widget->enter_handler)
1442 pointer = widget->enter_handler(focus, input, time,
1443 x, y,
1444 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001445 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001446
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001447 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001448 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001449}
1450
1451static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001452input_handle_motion(void *data, struct wl_input_device *input_device,
1453 uint32_t time,
1454 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001455{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001456 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001457 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001458 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001459 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001460
1461 input->x = x;
1462 input->y = y;
1463 input->sx = sx;
1464 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001465
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001466 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001467 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001468 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001469 }
1470
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001471 if (input->grab)
1472 widget = input->grab;
1473 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001474 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001475 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001476 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001477 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001478 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001479
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001480 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001481}
1482
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001483void
1484input_grab(struct input *input, struct widget *widget, uint32_t button)
1485{
1486 input->grab = widget;
1487 input->grab_button = button;
1488}
1489
1490void
1491input_ungrab(struct input *input, uint32_t time)
1492{
1493 struct widget *widget;
1494
1495 input->grab = NULL;
1496 if (input->pointer_focus) {
1497 widget = widget_find_widget(input->pointer_focus->widget,
1498 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001499 input_set_focus_widget(input, widget,
1500 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001501 }
1502}
1503
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001504static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001505input_handle_button(void *data,
1506 struct wl_input_device *input_device,
1507 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001508{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001509 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001510 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001511
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001512 if (input->focus_widget && input->grab == NULL && state)
1513 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001514
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001515 widget = input->focus_widget;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001516 if (widget && widget->button_handler)
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001517 (*input->grab->button_handler)(widget,
1518 input, time,
1519 button, state,
1520 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001521
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001522 if (input->grab && input->grab_button == button && !state)
1523 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001524}
1525
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001526static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001527input_handle_key(void *data, struct wl_input_device *input_device,
1528 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001529{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001530 struct input *input = data;
1531 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001532 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001533 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001534
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001535 code = key + d->xkb->min_key_code;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001536 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001537 return;
1538
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001539 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001540 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001541 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1542 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001543
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001544 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1545
1546 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001547 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001548 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001549 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001550
1551 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001552 (*window->key_handler)(window, input, time, key, sym, state,
1553 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001554}
1555
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001556static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001557input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001558{
1559 struct window *window = input->pointer_focus;
1560
1561 if (!window)
1562 return;
1563
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001564 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001565
Pekka Paalanene1207c72011-12-16 12:02:09 +02001566 input->pointer_focus = NULL;
1567 input->current_pointer_image = POINTER_UNSET;
1568}
1569
1570static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001571input_handle_pointer_focus(void *data,
1572 struct wl_input_device *input_device,
1573 uint32_t time, struct wl_surface *surface,
1574 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001575{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001576 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001577 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001578 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001579
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001580 window = input->pointer_focus;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001581 if (window && window->surface != surface)
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001582 input_remove_pointer_focus(input, time);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001583
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001584 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001585 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001586 window = input->pointer_focus;
1587
Kristian Høgsberg59826582011-01-20 11:56:57 -05001588 input->x = x;
1589 input->y = y;
1590 input->sx = sx;
1591 input->sy = sy;
1592
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001593 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001594 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001595 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001596}
1597
1598static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001599input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001600{
1601 struct window *window = input->keyboard_focus;
1602
1603 if (!window)
1604 return;
1605
1606 window->keyboard_device = NULL;
1607 if (window->keyboard_focus_handler)
1608 (*window->keyboard_focus_handler)(window, NULL,
1609 window->user_data);
1610
1611 input->keyboard_focus = NULL;
1612}
1613
1614static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001615input_handle_keyboard_focus(void *data,
1616 struct wl_input_device *input_device,
1617 uint32_t time,
1618 struct wl_surface *surface,
1619 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001620{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001621 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001622 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001623 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001624 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001625
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001626 input_remove_keyboard_focus(input);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001627
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001628 if (surface)
1629 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001630
1631 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001632 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001633 for (k = keys->data; k < end; k++)
1634 input->modifiers |= d->xkb->map->modmap[*k];
1635
1636 window = input->keyboard_focus;
1637 if (window) {
1638 window->keyboard_device = input;
1639 if (window->keyboard_focus_handler)
1640 (*window->keyboard_focus_handler)(window,
1641 window->keyboard_device,
1642 window->user_data);
1643 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001644}
1645
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001646static void
1647input_handle_touch_down(void *data,
1648 struct wl_input_device *wl_input_device,
1649 uint32_t time, struct wl_surface *surface,
1650 int32_t id, int32_t x, int32_t y)
1651{
1652}
1653
1654static void
1655input_handle_touch_up(void *data,
1656 struct wl_input_device *wl_input_device,
1657 uint32_t time, int32_t id)
1658{
1659}
1660
1661static void
1662input_handle_touch_motion(void *data,
1663 struct wl_input_device *wl_input_device,
1664 uint32_t time, int32_t id, int32_t x, int32_t y)
1665{
1666}
1667
1668static void
1669input_handle_touch_frame(void *data,
1670 struct wl_input_device *wl_input_device)
1671{
1672}
1673
1674static void
1675input_handle_touch_cancel(void *data,
1676 struct wl_input_device *wl_input_device)
1677{
1678}
1679
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001680static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001681 input_handle_motion,
1682 input_handle_button,
1683 input_handle_key,
1684 input_handle_pointer_focus,
1685 input_handle_keyboard_focus,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001686 input_handle_touch_down,
1687 input_handle_touch_up,
1688 input_handle_touch_motion,
1689 input_handle_touch_frame,
1690 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001691};
1692
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001693void
1694input_get_position(struct input *input, int32_t *x, int32_t *y)
1695{
1696 *x = input->sx;
1697 *y = input->sy;
1698}
1699
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001700struct wl_input_device *
1701input_get_input_device(struct input *input)
1702{
1703 return input->input_device;
1704}
1705
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001706uint32_t
1707input_get_modifiers(struct input *input)
1708{
1709 return input->modifiers;
1710}
1711
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001712struct widget *
1713input_get_focus_widget(struct input *input)
1714{
1715 return input->focus_widget;
1716}
1717
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001718struct data_offer {
1719 struct wl_data_offer *offer;
1720 struct input *input;
1721 struct wl_array types;
1722 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001723
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001724 struct task io_task;
1725 int fd;
1726 data_func_t func;
1727 int32_t x, y;
1728 void *user_data;
1729};
1730
1731static void
1732data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1733{
1734 struct data_offer *offer = data;
1735 char **p;
1736
1737 p = wl_array_add(&offer->types, sizeof *p);
1738 *p = strdup(type);
1739}
1740
1741static const struct wl_data_offer_listener data_offer_listener = {
1742 data_offer_offer,
1743};
1744
1745static void
1746data_offer_destroy(struct data_offer *offer)
1747{
1748 char **p;
1749
1750 offer->refcount--;
1751 if (offer->refcount == 0) {
1752 wl_data_offer_destroy(offer->offer);
1753 for (p = offer->types.data; *p; p++)
1754 free(*p);
1755 wl_array_release(&offer->types);
1756 free(offer);
1757 }
1758}
1759
1760static void
1761data_device_data_offer(void *data,
1762 struct wl_data_device *data_device, uint32_t id)
1763{
1764 struct data_offer *offer;
1765
1766 offer = malloc(sizeof *offer);
1767
1768 wl_array_init(&offer->types);
1769 offer->refcount = 1;
1770 offer->input = data;
1771
1772 /* FIXME: Generate typesafe wrappers for this */
1773 offer->offer = (struct wl_data_offer *)
1774 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1775 id, &wl_data_offer_interface);
1776
1777 wl_data_offer_add_listener(offer->offer,
1778 &data_offer_listener, offer);
1779}
1780
1781static void
1782data_device_enter(void *data, struct wl_data_device *data_device,
1783 uint32_t time, struct wl_surface *surface,
1784 int32_t x, int32_t y, struct wl_data_offer *offer)
1785{
1786 struct input *input = data;
1787 struct window *window;
1788 char **p;
1789
1790 input->drag_offer = wl_data_offer_get_user_data(offer);
1791 window = wl_surface_get_user_data(surface);
1792 input->pointer_focus = window;
1793
1794 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1795 *p = NULL;
1796
1797 window = input->pointer_focus;
1798 if (window->data_handler)
1799 window->data_handler(window, input, time, x, y,
1800 input->drag_offer->types.data,
1801 window->user_data);
1802}
1803
1804static void
1805data_device_leave(void *data, struct wl_data_device *data_device)
1806{
1807 struct input *input = data;
1808
1809 data_offer_destroy(input->drag_offer);
1810 input->drag_offer = NULL;
1811}
1812
1813static void
1814data_device_motion(void *data, struct wl_data_device *data_device,
1815 uint32_t time, int32_t x, int32_t y)
1816{
1817 struct input *input = data;
1818 struct window *window = input->pointer_focus;
1819
1820 input->sx = x;
1821 input->sy = y;
1822
1823 if (window->data_handler)
1824 window->data_handler(window, input, time, x, y,
1825 input->drag_offer->types.data,
1826 window->user_data);
1827}
1828
1829static void
1830data_device_drop(void *data, struct wl_data_device *data_device)
1831{
1832 struct input *input = data;
1833 struct window *window = input->pointer_focus;
1834
1835 if (window->drop_handler)
1836 window->drop_handler(window, input,
1837 input->sx, input->sy, window->user_data);
1838}
1839
1840static void
1841data_device_selection(void *data,
1842 struct wl_data_device *wl_data_device,
1843 struct wl_data_offer *offer)
1844{
1845 struct input *input = data;
1846 char **p;
1847
1848 if (input->selection_offer)
1849 data_offer_destroy(input->selection_offer);
1850
1851 input->selection_offer = wl_data_offer_get_user_data(offer);
1852 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1853 *p = NULL;
1854}
1855
1856static const struct wl_data_device_listener data_device_listener = {
1857 data_device_data_offer,
1858 data_device_enter,
1859 data_device_leave,
1860 data_device_motion,
1861 data_device_drop,
1862 data_device_selection
1863};
1864
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001865void
1866input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1867{
1868 struct display *display = input->display;
1869 struct wl_buffer *buffer;
1870 cairo_surface_t *surface;
1871
1872 if (pointer == input->current_pointer_image)
1873 return;
1874
1875 input->current_pointer_image = pointer;
1876 surface = display->pointer_surfaces[pointer];
1877
1878 if (!surface)
1879 return;
1880
1881 buffer = display_get_buffer_for_surface(display, surface);
1882 wl_input_device_attach(input->input_device, time, buffer,
1883 pointer_images[pointer].hotspot_x,
1884 pointer_images[pointer].hotspot_y);
1885}
1886
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001887struct wl_data_device *
1888input_get_data_device(struct input *input)
1889{
1890 return input->data_device;
1891}
1892
1893void
1894input_set_selection(struct input *input,
1895 struct wl_data_source *source, uint32_t time)
1896{
1897 wl_data_device_set_selection(input->data_device, source, time);
1898}
1899
1900void
1901input_accept(struct input *input, uint32_t time, const char *type)
1902{
1903 wl_data_offer_accept(input->drag_offer->offer, time, type);
1904}
1905
1906static void
1907offer_io_func(struct task *task, uint32_t events)
1908{
1909 struct data_offer *offer =
1910 container_of(task, struct data_offer, io_task);
1911 unsigned int len;
1912 char buffer[4096];
1913
1914 len = read(offer->fd, buffer, sizeof buffer);
1915 offer->func(buffer, len,
1916 offer->x, offer->y, offer->user_data);
1917
1918 if (len == 0) {
1919 close(offer->fd);
1920 data_offer_destroy(offer);
1921 }
1922}
1923
1924static void
1925data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1926 data_func_t func, void *user_data)
1927{
1928 int p[2];
1929
1930 pipe2(p, O_CLOEXEC);
1931 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1932 close(p[1]);
1933
1934 offer->io_task.run = offer_io_func;
1935 offer->fd = p[0];
1936 offer->func = func;
1937 offer->refcount++;
1938 offer->user_data = user_data;
1939
1940 display_watch_fd(offer->input->display,
1941 offer->fd, EPOLLIN, &offer->io_task);
1942}
1943
1944void
1945input_receive_drag_data(struct input *input, const char *mime_type,
1946 data_func_t func, void *data)
1947{
1948 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1949 input->drag_offer->x = input->sx;
1950 input->drag_offer->y = input->sy;
1951}
1952
1953int
1954input_receive_selection_data(struct input *input, const char *mime_type,
1955 data_func_t func, void *data)
1956{
1957 char **p;
1958
1959 if (input->selection_offer == NULL)
1960 return -1;
1961
1962 for (p = input->selection_offer->types.data; *p; p++)
1963 if (strcmp(mime_type, *p) == 0)
1964 break;
1965
1966 if (*p == NULL)
1967 return -1;
1968
1969 data_offer_receive_data(input->selection_offer,
1970 mime_type, func, data);
1971 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001972}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001973
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05001974int
1975input_receive_selection_data_to_fd(struct input *input,
1976 const char *mime_type, int fd)
1977{
1978 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1979
1980 return 0;
1981}
1982
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001983void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001984window_move(struct window *window, struct input *input, uint32_t time)
1985{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001986 if (!window->shell_surface)
1987 return;
1988
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001989 wl_shell_surface_move(window->shell_surface,
1990 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001991}
1992
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001993static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001994window_resize(struct window *window, int32_t width, int32_t height)
1995{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001996 struct rectangle allocation;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001997 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001998
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001999 allocation.x = 0;
2000 allocation.y = 0;
2001 allocation.width = width;
2002 allocation.height = height;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002003
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002004 widget = window->widget;
2005 widget_set_allocation(widget, allocation.x, allocation.y,
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002006 allocation.width, allocation.height);
2007
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002008 if (widget->resize_handler)
2009 widget->resize_handler(widget,
2010 allocation.width,
2011 allocation.height,
2012 widget->user_data);
2013
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002014 window->allocation = widget->allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002015
2016 window_schedule_redraw(window);
2017}
2018
2019static void
2020idle_resize(struct task *task, uint32_t events)
2021{
2022 struct window *window =
2023 container_of(task, struct window, resize_task);
2024
2025 window_resize(window,
2026 window->allocation.width, window->allocation.height);
2027 window->resize_scheduled = 0;
2028}
2029
2030void
2031window_schedule_resize(struct window *window, int width, int height)
2032{
2033 if (!window->resize_scheduled) {
2034 window->allocation.width = width;
2035 window->allocation.height = height;
2036 window->resize_task.run = idle_resize;
2037 display_defer(window->display, &window->resize_task);
2038 window->resize_scheduled = 1;
2039 }
2040}
2041
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002042void
2043widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2044{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002045 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002046}
2047
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002048static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002049handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002050 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002051 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002052{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002053 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002054
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002055 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002056 return;
2057
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002058 window->resize_edges = edges;
2059
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002060 window_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002061}
2062
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063static void
2064handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2065{
2066 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002067 struct menu *menu = window->widget->user_data;
2068
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002069 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002070 * device. Or just use wl_callback. And this really needs to
2071 * be a window vfunc that the menu can set. And we need the
2072 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002073
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002074 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002075 input_ungrab(menu->input, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002076 window_destroy(window);
2077}
2078
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002079static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002080 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002082};
2083
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002084void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002085window_get_allocation(struct window *window,
2086 struct rectangle *allocation)
2087{
2088 *allocation = window->allocation;
2089}
2090
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002091static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002092widget_redraw(struct widget *widget)
2093{
2094 struct widget *child;
2095
2096 if (widget->redraw_handler)
2097 widget->redraw_handler(widget, widget->user_data);
2098 wl_list_for_each(child, &widget->child_list, link)
2099 widget_redraw(child);
2100}
2101
2102static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002103idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002104{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002105 struct window *window =
2106 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002107
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002108 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002109 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002110 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002111 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002112}
2113
2114void
2115window_schedule_redraw(struct window *window)
2116{
2117 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002118 window->redraw_task.run = idle_redraw;
2119 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002120 window->redraw_scheduled = 1;
2121 }
2122}
2123
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002124void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002125window_set_custom(struct window *window)
2126{
2127 window->type = TYPE_CUSTOM;
2128}
2129
2130void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002131window_set_fullscreen(struct window *window, int fullscreen)
2132{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002133 int32_t width, height;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002134 struct output *output;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002135
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002136 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002137 return;
2138
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002139 if (fullscreen) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002140 output = display_get_output(window->display);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002141 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002142 window->saved_allocation = window->allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002143 width = output->allocation.width;
2144 height = output->allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002145 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002146 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147 width = window->saved_allocation.width;
2148 height = window->saved_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002149 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002150
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002151 window_schedule_resize(window, width, height);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002152}
2153
2154void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002155window_set_user_data(struct window *window, void *data)
2156{
2157 window->user_data = data;
2158}
2159
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002160void *
2161window_get_user_data(struct window *window)
2162{
2163 return window->user_data;
2164}
2165
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002166void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002167window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002168 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002169{
2170 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002171}
2172
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002173void
2174window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002175 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002176{
2177 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002178}
2179
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002180void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181window_set_data_handler(struct window *window, window_data_handler_t handler)
2182{
2183 window->data_handler = handler;
2184}
2185
2186void
2187window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2188{
2189 window->drop_handler = handler;
2190}
2191
2192void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002193window_set_close_handler(struct window *window,
2194 window_close_handler_t handler)
2195{
2196 window->close_handler = handler;
2197}
2198
2199void
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002200window_set_transparent(struct window *window, int transparent)
2201{
2202 window->transparent = transparent;
2203}
2204
2205void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002206window_set_title(struct window *window, const char *title)
2207{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002208 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002209 window->title = strdup(title);
2210}
2211
2212const char *
2213window_get_title(struct window *window)
2214{
2215 return window->title;
2216}
2217
2218void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002219display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2220 int32_t x, int32_t y, int32_t width, int32_t height)
2221{
2222 struct wl_buffer *buffer;
2223
2224 buffer = display_get_buffer_for_surface(display, cairo_surface);
2225
2226 wl_buffer_damage(buffer, x, y, width, height);
2227}
2228
2229void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002230window_damage(struct window *window, int32_t x, int32_t y,
2231 int32_t width, int32_t height)
2232{
2233 wl_surface_damage(window->surface, x, y, width, height);
2234}
2235
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002236static struct window *
2237window_create_internal(struct display *display, struct window *parent,
2238 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002239{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002240 struct window *window;
2241
2242 window = malloc(sizeof *window);
2243 if (window == NULL)
2244 return NULL;
2245
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002246 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002247 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002248 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002249 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002250 if (display->shell) {
2251 window->shell_surface =
2252 wl_shell_get_shell_surface(display->shell,
2253 window->surface);
2254 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002255 window->allocation.x = 0;
2256 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002257 window->allocation.width = width;
2258 window->allocation.height = height;
2259 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002260 window->transparent = 1;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002261
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002262 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002263#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002264 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2265 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002266#else
2267 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2268#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002269 else
2270 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002271
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002272 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002273 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002274
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002275 if (window->shell_surface) {
2276 wl_shell_surface_set_user_data(window->shell_surface, window);
2277 wl_shell_surface_add_listener(window->shell_surface,
2278 &shell_surface_listener, window);
2279 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002280
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002281 return window;
2282}
2283
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002284struct window *
2285window_create(struct display *display, int32_t width, int32_t height)
2286{
2287 struct window *window;
2288
2289 window = window_create_internal(display, NULL, width, height);
2290 if (!window)
2291 return NULL;
2292
2293 return window;
2294}
2295
2296struct window *
2297window_create_transient(struct display *display, struct window *parent,
2298 int32_t x, int32_t y, int32_t width, int32_t height)
2299{
2300 struct window *window;
2301
2302 window = window_create_internal(parent->display,
2303 parent, width, height);
2304 if (!window)
2305 return NULL;
2306
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002307 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002308 window->x = x;
2309 window->y = y;
2310
2311 return window;
2312}
2313
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002314static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002315menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002316{
2317 int next;
2318
2319 next = (sy - 8) / 20;
2320 if (menu->current != next) {
2321 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002322 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002323 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002324}
2325
2326static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002327menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002328 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002329 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002330{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002331 struct menu *menu = data;
2332
2333 if (widget == menu->widget)
2334 menu_set_item(data, y);
2335
2336 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002337}
2338
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002339static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002340menu_enter_handler(struct widget *widget,
2341 struct input *input, uint32_t time,
2342 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002343{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002344 struct menu *menu = data;
2345
2346 if (widget == menu->widget)
2347 menu_set_item(data, y);
2348
2349 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002350}
2351
2352static void
2353menu_leave_handler(struct widget *widget, struct input *input, void *data)
2354{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002355 struct menu *menu = data;
2356
2357 if (widget == menu->widget)
2358 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002359}
2360
2361static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002362menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002363 struct input *input, uint32_t time,
2364 int button, int state, void *data)
2365
2366{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002367 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002368
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002369 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002370 /* Either relase after press-drag-release or
2371 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002372 menu->func(menu->window->parent,
2373 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002374 input_ungrab(input, time);
2375 window_destroy(menu->widget->window);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002376 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002377}
2378
2379static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002380menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002381{
2382 cairo_t *cr;
2383 const int32_t r = 3, margin = 3;
2384 struct menu *menu = data;
2385 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002386 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002387
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002388 cr = cairo_create(window->cairo_surface);
2389 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2390 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2391 cairo_paint(cr);
2392
2393 width = window->allocation.width;
2394 height = window->allocation.height;
2395 rounded_rect(cr, 0, 0, width, height, r);
2396 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2397 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2398 cairo_fill(cr);
2399
2400 for (i = 0; i < menu->count; i++) {
2401 if (i == menu->current) {
2402 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2403 cairo_rectangle(cr, margin, i * 20 + margin,
2404 width - 2 * margin, 20);
2405 cairo_fill(cr);
2406 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2407 cairo_move_to(cr, 10, i * 20 + 16);
2408 cairo_show_text(cr, menu->entries[i]);
2409 } else {
2410 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2411 cairo_move_to(cr, 10, i * 20 + 16);
2412 cairo_show_text(cr, menu->entries[i]);
2413 }
2414 }
2415
2416 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002417}
2418
2419struct window *
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002420window_create_menu(struct display *display,
2421 struct input *input, uint32_t time, struct window *parent,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002422 int32_t x, int32_t y,
2423 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002424{
2425 struct window *window;
2426 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002427 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002428
2429 menu = malloc(sizeof *menu);
2430 if (!menu)
2431 return NULL;
2432
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002433 window = window_create_internal(parent->display, parent,
2434 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002435 if (!window)
2436 return NULL;
2437
2438 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002439 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002440 menu->entries = entries;
2441 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002442 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002443 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002444 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002445 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002446 window->type = TYPE_MENU;
2447 window->x = x;
2448 window->y = y;
2449
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002450 wl_shell_surface_set_popup(window->shell_surface,
2451 input->input_device, time,
2452 window->parent->shell_surface,
2453 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002454
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002455 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002456 widget_set_enter_handler(menu->widget, menu_enter_handler);
2457 widget_set_leave_handler(menu->widget, menu_leave_handler);
2458 widget_set_motion_handler(menu->widget, menu_motion_handler);
2459 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002460
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002461 input_grab(input, menu->widget, 0);
2462
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002463 return window;
2464}
2465
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002466void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002467window_set_buffer_type(struct window *window, enum window_buffer_type type)
2468{
2469 window->buffer_type = type;
2470}
2471
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002472
2473static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002474display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002475 struct wl_output *wl_output,
2476 int x, int y,
2477 int physical_width,
2478 int physical_height,
2479 int subpixel,
2480 const char *make,
2481 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002482{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002483 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002484
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002485 output->allocation.x = x;
2486 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002487}
2488
2489static void
2490display_handle_mode(void *data,
2491 struct wl_output *wl_output,
2492 uint32_t flags,
2493 int width,
2494 int height,
2495 int refresh)
2496{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002497 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002498 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002499
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002500 if (flags & WL_OUTPUT_MODE_CURRENT) {
2501 output->allocation.width = width;
2502 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002503 if (display->output_configure_handler)
2504 (*display->output_configure_handler)(
2505 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002506 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002507}
2508
2509static const struct wl_output_listener output_listener = {
2510 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002511 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002512};
2513
2514static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002515display_add_output(struct display *d, uint32_t id)
2516{
2517 struct output *output;
2518
2519 output = malloc(sizeof *output);
2520 if (output == NULL)
2521 return;
2522
2523 memset(output, 0, sizeof *output);
2524 output->display = d;
2525 output->output =
2526 wl_display_bind(d->display, id, &wl_output_interface);
2527 wl_list_insert(d->output_list.prev, &output->link);
2528
2529 wl_output_add_listener(output->output, &output_listener, output);
2530}
2531
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002532static void
2533output_destroy(struct output *output)
2534{
2535 if (output->destroy_handler)
2536 (*output->destroy_handler)(output, output->user_data);
2537
2538 wl_output_destroy(output->output);
2539 wl_list_remove(&output->link);
2540 free(output);
2541}
2542
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002543void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002544display_set_output_configure_handler(struct display *display,
2545 display_output_handler_t handler)
2546{
2547 struct output *output;
2548
2549 display->output_configure_handler = handler;
2550 if (!handler)
2551 return;
2552
2553 wl_list_for_each(output, &display->output_list, link)
2554 (*display->output_configure_handler)(output,
2555 display->user_data);
2556}
2557
2558void
2559output_set_user_data(struct output *output, void *data)
2560{
2561 output->user_data = data;
2562}
2563
2564void *
2565output_get_user_data(struct output *output)
2566{
2567 return output->user_data;
2568}
2569
2570void
2571output_set_destroy_handler(struct output *output,
2572 display_output_handler_t handler)
2573{
2574 output->destroy_handler = handler;
2575 /* FIXME: implement this, once we have way to remove outputs */
2576}
2577
2578void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002579output_get_allocation(struct output *output, struct rectangle *allocation)
2580{
2581 *allocation = output->allocation;
2582}
2583
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002584struct wl_output *
2585output_get_wl_output(struct output *output)
2586{
2587 return output->output;
2588}
2589
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002590static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002591display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002592{
2593 struct input *input;
2594
2595 input = malloc(sizeof *input);
2596 if (input == NULL)
2597 return;
2598
2599 memset(input, 0, sizeof *input);
2600 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002601 input->input_device =
2602 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002603 input->pointer_focus = NULL;
2604 input->keyboard_focus = NULL;
2605 wl_list_insert(d->input_list.prev, &input->link);
2606
2607 wl_input_device_add_listener(input->input_device,
2608 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002609 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002610
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002611 input->data_device =
2612 wl_data_device_manager_get_data_device(d->data_device_manager,
2613 input->input_device);
2614 wl_data_device_add_listener(input->data_device,
2615 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002616}
2617
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002618static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002619input_destroy(struct input *input)
2620{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002621 input_remove_keyboard_focus(input);
2622 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002623
2624 if (input->drag_offer)
2625 data_offer_destroy(input->drag_offer);
2626
2627 if (input->selection_offer)
2628 data_offer_destroy(input->selection_offer);
2629
2630 wl_data_device_destroy(input->data_device);
2631 wl_list_remove(&input->link);
2632 wl_input_device_destroy(input->input_device);
2633 free(input);
2634}
2635
2636static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002637display_handle_global(struct wl_display *display, uint32_t id,
2638 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002639{
2640 struct display *d = data;
2641
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002642 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002643 d->compositor =
2644 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002645 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002646 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002647 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002648 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002649 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002650 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002651 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002652 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002653 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2654 d->data_device_manager =
2655 wl_display_bind(display, id,
2656 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002657 }
2658}
2659
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002660static void
2661display_render_frame(struct display *d)
2662{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002663 int radius = 8;
2664 cairo_t *cr;
2665
2666 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2667 cr = cairo_create(d->shadow);
2668 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2669 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2670 rounded_rect(cr, 16, 16, 112, 112, radius);
2671 cairo_fill(cr);
2672 cairo_destroy(cr);
2673 blur_surface(d->shadow, 64);
2674
2675 d->active_frame =
2676 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2677 cr = cairo_create(d->active_frame);
2678 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2679 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2680 rounded_rect(cr, 16, 16, 112, 112, radius);
2681 cairo_fill(cr);
2682 cairo_destroy(cr);
2683
2684 d->inactive_frame =
2685 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2686 cr = cairo_create(d->inactive_frame);
2687 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2688 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2689 rounded_rect(cr, 16, 16, 112, 112, radius);
2690 cairo_fill(cr);
2691 cairo_destroy(cr);
2692}
2693
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002694static void
2695init_xkb(struct display *d)
2696{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002697 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002698
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002699 names.rules = "evdev";
2700 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002701 names.layout = option_xkb_layout;
2702 names.variant = option_xkb_variant;
2703 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002704
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002705 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002706 if (!d->xkb) {
2707 fprintf(stderr, "Failed to compile keymap\n");
2708 exit(1);
2709 }
2710}
2711
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002712static void
2713fini_xkb(struct display *display)
2714{
2715 xkb_free_keymap(display->xkb);
2716}
2717
Yuval Fledel45568f62010-12-06 09:18:12 -05002718static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002719init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002720{
2721 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002722 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002723
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002724 static const EGLint argb_cfg_attribs[] = {
2725 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002726 EGL_RED_SIZE, 1,
2727 EGL_GREEN_SIZE, 1,
2728 EGL_BLUE_SIZE, 1,
2729 EGL_ALPHA_SIZE, 1,
2730 EGL_DEPTH_SIZE, 1,
2731 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2732 EGL_NONE
2733 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002734
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002735 static const EGLint rgb_cfg_attribs[] = {
2736 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2737 EGL_RED_SIZE, 1,
2738 EGL_GREEN_SIZE, 1,
2739 EGL_BLUE_SIZE, 1,
2740 EGL_ALPHA_SIZE, 0,
2741 EGL_DEPTH_SIZE, 1,
2742 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2743 EGL_NONE
2744 };
2745
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002746#ifdef USE_CAIRO_GLESV2
2747 static const EGLint context_attribs[] = {
2748 EGL_CONTEXT_CLIENT_VERSION, 2,
2749 EGL_NONE
2750 };
2751 EGLint api = EGL_OPENGL_ES_API;
2752#else
2753 EGLint *context_attribs = NULL;
2754 EGLint api = EGL_OPENGL_API;
2755#endif
2756
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002757 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002758 if (!eglInitialize(d->dpy, &major, &minor)) {
2759 fprintf(stderr, "failed to initialize display\n");
2760 return -1;
2761 }
2762
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002763 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002764 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2765 return -1;
2766 }
2767
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002768 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2769 &d->argb_config, 1, &n) || n != 1) {
2770 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002771 return -1;
2772 }
2773
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002774 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2775 &d->rgb_config, 1, &n) || n != 1) {
2776 fprintf(stderr, "failed to choose rgb config\n");
2777 return -1;
2778 }
2779
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002780 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2781 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002782 if (d->rgb_ctx == NULL) {
2783 fprintf(stderr, "failed to create context\n");
2784 return -1;
2785 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002786 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002787 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002788 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002789 fprintf(stderr, "failed to create context\n");
2790 return -1;
2791 }
2792
Benjamin Franzke0c991632011-09-27 21:57:31 +02002793 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002794 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002795 return -1;
2796 }
2797
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002798#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002799 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2800 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002801 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002802 return -1;
2803 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002804 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2805 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2806 fprintf(stderr, "failed to get cairo egl argb device\n");
2807 return -1;
2808 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002809#endif
2810
2811 return 0;
2812}
2813
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002814static void
2815fini_egl(struct display *display)
2816{
2817#ifdef HAVE_CAIRO_EGL
2818 cairo_device_destroy(display->argb_device);
2819 cairo_device_destroy(display->rgb_device);
2820#endif
2821
2822 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2823 EGL_NO_CONTEXT);
2824
2825 eglTerminate(display->dpy);
2826 eglReleaseThread();
2827}
2828
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002829static int
2830event_mask_update(uint32_t mask, void *data)
2831{
2832 struct display *d = data;
2833
2834 d->mask = mask;
2835
2836 return 0;
2837}
2838
2839static void
2840handle_display_data(struct task *task, uint32_t events)
2841{
2842 struct display *display =
2843 container_of(task, struct display, display_task);
2844
2845 wl_display_iterate(display->display, display->mask);
2846}
2847
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002848struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002849display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002850{
2851 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002852 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002853 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002854 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002855
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002856 g_type_init();
2857
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002858 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002859 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002860 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002861
2862 xkb_option_group = g_option_group_new("xkb",
2863 "Keyboard options",
2864 "Show all XKB options",
2865 NULL, NULL);
2866 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2867 g_option_context_add_group (context, xkb_option_group);
2868
2869 if (!g_option_context_parse(context, argc, argv, &error)) {
2870 fprintf(stderr, "option parsing failed: %s\n", error->message);
2871 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002872 }
2873
Tim Wiederhake748f6722011-01-23 23:25:25 +01002874 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002875
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002876 d = malloc(sizeof *d);
2877 if (d == NULL)
2878 return NULL;
2879
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002880 memset(d, 0, sizeof *d);
2881
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002882 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002883 if (d->display == NULL) {
2884 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002885 return NULL;
2886 }
2887
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002888 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2889 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2890 d->display_task.run = handle_display_data;
2891 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2892
2893 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002894 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002895 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002896
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002897 /* Set up listener so we'll catch all events. */
2898 wl_display_add_global_listener(d->display,
2899 display_handle_global, d);
2900
2901 /* Process connection events. */
2902 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002903 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002904 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002905
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002906 d->image_target_texture_2d =
2907 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2908 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2909 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2910
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002911 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002912
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002913 display_render_frame(d);
2914
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002915 wl_list_init(&d->window_list);
2916
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002917 init_xkb(d);
2918
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002919 return d;
2920}
2921
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002922static void
2923display_destroy_outputs(struct display *display)
2924{
2925 struct output *tmp;
2926 struct output *output;
2927
2928 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2929 output_destroy(output);
2930}
2931
Pekka Paalanene1207c72011-12-16 12:02:09 +02002932static void
2933display_destroy_inputs(struct display *display)
2934{
2935 struct input *tmp;
2936 struct input *input;
2937
2938 wl_list_for_each_safe(input, tmp, &display->input_list, link)
2939 input_destroy(input);
2940}
2941
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002942void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002943display_destroy(struct display *display)
2944{
Pekka Paalanenc2052982011-12-16 11:41:32 +02002945 if (!wl_list_empty(&display->window_list))
2946 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2947
2948 if (!wl_list_empty(&display->deferred_list))
2949 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2950
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002951 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002952 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002953
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002954 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02002955
2956 cairo_surface_destroy(display->active_frame);
2957 cairo_surface_destroy(display->inactive_frame);
2958 cairo_surface_destroy(display->shadow);
2959 destroy_pointer_surfaces(display);
2960
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002961 fini_egl(display);
2962
Pekka Paalanenc2052982011-12-16 11:41:32 +02002963 if (display->shell)
2964 wl_shell_destroy(display->shell);
2965
2966 if (display->shm)
2967 wl_shm_destroy(display->shm);
2968
2969 if (display->data_device_manager)
2970 wl_data_device_manager_destroy(display->data_device_manager);
2971
2972 wl_compositor_destroy(display->compositor);
2973
2974 close(display->epoll_fd);
2975
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002976 wl_display_flush(display->display);
2977 wl_display_destroy(display->display);
2978 free(display);
2979}
2980
2981void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002982display_set_user_data(struct display *display, void *data)
2983{
2984 display->user_data = data;
2985}
2986
2987void *
2988display_get_user_data(struct display *display)
2989{
2990 return display->user_data;
2991}
2992
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002993struct wl_display *
2994display_get_display(struct display *display)
2995{
2996 return display->display;
2997}
2998
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002999struct output *
3000display_get_output(struct display *display)
3001{
3002 return container_of(display->output_list.next, struct output, link);
3003}
3004
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003005struct wl_compositor *
3006display_get_compositor(struct display *display)
3007{
3008 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003009}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003010
3011EGLDisplay
3012display_get_egl_display(struct display *d)
3013{
3014 return d->dpy;
3015}
3016
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003017struct wl_data_source *
3018display_create_data_source(struct display *display)
3019{
3020 return wl_data_device_manager_create_data_source(display->data_device_manager);
3021}
3022
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003023EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003024display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003025{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003026 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003027}
3028
Benjamin Franzke0c991632011-09-27 21:57:31 +02003029EGLConfig
3030display_get_argb_egl_config(struct display *d)
3031{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003032 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003033}
3034
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003035struct wl_shell *
3036display_get_shell(struct display *display)
3037{
3038 return display->shell;
3039}
3040
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003041int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003042display_acquire_window_surface(struct display *display,
3043 struct window *window,
3044 EGLContext ctx)
3045{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003046#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003047 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003048 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003049
3050 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003051 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003052 device = cairo_surface_get_device(window->cairo_surface);
3053 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003054 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003055
Benjamin Franzke0c991632011-09-27 21:57:31 +02003056 if (!ctx) {
3057 if (device == display->rgb_device)
3058 ctx = display->rgb_ctx;
3059 else if (device == display->argb_device)
3060 ctx = display->argb_ctx;
3061 else
3062 assert(0);
3063 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003064
3065 data = cairo_surface_get_user_data(window->cairo_surface,
3066 &surface_data_key);
3067
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003068 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003069 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003070 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3071 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003072
3073 return 0;
3074#else
3075 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003076#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003077}
3078
3079void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003080display_release_window_surface(struct display *display,
3081 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003082{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003083#ifdef HAVE_CAIRO_EGL
3084 cairo_device_t *device;
3085
3086 device = cairo_surface_get_device(window->cairo_surface);
3087 if (!device)
3088 return;
3089
3090 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003091 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003092 cairo_device_release(device);
3093#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003094}
3095
3096void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003097display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003098{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003099 wl_list_insert(&display->deferred_list, &task->link);
3100}
3101
3102void
3103display_watch_fd(struct display *display,
3104 int fd, uint32_t events, struct task *task)
3105{
3106 struct epoll_event ep;
3107
3108 ep.events = events;
3109 ep.data.ptr = task;
3110 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3111}
3112
3113void
3114display_run(struct display *display)
3115{
3116 struct task *task;
3117 struct epoll_event ep[16];
3118 int i, count;
3119
Pekka Paalanen826d7952011-12-15 10:14:07 +02003120 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003121 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003122 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003123
3124 while (!wl_list_empty(&display->deferred_list)) {
3125 task = container_of(display->deferred_list.next,
3126 struct task, link);
3127 wl_list_remove(&task->link);
3128 task->run(task, 0);
3129 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003130
3131 if (!display->running)
3132 break;
3133
3134 wl_display_flush(display->display);
3135
3136 count = epoll_wait(display->epoll_fd,
3137 ep, ARRAY_LENGTH(ep), -1);
3138 for (i = 0; i < count; i++) {
3139 task = ep[i].data.ptr;
3140 task->run(task, ep[i].events);
3141 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003142 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003143}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003144
3145void
3146display_exit(struct display *display)
3147{
3148 display->running = 0;
3149}