blob: 9f5b837c2b1890dfced35e1dd9f52050d8f94a9a [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øgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040040
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040041#include <pixman.h>
42
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberg8def2642011-01-14 17:41:33 -050054#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040055#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050057
Daniel Stone9d4f0302012-02-15 16:33:21 +000058#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050059#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040060
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020062#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040063#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050066
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050067struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050068 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040070 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040071 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040072 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040073 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050074 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020075 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020076 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040077
78 int display_fd;
79 uint32_t mask;
80 struct task display_task;
81
82 int epoll_fd;
83 struct wl_list deferred_list;
84
Pekka Paalanen826d7952011-12-15 10:14:07 +020085 int running;
86
Kristian Høgsberg478d9262010-06-08 20:34:11 -040087 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050089 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040090 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050091 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040092 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040093 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040094
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050095 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
96 PFNEGLCREATEIMAGEKHRPROC create_image;
97 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020098
99 display_output_handler_t output_configure_handler;
100
101 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500102};
103
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400106 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500107 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500109 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110 TYPE_CUSTOM
111};
112
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500113struct window {
114 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500115 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500116 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200117 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500118 struct wl_region *input_region;
119 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500120 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500121 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500122 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500123 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400124 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400125 int redraw_scheduled;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400126 int redraw_needed;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400127 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400128 int resize_needed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400130 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400131 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400132 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500133
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500134 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500135
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400136 struct wl_shm_pool *pool;
137 size_t pool_size;
138 void *pool_data;
139
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500140 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500141 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400142 window_data_handler_t data_handler;
143 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500144 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400145
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200146 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500147 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500148
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500149 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400150 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500151};
152
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500153struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500154 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500155 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400156 struct wl_list link;
157 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500158 widget_resize_handler_t resize_handler;
159 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500160 widget_enter_handler_t enter_handler;
161 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500162 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500163 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400164 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500165 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400166};
167
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400168struct input {
169 struct display *display;
170 struct wl_input_device *input_device;
171 struct window *pointer_focus;
172 struct window *keyboard_focus;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400173 int current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400174 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200175 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400176 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400177
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500178 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500179 struct widget *grab;
180 uint32_t grab_button;
181
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400182 struct wl_data_device *data_device;
183 struct data_offer *drag_offer;
184 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400185};
186
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500187struct output {
188 struct display *display;
189 struct wl_output *output;
190 struct rectangle allocation;
191 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200192
193 display_output_handler_t destroy_handler;
194 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500195};
196
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500197struct frame {
198 struct widget *widget;
199 struct widget *child;
200 int margin;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -0400201 int width;
202 int titlebar_height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500203};
204
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500205struct menu {
206 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500207 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500208 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500209 const char **entries;
210 uint32_t time;
211 int current;
212 int count;
213 menu_func_t func;
214};
215
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400216enum {
217 POINTER_DEFAULT = 100,
218 POINTER_UNSET
219};
220
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500221enum window_location {
222 WINDOW_INTERIOR = 0,
223 WINDOW_RESIZING_TOP = 1,
224 WINDOW_RESIZING_BOTTOM = 2,
225 WINDOW_RESIZING_LEFT = 4,
226 WINDOW_RESIZING_TOP_LEFT = 5,
227 WINDOW_RESIZING_BOTTOM_LEFT = 6,
228 WINDOW_RESIZING_RIGHT = 8,
229 WINDOW_RESIZING_TOP_RIGHT = 9,
230 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
231 WINDOW_RESIZING_MASK = 15,
232 WINDOW_EXTERIOR = 16,
233 WINDOW_TITLEBAR = 17,
234 WINDOW_CLIENT_AREA = 18,
235};
236
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400237const char *option_xkb_layout = "us";
238const char *option_xkb_variant = "";
239const char *option_xkb_options = "";
240
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400241static const struct weston_option xkb_options[] = {
242 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
243 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
244 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400245};
246
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400247static const cairo_user_data_key_t surface_data_key;
248struct surface_data {
249 struct wl_buffer *buffer;
250};
251
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500252#define MULT(_d,c,a,t) \
253 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
254
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500255#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400256
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100257struct egl_window_surface_data {
258 struct display *display;
259 struct wl_surface *surface;
260 struct wl_egl_window *window;
261 EGLSurface surf;
262};
263
264static void
265egl_window_surface_data_destroy(void *p)
266{
267 struct egl_window_surface_data *data = p;
268 struct display *d = data->display;
269
270 eglDestroySurface(d->dpy, data->surf);
271 wl_egl_window_destroy(data->window);
272 data->surface = NULL;
273
274 free(p);
275}
276
277static cairo_surface_t *
278display_create_egl_window_surface(struct display *display,
279 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400280 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100281 struct rectangle *rectangle)
282{
283 cairo_surface_t *cairo_surface;
284 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400285 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200286 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100287
288 data = malloc(sizeof *data);
289 if (data == NULL)
290 return NULL;
291
292 data->display = display;
293 data->surface = surface;
294
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500295 config = display->argb_config;
296 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400297
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400298 data->window = wl_egl_window_create(surface,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100299 rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400300 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100301
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400302 data->surf = eglCreateWindowSurface(display->dpy, config,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500303 data->window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100304
Benjamin Franzke0c991632011-09-27 21:57:31 +0200305 cairo_surface = cairo_gl_surface_create_for_egl(device,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100306 data->surf,
307 rectangle->width,
308 rectangle->height);
309
310 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
311 data, egl_window_surface_data_destroy);
312
313 return cairo_surface;
314}
315
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400317 struct surface_data data;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200318 cairo_device_t *device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400319 EGLImageKHR image;
320 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800321 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400323};
324
325static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500326egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400327{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500328 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800329 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400330
Benjamin Franzke0c991632011-09-27 21:57:31 +0200331 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400332 glDeleteTextures(1, &data->texture);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200333 cairo_device_release(data->device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800334
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500335 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400336 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500337 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500338 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400339}
340
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500341EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342display_get_image_for_egl_image_surface(struct display *display,
343 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500344{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500345 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500346
347 data = cairo_surface_get_user_data (surface, &surface_data_key);
348
349 return data->image;
350}
351
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500352static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500353display_create_egl_image_surface(struct display *display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400354 uint32_t flags,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500355 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400356{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500357 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400358 EGLDisplay dpy = display->dpy;
359 cairo_surface_t *surface;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200360 cairo_content_t content;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400361
362 data = malloc(sizeof *data);
363 if (data == NULL)
364 return NULL;
365
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800366 data->display = display;
367
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400368 data->pixmap = wl_egl_pixmap_create(rectangle->width,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400369 rectangle->height, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500370 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000371 free(data);
372 return NULL;
373 }
374
Kristian Høgsberg067fd602012-02-29 16:15:53 -0500375 data->device = display->argb_device;
376 content = CAIRO_CONTENT_COLOR_ALPHA;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400377
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500378 data->image = display->create_image(dpy, NULL,
379 EGL_NATIVE_PIXMAP_KHR,
380 (EGLClientBuffer) data->pixmap,
381 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500382 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500383 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500384 free(data);
385 return NULL;
386 }
387
388 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400389 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500390
Benjamin Franzke0c991632011-09-27 21:57:31 +0200391 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400392 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400393 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500394 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200395 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400396
Benjamin Franzke0c991632011-09-27 21:57:31 +0200397 surface = cairo_gl_surface_create_for_texture(data->device,
398 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 data->texture,
400 rectangle->width,
401 rectangle->height);
402
403 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500404 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400405
406 return surface;
407}
408
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500409static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500410display_create_egl_image_surface_from_file(struct display *display,
411 const char *filename,
412 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400413{
414 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400415 pixman_image_t *image;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500416 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400418 image = load_image(filename);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400419
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200420 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000421 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400422 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000423 return NULL;
424 }
425
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800426 data = cairo_surface_get_user_data(surface, &surface_data_key);
427
Benjamin Franzke0c991632011-09-27 21:57:31 +0200428 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800429 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800430 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400431 GL_RGBA, GL_UNSIGNED_BYTE,
432 pixman_image_get_data(image));
Benjamin Franzke0c991632011-09-27 21:57:31 +0200433 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400434
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400435 pixman_image_unref(image);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400436
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400437 return surface;
438}
439
440#endif
441
442struct wl_buffer *
443display_get_buffer_for_surface(struct display *display,
444 cairo_surface_t *surface)
445{
446 struct surface_data *data;
447
448 data = cairo_surface_get_user_data (surface, &surface_data_key);
449
450 return data->buffer;
451}
452
453struct shm_surface_data {
454 struct surface_data data;
455 void *map;
456 size_t length;
457};
458
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500459static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400460shm_surface_data_destroy(void *p)
461{
462 struct shm_surface_data *data = p;
463
464 wl_buffer_destroy(data->data.buffer);
465 munmap(data->map, data->length);
466}
467
Kristian Høgsberg16626282012-04-03 11:21:27 -0400468static struct wl_shm_pool *
469make_shm_pool(struct display *display, int size, void **data)
470{
471 char filename[] = "/tmp/wayland-shm-XXXXXX";
472 struct wl_shm_pool *pool;
473 int fd;
474
475 fd = mkstemp(filename);
476 if (fd < 0) {
477 fprintf(stderr, "open %s failed: %m\n", filename);
478 return NULL;
479 }
480 if (ftruncate(fd, size) < 0) {
481 fprintf(stderr, "ftruncate failed: %m\n");
482 close(fd);
483 return NULL;
484 }
485
486 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
487 unlink(filename);
488
489 if (*data == MAP_FAILED) {
490 fprintf(stderr, "mmap failed: %m\n");
491 close(fd);
492 return NULL;
493 }
494
495 pool = wl_shm_create_pool(display->shm, fd, size);
496
497 close(fd);
498
499 return pool;
500}
501
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500502static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400503display_create_shm_surface(struct display *display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400504 struct rectangle *rectangle, uint32_t flags,
505 struct window *window)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506{
507 struct shm_surface_data *data;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400508 struct wl_shm_pool *pool = NULL;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400509 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400510 cairo_surface_t *surface;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400511 void *map;
Kristian Høgsberg16626282012-04-03 11:21:27 -0400512 int stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513
514 data = malloc(sizeof *data);
515 if (data == NULL)
516 return NULL;
517
518 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
519 rectangle->width);
520 data->length = stride * rectangle->height;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400521 if (window && window->pool && data->length < window->pool_size) {
522 pool = window->pool;
523 map = window->pool_data;
524 } else {
525 pool = make_shm_pool(display, data->length, &data->map);
526 map = data->map;
527 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400528
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400529 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400530 CAIRO_FORMAT_ARGB32,
531 rectangle->width,
532 rectangle->height,
533 stride);
534
535 cairo_surface_set_user_data (surface, &surface_data_key,
536 data, shm_surface_data_destroy);
537
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400538 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500539 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400540 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500541 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400542
Kristian Høgsberg16626282012-04-03 11:21:27 -0400543 data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
544 rectangle->width,
545 rectangle->height,
546 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400547
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400548 if (map == data->map)
549 wl_shm_pool_destroy(pool);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
551 return surface;
552}
553
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500554static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555display_create_shm_surface_from_file(struct display *display,
556 const char *filename,
557 struct rectangle *rect)
558{
559 cairo_surface_t *surface;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400560 pixman_image_t *image;
561 void *dest;
562 int size;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400563
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400564 image = load_image(filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400565
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400566 surface = display_create_shm_surface(display, rect, 0, NULL);
nobled7b87cb02011-02-01 18:51:47 +0000567 if (surface == NULL) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400568 pixman_image_unref(image);
nobled7b87cb02011-02-01 18:51:47 +0000569 return NULL;
570 }
571
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400572 size = pixman_image_get_stride(image) * pixman_image_get_height(image);
573 dest = cairo_image_surface_get_data(surface);
574 memcpy(dest, pixman_image_get_data(image), size);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400575
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400576 pixman_image_unref(image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400577
578 return surface;
579}
580
nobled7b87cb02011-02-01 18:51:47 +0000581static int
582check_size(struct rectangle *rect)
583{
584 if (rect->width && rect->height)
585 return 0;
586
587 fprintf(stderr, "tried to create surface of "
588 "width: %d, height: %d\n", rect->width, rect->height);
589 return -1;
590}
591
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400592cairo_surface_t *
593display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400595 struct rectangle *rectangle,
596 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400597{
nobled7b87cb02011-02-01 18:51:47 +0000598 if (check_size(rectangle) < 0)
599 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500600#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500601 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602 if (surface)
603 return display_create_egl_window_surface(display,
604 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400605 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100606 rectangle);
607 else
608 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400609 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100610 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500611 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400612#endif
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400613 return display_create_shm_surface(display, rectangle, flags, NULL);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400614}
615
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500616static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400617display_create_surface_from_file(struct display *display,
618 const char *filename,
619 struct rectangle *rectangle)
620{
nobled7b87cb02011-02-01 18:51:47 +0000621 if (check_size(rectangle) < 0)
622 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500623#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500624 if (display->dpy) {
625 return display_create_egl_image_surface_from_file(display,
626 filename,
627 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500628 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400629#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500630 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631}
Yuval Fledel45568f62010-12-06 09:18:12 -0500632 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400633 const char *filename;
634 int hotspot_x, hotspot_y;
635} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500636 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
637 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
638 { DATADIR "/weston/bottom_side.png", 16, 20 },
639 { DATADIR "/weston/grabbing.png", 20, 17 },
640 { DATADIR "/weston/left_ptr.png", 10, 5 },
641 { DATADIR "/weston/left_side.png", 10, 20 },
642 { DATADIR "/weston/right_side.png", 30, 19 },
643 { DATADIR "/weston/top_left_corner.png", 8, 8 },
644 { DATADIR "/weston/top_right_corner.png", 26, 8 },
645 { DATADIR "/weston/top_side.png", 18, 8 },
646 { DATADIR "/weston/xterm.png", 15, 15 },
647 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400648};
649
650static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400651create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400652{
653 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400654 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400655 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400657 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658 display->pointer_surfaces =
659 malloc(count * sizeof *display->pointer_surfaces);
660 rect.width = width;
661 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400662 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400663 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400664 display_create_surface_from_file(display,
665 pointer_images[i].filename,
666 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100667 if (!display->pointer_surfaces[i]) {
668 fprintf(stderr, "Error loading pointer image: %s\n",
669 pointer_images[i].filename);
670 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 }
672
673}
674
Pekka Paalanen325bb602011-12-19 10:31:45 +0200675static void
676destroy_pointer_surfaces(struct display *display)
677{
678 int i, count;
679
680 count = ARRAY_LENGTH(pointer_images);
681 for (i = 0; i < count; ++i) {
682 if (display->pointer_surfaces[i])
683 cairo_surface_destroy(display->pointer_surfaces[i]);
684 }
685 free(display->pointer_surfaces);
686}
687
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400688cairo_surface_t *
689display_get_pointer_surface(struct display *display, int pointer,
690 int *width, int *height,
691 int *hotspot_x, int *hotspot_y)
692{
693 cairo_surface_t *surface;
694
695 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500696#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400697 *width = cairo_gl_surface_get_width(surface);
698 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000699#else
700 *width = cairo_image_surface_get_width(surface);
701 *height = cairo_image_surface_get_height(surface);
702#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400703 *hotspot_x = pointer_images[pointer].hotspot_x;
704 *hotspot_y = pointer_images[pointer].hotspot_y;
705
706 return cairo_surface_reference(surface);
707}
708
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400709static void
710window_attach_surface(struct window *window);
711
712static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400713free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400714{
715 struct window *window = data;
716
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400717 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400718 cairo_surface_destroy(window->pending_surface);
719 window->pending_surface = NULL;
720 if (window->cairo_surface)
721 window_attach_surface(window);
722}
723
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400724static const struct wl_callback_listener free_surface_listener = {
725 free_surface
726};
727
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500728static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200729window_get_resize_dx_dy(struct window *window, int *x, int *y)
730{
731 if (window->resize_edges & WINDOW_RESIZING_LEFT)
732 *x = window->server_allocation.width - window->allocation.width;
733 else
734 *x = 0;
735
736 if (window->resize_edges & WINDOW_RESIZING_TOP)
737 *y = window->server_allocation.height -
738 window->allocation.height;
739 else
740 *y = 0;
741
742 window->resize_edges = 0;
743}
744
745static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500746window_attach_surface(struct window *window)
747{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400748 struct display *display = window->display;
749 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400750 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000751#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100752 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000753#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500754 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755
756 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000757#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100758 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
759 data = cairo_surface_get_user_data(window->cairo_surface,
760 &surface_data_key);
761
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100762 cairo_gl_surface_swapbuffers(window->cairo_surface);
763 wl_egl_window_get_attached_size(data->window,
764 &window->server_allocation.width,
765 &window->server_allocation.height);
766 break;
767 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000768#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100769 case WINDOW_BUFFER_TYPE_SHM:
770 if (window->pending_surface != NULL)
771 return;
772
773 window->pending_surface = window->cairo_surface;
774 window->cairo_surface = NULL;
775
776 buffer =
777 display_get_buffer_for_surface(display,
778 window->pending_surface);
779
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200780 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100781 wl_surface_attach(window->surface, buffer, x, y);
782 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400783 cb = wl_display_sync(display->display);
784 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100785 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000786 default:
787 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100788 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500789
Kristian Høgsberg63e5e062012-03-04 23:47:56 -0500790 if (window->input_region) {
791 wl_surface_set_input_region(window->surface,
792 window->input_region);
793 wl_region_destroy(window->input_region);
794 window->input_region = NULL;
795 }
796
797 if (window->opaque_region) {
798 wl_surface_set_opaque_region(window->surface,
799 window->opaque_region);
800 wl_region_destroy(window->opaque_region);
801 window->opaque_region = NULL;
802 }
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500803
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500804 wl_surface_damage(window->surface, 0, 0,
805 window->allocation.width,
806 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500807}
808
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500809void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400810window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500811{
Kristian Høgsberg9629fe32012-03-26 15:56:39 -0400812 if (window->cairo_surface)
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100813 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500814}
815
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400816void
817window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500819 cairo_surface_reference(surface);
820
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400821 if (window->cairo_surface != NULL)
822 cairo_surface_destroy(window->cairo_surface);
823
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500824 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400825}
826
Benjamin Franzke22d54812011-07-16 19:50:32 +0000827#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100828static void
829window_resize_cairo_window_surface(struct window *window)
830{
831 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200832 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100833
834 data = cairo_surface_get_user_data(window->cairo_surface,
835 &surface_data_key);
836
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200837 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100838 wl_egl_window_resize(data->window,
839 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200840 window->allocation.height,
841 x,y);
842
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100843 cairo_gl_surface_set_size(window->cairo_surface,
844 window->allocation.width,
845 window->allocation.height);
846}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000847#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100848
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400849struct display *
850window_get_display(struct window *window)
851{
852 return window->display;
853}
854
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400855void
856window_create_surface(struct window *window)
857{
858 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400859 uint32_t flags = 0;
860
861 if (!window->transparent)
862 flags = SURFACE_OPAQUE;
863
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400864 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500865#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100866 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
867 if (window->cairo_surface) {
868 window_resize_cairo_window_surface(window);
869 return;
870 }
871 surface = display_create_surface(window->display,
872 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400873 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500875 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400876 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100877 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400878 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879 break;
880#endif
881 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400882 surface = display_create_shm_surface(window->display,
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400883 &window->allocation,
884 flags, window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400885 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800886 default:
887 surface = NULL;
888 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400890
891 window_set_surface(window, surface);
892 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400893}
894
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200895static void frame_destroy(struct frame *frame);
896
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400897void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500898window_destroy(struct window *window)
899{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200900 struct display *display = window->display;
901 struct input *input;
902
903 if (window->redraw_scheduled)
904 wl_list_remove(&window->redraw_task.link);
905
906 wl_list_for_each(input, &display->input_list, link) {
907 if (input->pointer_focus == window)
908 input->pointer_focus = NULL;
909 if (input->keyboard_focus == window)
910 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500911 if (input->focus_widget &&
912 input->focus_widget->window == window)
913 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200914 }
915
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500916 if (window->input_region)
917 wl_region_destroy(window->input_region);
918 if (window->opaque_region)
919 wl_region_destroy(window->opaque_region);
920
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200921 if (window->frame)
922 frame_destroy(window->frame);
923
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200924 if (window->shell_surface)
925 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500926 wl_surface_destroy(window->surface);
927 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200928
929 if (window->cairo_surface != NULL)
930 cairo_surface_destroy(window->cairo_surface);
931 if (window->pending_surface != NULL)
932 cairo_surface_destroy(window->pending_surface);
933
934 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500935 free(window);
936}
937
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500938static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500939widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400940{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500941 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400942
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500943 wl_list_for_each(child, &widget->child_list, link) {
944 target = widget_find_widget(child, x, y);
945 if (target)
946 return target;
947 }
948
949 if (widget->allocation.x <= x &&
950 x < widget->allocation.x + widget->allocation.width &&
951 widget->allocation.y <= y &&
952 y < widget->allocation.y + widget->allocation.height) {
953 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400954 }
955
956 return NULL;
957}
958
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500959static struct widget *
960widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400961{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500962 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400963
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500964 widget = malloc(sizeof *widget);
965 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500966 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500967 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500968 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500969 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500970 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500971
972 return widget;
973}
974
975struct widget *
976window_add_widget(struct window *window, void *data)
977{
978 window->widget = widget_create(window, data);
979 wl_list_init(&window->widget->link);
980
981 return window->widget;
982}
983
984struct widget *
985widget_add_widget(struct widget *parent, void *data)
986{
987 struct widget *widget;
988
989 widget = widget_create(parent->window, data);
990 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400991
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500992 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400993}
994
995void
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500996widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400997{
Pekka Paalanene156fb62012-01-19 13:51:38 +0200998 struct display *display = widget->window->display;
999 struct input *input;
1000
1001 wl_list_for_each(input, &display->input_list, link) {
1002 if (input->focus_widget == widget)
1003 input->focus_widget = NULL;
1004 }
1005
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006 wl_list_remove(&widget->link);
1007 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008}
1009
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001012{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001013 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001014}
1015
1016void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001017widget_set_size(struct widget *widget, int32_t width, int32_t height)
1018{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001019 widget->allocation.width = width;
1020 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001021}
1022
1023void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001024widget_set_allocation(struct widget *widget,
1025 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001026{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001027 widget->allocation.x = x;
1028 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001029 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001030}
1031
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001032void
1033widget_set_transparent(struct widget *widget, int transparent)
1034{
1035 widget->opaque = !transparent;
1036}
1037
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001038void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001041 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001042}
1043
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001044void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001045widget_set_resize_handler(struct widget *widget,
1046 widget_resize_handler_t handler)
1047{
1048 widget->resize_handler = handler;
1049}
1050
1051void
1052widget_set_redraw_handler(struct widget *widget,
1053 widget_redraw_handler_t handler)
1054{
1055 widget->redraw_handler = handler;
1056}
1057
1058void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001059widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001060{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001061 widget->enter_handler = handler;
1062}
1063
1064void
1065widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1066{
1067 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001068}
1069
1070void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001071widget_set_motion_handler(struct widget *widget,
1072 widget_motion_handler_t handler)
1073{
1074 widget->motion_handler = handler;
1075}
1076
1077void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001078widget_set_button_handler(struct widget *widget,
1079 widget_button_handler_t handler)
1080{
1081 widget->button_handler = handler;
1082}
1083
1084void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001085widget_schedule_redraw(struct widget *widget)
1086{
1087 window_schedule_redraw(widget->window);
1088}
1089
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001090cairo_surface_t *
1091window_get_surface(struct window *window)
1092{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001093 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001094}
1095
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001096struct wl_surface *
1097window_get_wl_surface(struct window *window)
1098{
1099 return window->surface;
1100}
1101
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001102struct wl_shell_surface *
1103window_get_wl_shell_surface(struct window *window)
1104{
1105 return window->shell_surface;
1106}
1107
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001108static void
1109frame_resize_handler(struct widget *widget,
1110 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001111{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001112 struct frame *frame = data;
1113 struct widget *child = frame->child;
1114 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001115 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001116 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001117 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001118
Kristian Høgsbergb435e842012-03-05 20:38:08 -05001119 if (widget->window->type != TYPE_FULLSCREEN) {
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001120 decoration_width = (frame->width + frame->margin) * 2;
1121 decoration_height = frame->width +
1122 frame->titlebar_height + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001123
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001124 allocation.x = frame->width + frame->margin;
1125 allocation.y = frame->titlebar_height + frame->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001126 allocation.width = width - decoration_width;
1127 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001128
1129 widget->window->input_region =
1130 wl_compositor_create_region(display->compositor);
1131 wl_region_add(widget->window->input_region,
1132 frame->margin, frame->margin,
1133 width - 2 * frame->margin,
1134 height - 2 * frame->margin);
1135
1136 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001137 } else {
1138 decoration_width = 0;
1139 decoration_height = 0;
1140
1141 allocation.x = 0;
1142 allocation.y = 0;
1143 allocation.width = width;
1144 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001145 opaque_margin = 0;
1146 }
1147
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001148 widget_set_allocation(child, allocation.x, allocation.y,
1149 allocation.width, allocation.height);
1150
1151 if (child->resize_handler)
1152 child->resize_handler(child,
1153 allocation.width,
1154 allocation.height,
1155 child->user_data);
1156
1157 widget_set_allocation(widget, 0, 0,
1158 child->allocation.width + decoration_width,
1159 child->allocation.height + decoration_height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05001160
1161 if (child->opaque) {
1162 widget->window->opaque_region =
1163 wl_compositor_create_region(display->compositor);
1164 wl_region_add(widget->window->opaque_region,
1165 opaque_margin, opaque_margin,
1166 widget->allocation.width - 2 * opaque_margin,
1167 widget->allocation.height - 2 * opaque_margin);
1168 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169}
1170
1171static void
1172frame_redraw_handler(struct widget *widget, void *data)
1173{
1174 struct frame *frame = data;
1175 cairo_t *cr;
1176 cairo_text_extents_t extents;
1177 cairo_surface_t *source;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001178 int x, y, width, height;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001179 struct window *window = widget->window;
1180
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001181 if (window->type == TYPE_FULLSCREEN)
1182 return;
1183
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001184 width = widget->allocation.width;
1185 height = widget->allocation.height;
1186
1187 cr = cairo_create(window->cairo_surface);
1188
1189 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1190 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1191 cairo_paint(cr);
1192
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001193 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001194 tile_mask(cr, window->display->shadow,
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001195 2, 2, width + 8, height + 8,
1196 64, 64);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001197
1198 if (window->keyboard_device)
1199 source = window->display->active_frame;
1200 else
1201 source = window->display->inactive_frame;
1202
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001203 tile_source(cr, source,
1204 frame->margin, frame->margin,
1205 width - frame->margin * 2, height - frame->margin * 2,
1206 frame->width, frame->titlebar_height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001207
1208 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001209 cairo_select_font_face(cr, "sans",
1210 CAIRO_FONT_SLANT_NORMAL,
1211 CAIRO_FONT_WEIGHT_BOLD);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001212 cairo_set_font_size(cr, 14);
1213 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001214 x = (width - extents.width) / 2;
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001215 y = frame->margin + 8 - extents.y_bearing;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001216 if (window->keyboard_device) {
1217 cairo_move_to(cr, x + 1, y + 1);
1218 cairo_set_source_rgb(cr, 1, 1, 1);
1219 cairo_show_text(cr, window->title);
1220 cairo_move_to(cr, x, y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001221 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001222 cairo_show_text(cr, window->title);
1223 } else {
1224 cairo_move_to(cr, x, y);
1225 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1226 cairo_show_text(cr, window->title);
1227 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001228
1229 cairo_destroy(cr);
1230}
1231
1232static int
1233frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1234{
1235 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001236 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001237 const int grip_size = 8;
1238
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001239 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001240 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001241 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001242 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001243 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001244 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001245 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001246 hlocation = WINDOW_RESIZING_RIGHT;
1247 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001248 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001249
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001250 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001251 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001252 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001253 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001254 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001255 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001256 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001257 vlocation = WINDOW_RESIZING_BOTTOM;
1258 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001259 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001260
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001261 location = vlocation | hlocation;
1262 if (location & WINDOW_EXTERIOR)
1263 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001264 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001265 location = WINDOW_TITLEBAR;
1266 else if (location == WINDOW_INTERIOR)
1267 location = WINDOW_CLIENT_AREA;
1268
1269 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001270}
1271
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001272static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001274{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001275 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001278 switch (location) {
1279 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001280 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001281 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001282 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001284 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001285 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001286 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001288 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001289 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001290 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001292 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001293 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001294 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 case WINDOW_EXTERIOR:
1296 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001297 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001298 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001299 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001300}
1301
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001302static void
1303frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001304{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001305 switch (index) {
1306 case 0: /* close */
1307 if (window->close_handler)
1308 window->close_handler(window->parent,
1309 window->user_data);
1310 else
1311 display_exit(window->display);
1312 break;
1313 case 1: /* fullscreen */
1314 /* we don't have a way to get out of fullscreen for now */
1315 window_set_fullscreen(window, 1);
1316 break;
1317 case 2: /* rotate */
1318 case 3: /* scale */
1319 break;
1320 }
1321}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001322
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001323void
1324window_show_frame_menu(struct window *window,
1325 struct input *input, uint32_t time)
1326{
1327 int32_t x, y;
1328
1329 static const char *entries[] = {
1330 "Close", "Fullscreen", "Rotate", "Scale"
1331 };
1332
1333 input_get_position(input, &x, &y);
1334 window_show_menu(window->display, input, time, window,
1335 x - 10, y - 10, frame_menu_func, entries, 4);
1336}
1337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001338static int
1339frame_enter_handler(struct widget *widget,
1340 struct input *input, uint32_t time,
1341 int32_t x, int32_t y, void *data)
1342{
1343 return frame_get_pointer_image_for_location(data, input);
1344}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001345
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001346static int
1347frame_motion_handler(struct widget *widget,
1348 struct input *input, uint32_t time,
1349 int32_t x, int32_t y, void *data)
1350{
1351 return frame_get_pointer_image_for_location(data, input);
1352}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001353
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001354static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001355frame_button_handler(struct widget *widget,
1356 struct input *input, uint32_t time,
1357 int button, int state, void *data)
1358
1359{
1360 struct frame *frame = data;
1361 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001362 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001363 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001365 location = frame_get_pointer_location(frame, input->sx, input->sy);
1366
1367 if (window->display->shell && button == BTN_LEFT && state == 1) {
1368 switch (location) {
1369 case WINDOW_TITLEBAR:
1370 if (!window->shell_surface)
1371 break;
1372 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001373 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001374 wl_shell_surface_move(window->shell_surface,
1375 input_get_input_device(input),
1376 time);
1377 break;
1378 case WINDOW_RESIZING_TOP:
1379 case WINDOW_RESIZING_BOTTOM:
1380 case WINDOW_RESIZING_LEFT:
1381 case WINDOW_RESIZING_RIGHT:
1382 case WINDOW_RESIZING_TOP_LEFT:
1383 case WINDOW_RESIZING_TOP_RIGHT:
1384 case WINDOW_RESIZING_BOTTOM_LEFT:
1385 case WINDOW_RESIZING_BOTTOM_RIGHT:
1386 if (!window->shell_surface)
1387 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001388 input_ungrab(input, time);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001389
1390 if (!display->dpy) {
1391 /* If we're using shm, allocate a big
1392 pool to create buffers out of while
1393 we resize. We should probably base
1394 this number on the size of the output. */
1395 window->pool_size = 6 * 1024 * 1024;
1396 window->pool = make_shm_pool(display,
1397 window->pool_size,
1398 &window->pool_data);
1399 }
1400
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001401 wl_shell_surface_resize(window->shell_surface,
1402 input_get_input_device(input),
1403 time, location);
1404 break;
1405 }
1406 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001407 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001408 }
1409}
1410
1411struct widget *
1412frame_create(struct window *window, void *data)
1413{
1414 struct frame *frame;
1415
1416 frame = malloc(sizeof *frame);
1417 memset(frame, 0, sizeof *frame);
1418
1419 frame->widget = window_add_widget(window, frame);
1420 frame->child = widget_add_widget(frame->widget, data);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04001421 frame->margin = 32;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04001422 frame->width = 4;
1423 frame->titlebar_height = 30
1424;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001425 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1426 widget_set_resize_handler(frame->widget, frame_resize_handler);
1427 widget_set_enter_handler(frame->widget, frame_enter_handler);
1428 widget_set_motion_handler(frame->widget, frame_motion_handler);
1429 widget_set_button_handler(frame->widget, frame_button_handler);
1430
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001431 window->frame = frame;
1432
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001433 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001434}
1435
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001436static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001437frame_destroy(struct frame *frame)
1438{
1439 /* frame->child must be destroyed by the application */
1440 widget_destroy(frame->widget);
1441 free(frame);
1442}
1443
1444static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001445input_set_focus_widget(struct input *input, struct widget *focus,
1446 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001447{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001448 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001449 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001450
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001451 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001452 return;
1453
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001454 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001455 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001456 widget = old;
1457 if (input->grab)
1458 widget = input->grab;
1459 if (widget->leave_handler)
1460 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001461 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001462 }
1463
1464 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001465 widget = focus;
1466 if (input->grab)
1467 widget = input->grab;
1468 if (widget->enter_handler)
1469 pointer = widget->enter_handler(focus, input, time,
1470 x, y,
1471 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001473
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001474 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001475 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001476}
1477
1478static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001479input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001480 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001481{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001482 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001483 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001484 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001485 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001486
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001487 input->sx = sx;
1488 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001489
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001490 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001491 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001492 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001493 }
1494
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001495 if (input->grab)
1496 widget = input->grab;
1497 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001498 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001499 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001500 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001501 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001502 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001503
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001504 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001505}
1506
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001507void
1508input_grab(struct input *input, struct widget *widget, uint32_t button)
1509{
1510 input->grab = widget;
1511 input->grab_button = button;
1512}
1513
1514void
1515input_ungrab(struct input *input, uint32_t time)
1516{
1517 struct widget *widget;
1518
1519 input->grab = NULL;
1520 if (input->pointer_focus) {
1521 widget = widget_find_widget(input->pointer_focus->widget,
1522 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001523 input_set_focus_widget(input, widget,
1524 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001525 }
1526}
1527
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001528static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001529input_handle_button(void *data,
1530 struct wl_input_device *input_device,
1531 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001532{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001533 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001534 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001535
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001536 if (input->focus_widget && input->grab == NULL && state)
1537 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001538
Neil Roberts6b28aad2012-01-23 19:11:18 +00001539 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001540 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001541 (*widget->button_handler)(widget,
1542 input, time,
1543 button, state,
1544 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001545
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001546 if (input->grab && input->grab_button == button && !state)
1547 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001548}
1549
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001550static void
Scott Moreau210d0792012-03-22 10:47:01 -06001551input_handle_axis(void *data,
1552 struct wl_input_device *input_device,
1553 uint32_t time, uint32_t axis, int32_t value)
1554{
1555}
1556
1557static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001558input_handle_key(void *data, struct wl_input_device *input_device,
1559 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001560{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001561 struct input *input = data;
1562 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001563 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001564 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001565
Daniel Stone0d5a5092012-02-16 12:48:00 +00001566 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001567 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001568 return;
1569
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001570 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001571 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001572 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1573 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001574
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001575 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1576
1577 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001578 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001579 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001580 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001581
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001582 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1583 if (state)
1584 window_set_maximized(window,
1585 window->type != TYPE_MAXIMIZED);
1586 } else if (window->key_handler) {
1587 (*window->key_handler)(window, input, time, key,
1588 sym, state, window->user_data);
1589 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001590}
1591
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001592static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001593input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001594{
1595 struct window *window = input->pointer_focus;
1596
1597 if (!window)
1598 return;
1599
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001600 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001601
Pekka Paalanene1207c72011-12-16 12:02:09 +02001602 input->pointer_focus = NULL;
1603 input->current_pointer_image = POINTER_UNSET;
1604}
1605
1606static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001607input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001608 struct wl_input_device *input_device,
1609 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001610 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001611{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001612 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001613 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001614 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001615
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001616 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001617 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001618
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04001619 if (window->pool) {
1620 wl_shm_pool_destroy(window->pool);
1621 munmap(window->pool_data, window->pool_size);
1622 window->pool = NULL;
1623 /* Schedule a redraw to free the pool */
1624 window_schedule_redraw(window);
1625 }
1626
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001627 input->sx = sx;
1628 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001629
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001630 widget = widget_find_widget(window->widget, sx, sy);
1631 input_set_focus_widget(input, widget, time, sx, sy);
1632}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001633
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001634static void
1635input_handle_pointer_leave(void *data,
1636 struct wl_input_device *input_device,
1637 uint32_t time, struct wl_surface *surface)
1638{
1639 struct input *input = data;
1640
1641 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001642}
1643
1644static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001645input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001646{
1647 struct window *window = input->keyboard_focus;
1648
1649 if (!window)
1650 return;
1651
1652 window->keyboard_device = NULL;
1653 if (window->keyboard_focus_handler)
1654 (*window->keyboard_focus_handler)(window, NULL,
1655 window->user_data);
1656
1657 input->keyboard_focus = NULL;
1658}
1659
1660static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001661input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001662 struct wl_input_device *input_device,
1663 uint32_t time,
1664 struct wl_surface *surface,
1665 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001666{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001667 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001668 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001669 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001670 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001671
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001672 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001673
1674 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001675 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001676 for (k = keys->data; k < end; k++)
1677 input->modifiers |= d->xkb->map->modmap[*k];
1678
1679 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001680 window->keyboard_device = input;
1681 if (window->keyboard_focus_handler)
1682 (*window->keyboard_focus_handler)(window,
1683 window->keyboard_device,
1684 window->user_data);
1685}
1686
1687static void
1688input_handle_keyboard_leave(void *data,
1689 struct wl_input_device *input_device,
1690 uint32_t time,
1691 struct wl_surface *surface)
1692{
1693 struct input *input = data;
1694
1695 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001696}
1697
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001698static void
1699input_handle_touch_down(void *data,
1700 struct wl_input_device *wl_input_device,
1701 uint32_t time, struct wl_surface *surface,
1702 int32_t id, int32_t x, int32_t y)
1703{
1704}
1705
1706static void
1707input_handle_touch_up(void *data,
1708 struct wl_input_device *wl_input_device,
1709 uint32_t time, int32_t id)
1710{
1711}
1712
1713static void
1714input_handle_touch_motion(void *data,
1715 struct wl_input_device *wl_input_device,
1716 uint32_t time, int32_t id, int32_t x, int32_t y)
1717{
1718}
1719
1720static void
1721input_handle_touch_frame(void *data,
1722 struct wl_input_device *wl_input_device)
1723{
1724}
1725
1726static void
1727input_handle_touch_cancel(void *data,
1728 struct wl_input_device *wl_input_device)
1729{
1730}
1731
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001732static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001733 input_handle_motion,
1734 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -06001735 input_handle_axis,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001736 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001737 input_handle_pointer_enter,
1738 input_handle_pointer_leave,
1739 input_handle_keyboard_enter,
1740 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001741 input_handle_touch_down,
1742 input_handle_touch_up,
1743 input_handle_touch_motion,
1744 input_handle_touch_frame,
1745 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001746};
1747
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001748void
1749input_get_position(struct input *input, int32_t *x, int32_t *y)
1750{
1751 *x = input->sx;
1752 *y = input->sy;
1753}
1754
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001755struct wl_input_device *
1756input_get_input_device(struct input *input)
1757{
1758 return input->input_device;
1759}
1760
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001761uint32_t
1762input_get_modifiers(struct input *input)
1763{
1764 return input->modifiers;
1765}
1766
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001767struct widget *
1768input_get_focus_widget(struct input *input)
1769{
1770 return input->focus_widget;
1771}
1772
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001773struct data_offer {
1774 struct wl_data_offer *offer;
1775 struct input *input;
1776 struct wl_array types;
1777 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001778
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001779 struct task io_task;
1780 int fd;
1781 data_func_t func;
1782 int32_t x, y;
1783 void *user_data;
1784};
1785
1786static void
1787data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1788{
1789 struct data_offer *offer = data;
1790 char **p;
1791
1792 p = wl_array_add(&offer->types, sizeof *p);
1793 *p = strdup(type);
1794}
1795
1796static const struct wl_data_offer_listener data_offer_listener = {
1797 data_offer_offer,
1798};
1799
1800static void
1801data_offer_destroy(struct data_offer *offer)
1802{
1803 char **p;
1804
1805 offer->refcount--;
1806 if (offer->refcount == 0) {
1807 wl_data_offer_destroy(offer->offer);
1808 for (p = offer->types.data; *p; p++)
1809 free(*p);
1810 wl_array_release(&offer->types);
1811 free(offer);
1812 }
1813}
1814
1815static void
1816data_device_data_offer(void *data,
1817 struct wl_data_device *data_device, uint32_t id)
1818{
1819 struct data_offer *offer;
1820
1821 offer = malloc(sizeof *offer);
1822
1823 wl_array_init(&offer->types);
1824 offer->refcount = 1;
1825 offer->input = data;
1826
1827 /* FIXME: Generate typesafe wrappers for this */
1828 offer->offer = (struct wl_data_offer *)
1829 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1830 id, &wl_data_offer_interface);
1831
1832 wl_data_offer_add_listener(offer->offer,
1833 &data_offer_listener, offer);
1834}
1835
1836static void
1837data_device_enter(void *data, struct wl_data_device *data_device,
1838 uint32_t time, struct wl_surface *surface,
1839 int32_t x, int32_t y, struct wl_data_offer *offer)
1840{
1841 struct input *input = data;
1842 struct window *window;
1843 char **p;
1844
1845 input->drag_offer = wl_data_offer_get_user_data(offer);
1846 window = wl_surface_get_user_data(surface);
1847 input->pointer_focus = window;
1848
1849 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1850 *p = NULL;
1851
1852 window = input->pointer_focus;
1853 if (window->data_handler)
1854 window->data_handler(window, input, time, x, y,
1855 input->drag_offer->types.data,
1856 window->user_data);
1857}
1858
1859static void
1860data_device_leave(void *data, struct wl_data_device *data_device)
1861{
1862 struct input *input = data;
1863
1864 data_offer_destroy(input->drag_offer);
1865 input->drag_offer = NULL;
1866}
1867
1868static void
1869data_device_motion(void *data, struct wl_data_device *data_device,
1870 uint32_t time, int32_t x, int32_t y)
1871{
1872 struct input *input = data;
1873 struct window *window = input->pointer_focus;
1874
1875 input->sx = x;
1876 input->sy = y;
1877
1878 if (window->data_handler)
1879 window->data_handler(window, input, time, x, y,
1880 input->drag_offer->types.data,
1881 window->user_data);
1882}
1883
1884static void
1885data_device_drop(void *data, struct wl_data_device *data_device)
1886{
1887 struct input *input = data;
1888 struct window *window = input->pointer_focus;
1889
1890 if (window->drop_handler)
1891 window->drop_handler(window, input,
1892 input->sx, input->sy, window->user_data);
1893}
1894
1895static void
1896data_device_selection(void *data,
1897 struct wl_data_device *wl_data_device,
1898 struct wl_data_offer *offer)
1899{
1900 struct input *input = data;
1901 char **p;
1902
1903 if (input->selection_offer)
1904 data_offer_destroy(input->selection_offer);
1905
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001906 if (offer) {
1907 input->selection_offer = wl_data_offer_get_user_data(offer);
1908 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1909 *p = NULL;
1910 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001911}
1912
1913static const struct wl_data_device_listener data_device_listener = {
1914 data_device_data_offer,
1915 data_device_enter,
1916 data_device_leave,
1917 data_device_motion,
1918 data_device_drop,
1919 data_device_selection
1920};
1921
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001922void
1923input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1924{
1925 struct display *display = input->display;
1926 struct wl_buffer *buffer;
1927 cairo_surface_t *surface;
1928
1929 if (pointer == input->current_pointer_image)
1930 return;
1931
1932 input->current_pointer_image = pointer;
1933 surface = display->pointer_surfaces[pointer];
1934
1935 if (!surface)
1936 return;
1937
1938 buffer = display_get_buffer_for_surface(display, surface);
1939 wl_input_device_attach(input->input_device, time, buffer,
1940 pointer_images[pointer].hotspot_x,
1941 pointer_images[pointer].hotspot_y);
1942}
1943
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001944struct wl_data_device *
1945input_get_data_device(struct input *input)
1946{
1947 return input->data_device;
1948}
1949
1950void
1951input_set_selection(struct input *input,
1952 struct wl_data_source *source, uint32_t time)
1953{
1954 wl_data_device_set_selection(input->data_device, source, time);
1955}
1956
1957void
1958input_accept(struct input *input, uint32_t time, const char *type)
1959{
1960 wl_data_offer_accept(input->drag_offer->offer, time, type);
1961}
1962
1963static void
1964offer_io_func(struct task *task, uint32_t events)
1965{
1966 struct data_offer *offer =
1967 container_of(task, struct data_offer, io_task);
1968 unsigned int len;
1969 char buffer[4096];
1970
1971 len = read(offer->fd, buffer, sizeof buffer);
1972 offer->func(buffer, len,
1973 offer->x, offer->y, offer->user_data);
1974
1975 if (len == 0) {
1976 close(offer->fd);
1977 data_offer_destroy(offer);
1978 }
1979}
1980
1981static void
1982data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1983 data_func_t func, void *user_data)
1984{
1985 int p[2];
1986
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02001987 if (pipe2(p, O_CLOEXEC) == -1)
1988 return;
1989
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001990 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1991 close(p[1]);
1992
1993 offer->io_task.run = offer_io_func;
1994 offer->fd = p[0];
1995 offer->func = func;
1996 offer->refcount++;
1997 offer->user_data = user_data;
1998
1999 display_watch_fd(offer->input->display,
2000 offer->fd, EPOLLIN, &offer->io_task);
2001}
2002
2003void
2004input_receive_drag_data(struct input *input, const char *mime_type,
2005 data_func_t func, void *data)
2006{
2007 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2008 input->drag_offer->x = input->sx;
2009 input->drag_offer->y = input->sy;
2010}
2011
2012int
2013input_receive_selection_data(struct input *input, const char *mime_type,
2014 data_func_t func, void *data)
2015{
2016 char **p;
2017
2018 if (input->selection_offer == NULL)
2019 return -1;
2020
2021 for (p = input->selection_offer->types.data; *p; p++)
2022 if (strcmp(mime_type, *p) == 0)
2023 break;
2024
2025 if (*p == NULL)
2026 return -1;
2027
2028 data_offer_receive_data(input->selection_offer,
2029 mime_type, func, data);
2030 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002031}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002032
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002033int
2034input_receive_selection_data_to_fd(struct input *input,
2035 const char *mime_type, int fd)
2036{
2037 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2038
2039 return 0;
2040}
2041
Kristian Høgsberg41da9082010-11-30 14:01:07 -05002042void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002043window_move(struct window *window, struct input *input, uint32_t time)
2044{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002045 if (!window->shell_surface)
2046 return;
2047
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002048 wl_shell_surface_move(window->shell_surface,
2049 input->input_device, time);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002050}
2051
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002052static void
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002053idle_resize(struct window *window)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002054{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002055 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002056
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002057 window->resize_needed = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002058 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002059 widget_set_allocation(widget,
2060 window->pending_allocation.x,
2061 window->pending_allocation.y,
2062 window->pending_allocation.width,
2063 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002064
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002065 if (window->input_region) {
2066 wl_region_destroy(window->input_region);
2067 window->input_region = NULL;
2068 }
2069
2070 if (window->opaque_region) {
2071 wl_region_destroy(window->opaque_region);
2072 window->opaque_region = NULL;
2073 }
2074
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002075 if (widget->resize_handler)
2076 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002077 widget->allocation.width,
2078 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002079 widget->user_data);
2080
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002081 if (window->allocation.width != widget->allocation.width ||
2082 window->allocation.height != widget->allocation.height) {
2083 window->allocation = widget->allocation;
2084 window_schedule_redraw(window);
2085 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002086}
2087
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002088void
2089window_schedule_resize(struct window *window, int width, int height)
2090{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002091 window->pending_allocation.x = 0;
2092 window->pending_allocation.y = 0;
2093 window->pending_allocation.width = width;
2094 window->pending_allocation.height = height;
2095
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002096 window->resize_needed = 1;
2097 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002098}
2099
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002100void
2101widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2102{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002103 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002104}
2105
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002106static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002107handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002108 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002109 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002110{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002112
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002113 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002114 return;
2115
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002116 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002117 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002118}
2119
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002120static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002121menu_destroy(struct menu *menu)
2122{
2123 widget_destroy(menu->widget);
2124 window_destroy(menu->window);
2125 free(menu);
2126}
2127
2128static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002129handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2130{
2131 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002132 struct menu *menu = window->widget->user_data;
2133
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002134 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002135 * device. Or just use wl_callback. And this really needs to
2136 * be a window vfunc that the menu can set. And we need the
2137 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002138
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002139 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002140 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002141 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002142}
2143
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002144static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002145 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002146 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002147};
2148
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002149void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002150window_get_allocation(struct window *window,
2151 struct rectangle *allocation)
2152{
2153 *allocation = window->allocation;
2154}
2155
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002156static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002157widget_redraw(struct widget *widget)
2158{
2159 struct widget *child;
2160
2161 if (widget->redraw_handler)
2162 widget->redraw_handler(widget, widget->user_data);
2163 wl_list_for_each(child, &widget->child_list, link)
2164 widget_redraw(child);
2165}
2166
2167static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002168frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2169{
2170 struct window *window = data;
2171
2172 wl_callback_destroy(callback);
2173 window->redraw_scheduled = 0;
2174 if (window->redraw_needed)
2175 window_schedule_redraw(window);
2176}
2177
2178static const struct wl_callback_listener listener = {
2179 frame_callback
2180};
2181
2182static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002183idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002184{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002185 struct window *window = container_of(task, struct window, redraw_task);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002186 struct wl_callback *callback;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002187
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04002188 if (window->resize_needed)
2189 idle_resize(window);
2190
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002191 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002192 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002193 window_flush(window);
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002194 window->redraw_needed = 0;
2195
2196 callback = wl_surface_frame(window->surface);
2197 wl_callback_add_listener(callback, &listener, window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002198}
2199
2200void
2201window_schedule_redraw(struct window *window)
2202{
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04002203 window->redraw_needed = 1;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002204 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002205 window->redraw_task.run = idle_redraw;
2206 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002207 window->redraw_scheduled = 1;
2208 }
2209}
2210
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002211void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002212window_set_custom(struct window *window)
2213{
2214 window->type = TYPE_CUSTOM;
2215}
2216
2217void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002218window_set_fullscreen(struct window *window, int fullscreen)
2219{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002220 if (!window->display->shell)
2221 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002222
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002223 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002224 return;
2225
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002226 if (fullscreen) {
2227 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002228 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002229 wl_shell_surface_set_fullscreen(window->shell_surface,
2230 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2231 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002232 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002233 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002234 wl_shell_surface_set_toplevel(window->shell_surface);
2235 window_schedule_resize(window,
2236 window->saved_allocation.width,
2237 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002238 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002239}
2240
2241void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002242window_set_maximized(struct window *window, int maximized)
2243{
2244 if (!window->display->shell)
2245 return;
2246
2247 if ((window->type == TYPE_MAXIMIZED) == maximized)
2248 return;
2249
2250 if (window->type == TYPE_TOPLEVEL) {
2251 window->saved_allocation = window->allocation;
2252 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2253 window->type = TYPE_MAXIMIZED;
2254 } else {
2255 wl_shell_surface_set_toplevel(window->shell_surface);
2256 window->type = TYPE_TOPLEVEL;
2257 window_schedule_resize(window,
2258 window->saved_allocation.width,
2259 window->saved_allocation.height);
2260 }
2261}
2262
2263void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002264window_set_user_data(struct window *window, void *data)
2265{
2266 window->user_data = data;
2267}
2268
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002269void *
2270window_get_user_data(struct window *window)
2271{
2272 return window->user_data;
2273}
2274
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002275void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002276window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002277 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002278{
2279 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002280}
2281
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002282void
2283window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002284 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002285{
2286 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002287}
2288
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002289void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002290window_set_data_handler(struct window *window, window_data_handler_t handler)
2291{
2292 window->data_handler = handler;
2293}
2294
2295void
2296window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2297{
2298 window->drop_handler = handler;
2299}
2300
2301void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002302window_set_close_handler(struct window *window,
2303 window_close_handler_t handler)
2304{
2305 window->close_handler = handler;
2306}
2307
2308void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002309window_set_title(struct window *window, const char *title)
2310{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002311 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002312 window->title = strdup(title);
2313}
2314
2315const char *
2316window_get_title(struct window *window)
2317{
2318 return window->title;
2319}
2320
2321void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002322window_damage(struct window *window, int32_t x, int32_t y,
2323 int32_t width, int32_t height)
2324{
2325 wl_surface_damage(window->surface, x, y, width, height);
2326}
2327
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002328static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002329window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002330{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002331 struct window *window;
2332
2333 window = malloc(sizeof *window);
2334 if (window == NULL)
2335 return NULL;
2336
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002337 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002338 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002339 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002340 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002341 if (display->shell) {
2342 window->shell_surface =
2343 wl_shell_get_shell_surface(display->shell,
2344 window->surface);
2345 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002346 window->allocation.x = 0;
2347 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002348 window->allocation.width = 0;
2349 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002350 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002351 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002352 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002353 window->input_region = NULL;
2354 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002355
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002356 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002357#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002358 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2359 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002360#else
2361 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2362#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002363 else
2364 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002365
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002366 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002367 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002368
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002369 if (window->shell_surface) {
2370 wl_shell_surface_set_user_data(window->shell_surface, window);
2371 wl_shell_surface_add_listener(window->shell_surface,
2372 &shell_surface_listener, window);
2373 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002374
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002375 return window;
2376}
2377
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002378struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002379window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002380{
2381 struct window *window;
2382
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002383 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002384 if (!window)
2385 return NULL;
2386
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002387 window->type = TYPE_TOPLEVEL;
2388 if (display->shell)
2389 wl_shell_surface_set_toplevel(window->shell_surface);
2390
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002391 return window;
2392}
2393
2394struct window *
2395window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002396 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002397{
2398 struct window *window;
2399
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002400 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002401 if (!window)
2402 return NULL;
2403
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002404 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002405 window->x = x;
2406 window->y = y;
2407
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002408 if (display->shell)
2409 wl_shell_surface_set_transient(window->shell_surface,
2410 window->parent->shell_surface,
2411 window->x, window->y, 0);
2412
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002413 return window;
2414}
2415
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002416static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002417menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002418{
2419 int next;
2420
2421 next = (sy - 8) / 20;
2422 if (menu->current != next) {
2423 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002424 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002425 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002426}
2427
2428static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002429menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002430 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002431 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002432{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002433 struct menu *menu = data;
2434
2435 if (widget == menu->widget)
2436 menu_set_item(data, y);
2437
2438 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002439}
2440
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002441static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002442menu_enter_handler(struct widget *widget,
2443 struct input *input, uint32_t time,
2444 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002445{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002446 struct menu *menu = data;
2447
2448 if (widget == menu->widget)
2449 menu_set_item(data, y);
2450
2451 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002452}
2453
2454static void
2455menu_leave_handler(struct widget *widget, struct input *input, void *data)
2456{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002457 struct menu *menu = data;
2458
2459 if (widget == menu->widget)
2460 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002461}
2462
2463static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002464menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002465 struct input *input, uint32_t time,
2466 int button, int state, void *data)
2467
2468{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002469 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002470
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002471 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002472 /* Either relase after press-drag-release or
2473 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002474 menu->func(menu->window->parent,
2475 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002476 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002477 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002478 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002479}
2480
2481static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002482menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002483{
2484 cairo_t *cr;
2485 const int32_t r = 3, margin = 3;
2486 struct menu *menu = data;
2487 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002488 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002489
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002490 cr = cairo_create(window->cairo_surface);
2491 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2492 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2493 cairo_paint(cr);
2494
2495 width = window->allocation.width;
2496 height = window->allocation.height;
2497 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002498
2499 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002500 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2501 cairo_fill(cr);
2502
2503 for (i = 0; i < menu->count; i++) {
2504 if (i == menu->current) {
2505 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2506 cairo_rectangle(cr, margin, i * 20 + margin,
2507 width - 2 * margin, 20);
2508 cairo_fill(cr);
2509 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2510 cairo_move_to(cr, 10, i * 20 + 16);
2511 cairo_show_text(cr, menu->entries[i]);
2512 } else {
2513 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2514 cairo_move_to(cr, 10, i * 20 + 16);
2515 cairo_show_text(cr, menu->entries[i]);
2516 }
2517 }
2518
2519 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002520}
2521
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002522void
2523window_show_menu(struct display *display,
2524 struct input *input, uint32_t time, struct window *parent,
2525 int32_t x, int32_t y,
2526 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002527{
2528 struct window *window;
2529 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002530 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002531
2532 menu = malloc(sizeof *menu);
2533 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002534 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002535
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002536 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002537 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002538 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002539
2540 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002541 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002542 menu->entries = entries;
2543 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002544 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002545 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002546 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002547 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002548 window->type = TYPE_MENU;
2549 window->x = x;
2550 window->y = y;
2551
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002552 wl_shell_surface_set_popup(window->shell_surface,
2553 input->input_device, time,
2554 window->parent->shell_surface,
2555 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002556
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002557 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002558 widget_set_enter_handler(menu->widget, menu_enter_handler);
2559 widget_set_leave_handler(menu->widget, menu_leave_handler);
2560 widget_set_motion_handler(menu->widget, menu_motion_handler);
2561 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002562
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002563 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002564 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002565}
2566
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002567void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002568window_set_buffer_type(struct window *window, enum window_buffer_type type)
2569{
2570 window->buffer_type = type;
2571}
2572
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002573
2574static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002575display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002576 struct wl_output *wl_output,
2577 int x, int y,
2578 int physical_width,
2579 int physical_height,
2580 int subpixel,
2581 const char *make,
2582 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002583{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002584 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002585
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002586 output->allocation.x = x;
2587 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002588}
2589
2590static void
2591display_handle_mode(void *data,
2592 struct wl_output *wl_output,
2593 uint32_t flags,
2594 int width,
2595 int height,
2596 int refresh)
2597{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002598 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002599 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002600
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002601 if (flags & WL_OUTPUT_MODE_CURRENT) {
2602 output->allocation.width = width;
2603 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002604 if (display->output_configure_handler)
2605 (*display->output_configure_handler)(
2606 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002607 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002608}
2609
2610static const struct wl_output_listener output_listener = {
2611 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002612 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002613};
2614
2615static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002616display_add_output(struct display *d, uint32_t id)
2617{
2618 struct output *output;
2619
2620 output = malloc(sizeof *output);
2621 if (output == NULL)
2622 return;
2623
2624 memset(output, 0, sizeof *output);
2625 output->display = d;
2626 output->output =
2627 wl_display_bind(d->display, id, &wl_output_interface);
2628 wl_list_insert(d->output_list.prev, &output->link);
2629
2630 wl_output_add_listener(output->output, &output_listener, output);
2631}
2632
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002633static void
2634output_destroy(struct output *output)
2635{
2636 if (output->destroy_handler)
2637 (*output->destroy_handler)(output, output->user_data);
2638
2639 wl_output_destroy(output->output);
2640 wl_list_remove(&output->link);
2641 free(output);
2642}
2643
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002644void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002645display_set_output_configure_handler(struct display *display,
2646 display_output_handler_t handler)
2647{
2648 struct output *output;
2649
2650 display->output_configure_handler = handler;
2651 if (!handler)
2652 return;
2653
2654 wl_list_for_each(output, &display->output_list, link)
2655 (*display->output_configure_handler)(output,
2656 display->user_data);
2657}
2658
2659void
2660output_set_user_data(struct output *output, void *data)
2661{
2662 output->user_data = data;
2663}
2664
2665void *
2666output_get_user_data(struct output *output)
2667{
2668 return output->user_data;
2669}
2670
2671void
2672output_set_destroy_handler(struct output *output,
2673 display_output_handler_t handler)
2674{
2675 output->destroy_handler = handler;
2676 /* FIXME: implement this, once we have way to remove outputs */
2677}
2678
2679void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002680output_get_allocation(struct output *output, struct rectangle *allocation)
2681{
2682 *allocation = output->allocation;
2683}
2684
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002685struct wl_output *
2686output_get_wl_output(struct output *output)
2687{
2688 return output->output;
2689}
2690
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002691static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002692display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002693{
2694 struct input *input;
2695
2696 input = malloc(sizeof *input);
2697 if (input == NULL)
2698 return;
2699
2700 memset(input, 0, sizeof *input);
2701 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002702 input->input_device =
2703 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002704 input->pointer_focus = NULL;
2705 input->keyboard_focus = NULL;
2706 wl_list_insert(d->input_list.prev, &input->link);
2707
2708 wl_input_device_add_listener(input->input_device,
2709 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002710 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002711
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002712 input->data_device =
2713 wl_data_device_manager_get_data_device(d->data_device_manager,
2714 input->input_device);
2715 wl_data_device_add_listener(input->data_device,
2716 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002717}
2718
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002719static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002720input_destroy(struct input *input)
2721{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002722 input_remove_keyboard_focus(input);
2723 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002724
2725 if (input->drag_offer)
2726 data_offer_destroy(input->drag_offer);
2727
2728 if (input->selection_offer)
2729 data_offer_destroy(input->selection_offer);
2730
2731 wl_data_device_destroy(input->data_device);
2732 wl_list_remove(&input->link);
2733 wl_input_device_destroy(input->input_device);
2734 free(input);
2735}
2736
2737static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002738display_handle_global(struct wl_display *display, uint32_t id,
2739 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002740{
2741 struct display *d = data;
2742
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002743 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002744 d->compositor =
2745 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002746 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002747 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002748 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002749 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002750 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002751 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002752 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002753 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002754 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2755 d->data_device_manager =
2756 wl_display_bind(display, id,
2757 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002758 }
2759}
2760
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002761static void
2762display_render_frame(struct display *d)
2763{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002764 cairo_t *cr;
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002765 cairo_pattern_t *pattern;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002766
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002767 d->frame_radius = 3;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002768 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2769 cr = cairo_create(d->shadow);
2770 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2771 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002772 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002773 cairo_fill(cr);
2774 cairo_destroy(cr);
2775 blur_surface(d->shadow, 64);
2776
2777 d->active_frame =
2778 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2779 cr = cairo_create(d->active_frame);
2780 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002781
2782 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2783 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2784 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2785 cairo_set_source(cr, pattern);
2786 cairo_pattern_destroy(pattern);
2787
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002788 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002789 cairo_fill(cr);
2790 cairo_destroy(cr);
2791
2792 d->inactive_frame =
2793 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2794 cr = cairo_create(d->inactive_frame);
2795 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergb8abe7e2012-03-20 23:56:05 -04002796 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
Kristian Høgsbergec323d22012-03-21 01:07:49 -04002797 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002798 cairo_fill(cr);
2799 cairo_destroy(cr);
2800}
2801
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002802static void
2803init_xkb(struct display *d)
2804{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002805 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002806
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002807 names.rules = "evdev";
2808 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002809 names.layout = option_xkb_layout;
2810 names.variant = option_xkb_variant;
2811 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002812
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002813 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002814 if (!d->xkb) {
2815 fprintf(stderr, "Failed to compile keymap\n");
2816 exit(1);
2817 }
2818}
2819
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002820static void
2821fini_xkb(struct display *display)
2822{
2823 xkb_free_keymap(display->xkb);
2824}
2825
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002826#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05002827static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002828init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002829{
2830 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002831 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002832
Rob Clark6396ed32012-03-11 19:48:41 -05002833#ifdef USE_CAIRO_GLESV2
2834# define GL_BIT EGL_OPENGL_ES2_BIT
2835#else
2836# define GL_BIT EGL_OPENGL_BIT
2837#endif
2838
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002839 static const EGLint argb_cfg_attribs[] = {
2840 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002841 EGL_RED_SIZE, 1,
2842 EGL_GREEN_SIZE, 1,
2843 EGL_BLUE_SIZE, 1,
2844 EGL_ALPHA_SIZE, 1,
2845 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05002846 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002847 EGL_NONE
2848 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002849
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002850#ifdef USE_CAIRO_GLESV2
2851 static const EGLint context_attribs[] = {
2852 EGL_CONTEXT_CLIENT_VERSION, 2,
2853 EGL_NONE
2854 };
2855 EGLint api = EGL_OPENGL_ES_API;
2856#else
2857 EGLint *context_attribs = NULL;
2858 EGLint api = EGL_OPENGL_API;
2859#endif
2860
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002861 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002862 if (!eglInitialize(d->dpy, &major, &minor)) {
2863 fprintf(stderr, "failed to initialize display\n");
2864 return -1;
2865 }
2866
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002867 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002868 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2869 return -1;
2870 }
2871
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002872 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2873 &d->argb_config, 1, &n) || n != 1) {
2874 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002875 return -1;
2876 }
2877
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002878 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002879 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002880 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002881 fprintf(stderr, "failed to create context\n");
2882 return -1;
2883 }
2884
Kristian Høgsberg067fd602012-02-29 16:15:53 -05002885 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002886 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002887 return -1;
2888 }
2889
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002890#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002891 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2892 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2893 fprintf(stderr, "failed to get cairo egl argb device\n");
2894 return -1;
2895 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002896#endif
2897
2898 return 0;
2899}
2900
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002901static void
2902fini_egl(struct display *display)
2903{
2904#ifdef HAVE_CAIRO_EGL
2905 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002906#endif
2907
2908 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2909 EGL_NO_CONTEXT);
2910
2911 eglTerminate(display->dpy);
2912 eglReleaseThread();
2913}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002914#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002915
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002916static int
2917event_mask_update(uint32_t mask, void *data)
2918{
2919 struct display *d = data;
2920
2921 d->mask = mask;
2922
2923 return 0;
2924}
2925
2926static void
2927handle_display_data(struct task *task, uint32_t events)
2928{
2929 struct display *display =
2930 container_of(task, struct display, display_task);
2931
2932 wl_display_iterate(display->display, display->mask);
2933}
2934
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002935struct display *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002936display_create(int argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002937{
2938 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002939
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002940 argc = parse_options(xkb_options,
2941 ARRAY_LENGTH(xkb_options), argc, argv);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002942
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002943 d = malloc(sizeof *d);
2944 if (d == NULL)
2945 return NULL;
2946
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002947 memset(d, 0, sizeof *d);
2948
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002949 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002950 if (d->display == NULL) {
2951 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002952 return NULL;
2953 }
2954
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002955 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2956 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2957 d->display_task.run = handle_display_data;
2958 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2959
2960 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002961 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002962 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002963
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002964 /* Set up listener so we'll catch all events. */
2965 wl_display_add_global_listener(d->display,
2966 display_handle_global, d);
2967
2968 /* Process connection events. */
2969 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002970#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002971 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002972 return NULL;
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04002973#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05002974
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05002975 d->image_target_texture_2d =
2976 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2977 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2978 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2979
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002980 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002981
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002982 display_render_frame(d);
2983
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002984 wl_list_init(&d->window_list);
2985
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002986 init_xkb(d);
2987
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002988 return d;
2989}
2990
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002991static void
2992display_destroy_outputs(struct display *display)
2993{
2994 struct output *tmp;
2995 struct output *output;
2996
2997 wl_list_for_each_safe(output, tmp, &display->output_list, link)
2998 output_destroy(output);
2999}
3000
Pekka Paalanene1207c72011-12-16 12:02:09 +02003001static void
3002display_destroy_inputs(struct display *display)
3003{
3004 struct input *tmp;
3005 struct input *input;
3006
3007 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3008 input_destroy(input);
3009}
3010
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003011void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003012display_destroy(struct display *display)
3013{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003014 if (!wl_list_empty(&display->window_list))
3015 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3016
3017 if (!wl_list_empty(&display->deferred_list))
3018 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3019
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003020 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003021 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003022
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003023 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003024
3025 cairo_surface_destroy(display->active_frame);
3026 cairo_surface_destroy(display->inactive_frame);
3027 cairo_surface_destroy(display->shadow);
3028 destroy_pointer_surfaces(display);
3029
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003030#ifdef HAVE_CAIRO_EGL
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003031 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04003032#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003033
Pekka Paalanenc2052982011-12-16 11:41:32 +02003034 if (display->shell)
3035 wl_shell_destroy(display->shell);
3036
3037 if (display->shm)
3038 wl_shm_destroy(display->shm);
3039
3040 if (display->data_device_manager)
3041 wl_data_device_manager_destroy(display->data_device_manager);
3042
3043 wl_compositor_destroy(display->compositor);
3044
3045 close(display->epoll_fd);
3046
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003047 wl_display_flush(display->display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05003048 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003049 free(display);
3050}
3051
3052void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003053display_set_user_data(struct display *display, void *data)
3054{
3055 display->user_data = data;
3056}
3057
3058void *
3059display_get_user_data(struct display *display)
3060{
3061 return display->user_data;
3062}
3063
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003064struct wl_display *
3065display_get_display(struct display *display)
3066{
3067 return display->display;
3068}
3069
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003070struct output *
3071display_get_output(struct display *display)
3072{
3073 return container_of(display->output_list.next, struct output, link);
3074}
3075
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003076struct wl_compositor *
3077display_get_compositor(struct display *display)
3078{
3079 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003080}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003081
3082EGLDisplay
3083display_get_egl_display(struct display *d)
3084{
3085 return d->dpy;
3086}
3087
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003088struct wl_data_source *
3089display_create_data_source(struct display *display)
3090{
3091 return wl_data_device_manager_create_data_source(display->data_device_manager);
3092}
3093
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003094EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003095display_get_argb_egl_config(struct display *d)
3096{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003097 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003098}
3099
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003100struct wl_shell *
3101display_get_shell(struct display *display)
3102{
3103 return display->shell;
3104}
3105
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003106int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003107display_acquire_window_surface(struct display *display,
3108 struct window *window,
3109 EGLContext ctx)
3110{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003111#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003112 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003113 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003114
3115 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003116 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003117 device = cairo_surface_get_device(window->cairo_surface);
3118 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003119 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003120
Benjamin Franzke0c991632011-09-27 21:57:31 +02003121 if (!ctx) {
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003122 if (device == display->argb_device)
Benjamin Franzke0c991632011-09-27 21:57:31 +02003123 ctx = display->argb_ctx;
3124 else
3125 assert(0);
3126 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003127
3128 data = cairo_surface_get_user_data(window->cairo_surface,
3129 &surface_data_key);
3130
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003131 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003132 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003133 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3134 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003135
3136 return 0;
3137#else
3138 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003139#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003140}
3141
3142void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003143display_release_window_surface(struct display *display,
3144 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003145{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003146#ifdef HAVE_CAIRO_EGL
3147 cairo_device_t *device;
3148
3149 device = cairo_surface_get_device(window->cairo_surface);
3150 if (!device)
3151 return;
3152
Kristian Høgsberg067fd602012-02-29 16:15:53 -05003153 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003154 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003155 cairo_device_release(device);
3156#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003157}
3158
3159void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003160display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003161{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003162 wl_list_insert(&display->deferred_list, &task->link);
3163}
3164
3165void
3166display_watch_fd(struct display *display,
3167 int fd, uint32_t events, struct task *task)
3168{
3169 struct epoll_event ep;
3170
3171 ep.events = events;
3172 ep.data.ptr = task;
3173 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3174}
3175
3176void
3177display_run(struct display *display)
3178{
3179 struct task *task;
3180 struct epoll_event ep[16];
3181 int i, count;
3182
Pekka Paalanen826d7952011-12-15 10:14:07 +02003183 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003184 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003185 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003186
3187 while (!wl_list_empty(&display->deferred_list)) {
3188 task = container_of(display->deferred_list.next,
3189 struct task, link);
3190 wl_list_remove(&task->link);
3191 task->run(task, 0);
3192 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003193
3194 if (!display->running)
3195 break;
3196
3197 wl_display_flush(display->display);
3198
3199 count = epoll_wait(display->epoll_fd,
3200 ep, ARRAY_LENGTH(ep), -1);
3201 for (i = 0; i < count; i++) {
3202 task = ep[i].data.ptr;
3203 task->run(task, ep[i].events);
3204 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003205 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003206}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003207
3208void
3209display_exit(struct display *display)
3210{
3211 display->running = 0;
3212}