blob: c8c6ebea3e53ee58d44d000220e7e852242f5539 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040023#define _GNU_SOURCE
24
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025#include "../config.h"
26
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020034#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <time.h>
36#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050037#include <glib.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040038#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050042#include <wayland-egl.h>
43
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044#include <GL/gl.h>
45#include <EGL/egl.h>
46#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047
Kristian Høgsberg8def2642011-01-14 17:41:33 -050048#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040050#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051
Daniel Stone9d4f0302012-02-15 16:33:21 +000052#include <xkbcommon/xkbcommon.h>
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -050053#include <X11/X.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040054
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050055#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020056#include <wayland-client.h>
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040057#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050060
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050062 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050063 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040064 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040065 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_device_manager *data_device_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040067 EGLDisplay dpy;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -040068 EGLConfig rgb_config;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050069 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020070 EGLContext rgb_ctx;
71 EGLContext argb_ctx;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040074
75 int display_fd;
76 uint32_t mask;
77 struct task display_task;
78
79 int epoll_fd;
80 struct wl_list deferred_list;
81
Pekka Paalanen826d7952011-12-15 10:14:07 +020082 int running;
83
Kristian Høgsberg478d9262010-06-08 20:34:11 -040084 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040085 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -050086 struct wl_list output_list;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040087 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -050088 int frame_radius;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040089 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040090 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040091
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050092 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
93 PFNEGLCREATEIMAGEKHRPROC create_image;
94 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Pekka Paalanen999c5b52011-11-30 10:52:38 +020095
96 display_output_handler_t output_configure_handler;
97
98 void *user_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050099};
100
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400101enum {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400103 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500104 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500106 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400107 TYPE_CUSTOM
108};
109
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500110struct window {
111 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500112 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500113 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_shell_surface *shell_surface;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500115 struct wl_region *input_region;
116 struct wl_region *opaque_region;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500117 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500118 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500119 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500120 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400121 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400122 int redraw_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400123 struct task redraw_task;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500124 int resize_scheduled;
125 struct task resize_task;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400126 int type;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400127 int transparent;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 struct input *keyboard_device;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400129 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500130
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500131 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500132
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500133 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500134 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400135 window_data_handler_t data_handler;
136 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500137 window_close_handler_t close_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400138
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200139 struct frame *frame;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500140 struct widget *widget;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500142 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400143 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500144};
145
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500146struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500147 struct window *window;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500148 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400149 struct wl_list link;
150 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500151 widget_resize_handler_t resize_handler;
152 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500153 widget_enter_handler_t enter_handler;
154 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500155 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500156 widget_button_handler_t button_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400157 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500158 int opaque;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400159};
160
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400161struct input {
162 struct display *display;
163 struct wl_input_device *input_device;
164 struct window *pointer_focus;
165 struct window *keyboard_focus;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400166 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400167 uint32_t modifiers;
Pekka Paalanendfb93a92012-02-13 15:33:28 +0200168 int32_t sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400169 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400170
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500171 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500172 struct widget *grab;
173 uint32_t grab_button;
174
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400175 struct wl_data_device *data_device;
176 struct data_offer *drag_offer;
177 struct data_offer *selection_offer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400178};
179
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500180struct output {
181 struct display *display;
182 struct wl_output *output;
183 struct rectangle allocation;
184 struct wl_list link;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200185
186 display_output_handler_t destroy_handler;
187 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500188};
189
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500190struct frame {
191 struct widget *widget;
192 struct widget *child;
193 int margin;
194};
195
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500196struct menu {
197 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500198 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500199 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500200 const char **entries;
201 uint32_t time;
202 int current;
203 int count;
204 menu_func_t func;
205};
206
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400207enum {
208 POINTER_DEFAULT = 100,
209 POINTER_UNSET
210};
211
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500212enum window_location {
213 WINDOW_INTERIOR = 0,
214 WINDOW_RESIZING_TOP = 1,
215 WINDOW_RESIZING_BOTTOM = 2,
216 WINDOW_RESIZING_LEFT = 4,
217 WINDOW_RESIZING_TOP_LEFT = 5,
218 WINDOW_RESIZING_BOTTOM_LEFT = 6,
219 WINDOW_RESIZING_RIGHT = 8,
220 WINDOW_RESIZING_TOP_RIGHT = 9,
221 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
222 WINDOW_RESIZING_MASK = 15,
223 WINDOW_EXTERIOR = 16,
224 WINDOW_TITLEBAR = 17,
225 WINDOW_CLIENT_AREA = 18,
226};
227
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400228const char *option_xkb_layout = "us";
229const char *option_xkb_variant = "";
230const char *option_xkb_options = "";
231
232static const GOptionEntry xkb_option_entries[] = {
233 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
234 &option_xkb_layout, "XKB Layout" },
235 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
236 &option_xkb_variant, "XKB Variant" },
237 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
238 &option_xkb_options, "XKB Options" },
239 { NULL }
240};
241
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400242static const cairo_user_data_key_t surface_data_key;
243struct surface_data {
244 struct wl_buffer *buffer;
245};
246
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500247#define MULT(_d,c,a,t) \
248 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
249
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500250#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100252struct egl_window_surface_data {
253 struct display *display;
254 struct wl_surface *surface;
255 struct wl_egl_window *window;
256 EGLSurface surf;
257};
258
259static void
260egl_window_surface_data_destroy(void *p)
261{
262 struct egl_window_surface_data *data = p;
263 struct display *d = data->display;
264
265 eglDestroySurface(d->dpy, data->surf);
266 wl_egl_window_destroy(data->window);
267 data->surface = NULL;
268
269 free(p);
270}
271
272static cairo_surface_t *
273display_create_egl_window_surface(struct display *display,
274 struct wl_surface *surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400275 uint32_t flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100276 struct rectangle *rectangle)
277{
278 cairo_surface_t *cairo_surface;
279 struct egl_window_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400280 EGLConfig config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200281 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100282
283 data = malloc(sizeof *data);
284 if (data == NULL)
285 return NULL;
286
287 data->display = display;
288 data->surface = surface;
289
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400290 if (flags & SURFACE_OPAQUE) {
291 config = display->rgb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200292 device = display->rgb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400293 } else {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500294 config = display->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200295 device = display->argb_device;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400296 }
297
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
Benjamin Franzke0c991632011-09-27 21:57:31 +0200375 if (flags & SURFACE_OPAQUE) {
376 data->device = display->rgb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200377 content = CAIRO_CONTENT_COLOR;
378 } else {
379 data->device = display->argb_device;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200380 content = CAIRO_CONTENT_COLOR_ALPHA;
381 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400382
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500383 data->image = display->create_image(dpy, NULL,
384 EGL_NATIVE_PIXMAP_KHR,
385 (EGLClientBuffer) data->pixmap,
386 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500387 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500388 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500389 free(data);
390 return NULL;
391 }
392
393 data->data.buffer =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400394 wl_egl_pixmap_create_buffer(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500395
Benjamin Franzke0c991632011-09-27 21:57:31 +0200396 cairo_device_acquire(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400397 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400398 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500399 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200400 cairo_device_release(data->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
Benjamin Franzke0c991632011-09-27 21:57:31 +0200402 surface = cairo_gl_surface_create_for_texture(data->device,
403 content,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400404 data->texture,
405 rectangle->width,
406 rectangle->height);
407
408 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500409 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400410
411 return surface;
412}
413
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500414static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500415display_create_egl_image_surface_from_file(struct display *display,
416 const char *filename,
417 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400418{
419 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400420 GdkPixbuf *pixbuf;
421 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400422 int stride, i;
423 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500424 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400425
426 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400428 FALSE, &error);
429 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400430 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400431
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400432 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
433 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500434 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400435 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400436 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400437
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400438
439 stride = gdk_pixbuf_get_rowstride(pixbuf);
440 pixels = gdk_pixbuf_get_pixels(pixbuf);
441
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400442 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400443 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400444 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400445 while (p < end) {
446 unsigned int t;
447
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400448 MULT(p[0], p[0], p[3], t);
449 MULT(p[1], p[1], p[3], t);
450 MULT(p[2], p[2], p[3], t);
451 p += 4;
452
453 }
454 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400455
Benjamin Franzke4b87a132011-09-01 10:36:16 +0200456 surface = display_create_egl_image_surface(display, 0, rect);
nobled7b87cb02011-02-01 18:51:47 +0000457 if (surface == NULL) {
458 g_object_unref(pixbuf);
459 return NULL;
460 }
461
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800462 data = cairo_surface_get_user_data(surface, &surface_data_key);
463
Benjamin Franzke0c991632011-09-27 21:57:31 +0200464 cairo_device_acquire(display->argb_device);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800465 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800466 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
467 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200468 cairo_device_release(display->argb_device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400469
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500470 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400471
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400472 return surface;
473}
474
475#endif
476
477struct wl_buffer *
478display_get_buffer_for_surface(struct display *display,
479 cairo_surface_t *surface)
480{
481 struct surface_data *data;
482
483 data = cairo_surface_get_user_data (surface, &surface_data_key);
484
485 return data->buffer;
486}
487
488struct shm_surface_data {
489 struct surface_data data;
490 void *map;
491 size_t length;
492};
493
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500494static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400495shm_surface_data_destroy(void *p)
496{
497 struct shm_surface_data *data = p;
498
499 wl_buffer_destroy(data->data.buffer);
500 munmap(data->map, data->length);
501}
502
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500503static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504display_create_shm_surface(struct display *display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400505 struct rectangle *rectangle, uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400506{
507 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400508 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400509 cairo_surface_t *surface;
Bryce Harrington40269a62010-11-19 12:15:36 -0800510 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 char filename[] = "/tmp/wayland-shm-XXXXXX";
512
513 data = malloc(sizeof *data);
514 if (data == NULL)
515 return NULL;
516
517 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 rectangle->width);
519 data->length = stride * rectangle->height;
520 fd = mkstemp(filename);
521 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000522 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 return NULL;
524 }
525 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000526 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400527 close(fd);
528 return NULL;
529 }
530
531 data->map = mmap(NULL, data->length,
532 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
533 unlink(filename);
534
535 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000536 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400537 close(fd);
538 return NULL;
539 }
540
541 surface = cairo_image_surface_create_for_data (data->map,
542 CAIRO_FORMAT_ARGB32,
543 rectangle->width,
544 rectangle->height,
545 stride);
546
547 cairo_surface_set_user_data (surface, &surface_data_key,
548 data, shm_surface_data_destroy);
549
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400550 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500551 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400552 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500553 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400554
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400555 data->data.buffer = wl_shm_create_buffer(display->shm,
556 fd,
557 rectangle->width,
558 rectangle->height,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400559 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400560
561 close(fd);
562
563 return surface;
564}
565
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500566static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400567display_create_shm_surface_from_file(struct display *display,
568 const char *filename,
569 struct rectangle *rect)
570{
571 cairo_surface_t *surface;
572 GdkPixbuf *pixbuf;
573 GError *error = NULL;
574 int stride, i;
575 unsigned char *pixels, *p, *end, *dest_data;
576 int dest_stride;
577 uint32_t *d;
578
579 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
580 rect->width, rect->height,
581 FALSE, &error);
582 if (error != NULL)
583 return NULL;
584
585 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
586 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500587 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400588 return NULL;
589 }
590
591 stride = gdk_pixbuf_get_rowstride(pixbuf);
592 pixels = gdk_pixbuf_get_pixels(pixbuf);
593
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400594 surface = display_create_shm_surface(display, rect, 0);
nobled7b87cb02011-02-01 18:51:47 +0000595 if (surface == NULL) {
596 g_object_unref(pixbuf);
597 return NULL;
598 }
599
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400600 dest_data = cairo_image_surface_get_data (surface);
601 dest_stride = cairo_image_surface_get_stride (surface);
602
603 for (i = 0; i < rect->height; i++) {
604 d = (uint32_t *) (dest_data + i * dest_stride);
605 p = pixels + i * stride;
606 end = p + rect->width * 4;
607 while (p < end) {
608 unsigned int t;
609 unsigned char a, r, g, b;
610
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400611 a = p[3];
612 MULT(r, p[0], a, t);
613 MULT(g, p[1], a, t);
614 MULT(b, p[2], a, t);
615 p += 4;
616 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
617 }
618 }
619
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500620 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400621
622 return surface;
623}
624
nobled7b87cb02011-02-01 18:51:47 +0000625static int
626check_size(struct rectangle *rect)
627{
628 if (rect->width && rect->height)
629 return 0;
630
631 fprintf(stderr, "tried to create surface of "
632 "width: %d, height: %d\n", rect->width, rect->height);
633 return -1;
634}
635
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400636cairo_surface_t *
637display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400639 struct rectangle *rectangle,
640 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400641{
nobled7b87cb02011-02-01 18:51:47 +0000642 if (check_size(rectangle) < 0)
643 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500644#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500645 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646 if (surface)
647 return display_create_egl_window_surface(display,
648 surface,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400649 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650 rectangle);
651 else
652 return display_create_egl_image_surface(display,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400653 flags,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500655 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656#endif
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400657 return display_create_shm_surface(display, rectangle, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658}
659
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500660static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400661display_create_surface_from_file(struct display *display,
662 const char *filename,
663 struct rectangle *rectangle)
664{
nobled7b87cb02011-02-01 18:51:47 +0000665 if (check_size(rectangle) < 0)
666 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500667#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500668 if (display->dpy) {
669 return display_create_egl_image_surface_from_file(display,
670 filename,
671 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500672 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400673#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500674 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400675}
Yuval Fledel45568f62010-12-06 09:18:12 -0500676 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400677 const char *filename;
678 int hotspot_x, hotspot_y;
679} pointer_images[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500680 { DATADIR "/weston/bottom_left_corner.png", 6, 30 },
681 { DATADIR "/weston/bottom_right_corner.png", 28, 28 },
682 { DATADIR "/weston/bottom_side.png", 16, 20 },
683 { DATADIR "/weston/grabbing.png", 20, 17 },
684 { DATADIR "/weston/left_ptr.png", 10, 5 },
685 { DATADIR "/weston/left_side.png", 10, 20 },
686 { DATADIR "/weston/right_side.png", 30, 19 },
687 { DATADIR "/weston/top_left_corner.png", 8, 8 },
688 { DATADIR "/weston/top_right_corner.png", 26, 8 },
689 { DATADIR "/weston/top_side.png", 18, 8 },
690 { DATADIR "/weston/xterm.png", 15, 15 },
691 { DATADIR "/weston/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400692};
693
694static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400695create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400696{
697 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400698 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400699 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400700
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400701 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400702 display->pointer_surfaces =
703 malloc(count * sizeof *display->pointer_surfaces);
704 rect.width = width;
705 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400706 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400707 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400708 display_create_surface_from_file(display,
709 pointer_images[i].filename,
710 &rect);
Rob Bradford21223bf2011-10-25 12:19:36 +0100711 if (!display->pointer_surfaces[i]) {
712 fprintf(stderr, "Error loading pointer image: %s\n",
713 pointer_images[i].filename);
714 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400715 }
716
717}
718
Pekka Paalanen325bb602011-12-19 10:31:45 +0200719static void
720destroy_pointer_surfaces(struct display *display)
721{
722 int i, count;
723
724 count = ARRAY_LENGTH(pointer_images);
725 for (i = 0; i < count; ++i) {
726 if (display->pointer_surfaces[i])
727 cairo_surface_destroy(display->pointer_surfaces[i]);
728 }
729 free(display->pointer_surfaces);
730}
731
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400732cairo_surface_t *
733display_get_pointer_surface(struct display *display, int pointer,
734 int *width, int *height,
735 int *hotspot_x, int *hotspot_y)
736{
737 cairo_surface_t *surface;
738
739 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500740#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400741 *width = cairo_gl_surface_get_width(surface);
742 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000743#else
744 *width = cairo_image_surface_get_width(surface);
745 *height = cairo_image_surface_get_height(surface);
746#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400747 *hotspot_x = pointer_images[pointer].hotspot_x;
748 *hotspot_y = pointer_images[pointer].hotspot_y;
749
750 return cairo_surface_reference(surface);
751}
752
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400753static void
754window_attach_surface(struct window *window);
755
756static void
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400757free_surface(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400758{
759 struct window *window = data;
760
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400761 wl_callback_destroy(callback);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400762 cairo_surface_destroy(window->pending_surface);
763 window->pending_surface = NULL;
764 if (window->cairo_surface)
765 window_attach_surface(window);
766}
767
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400768static const struct wl_callback_listener free_surface_listener = {
769 free_surface
770};
771
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500772static void
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200773window_get_resize_dx_dy(struct window *window, int *x, int *y)
774{
775 if (window->resize_edges & WINDOW_RESIZING_LEFT)
776 *x = window->server_allocation.width - window->allocation.width;
777 else
778 *x = 0;
779
780 if (window->resize_edges & WINDOW_RESIZING_TOP)
781 *y = window->server_allocation.height -
782 window->allocation.height;
783 else
784 *y = 0;
785
786 window->resize_edges = 0;
787}
788
789static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500790window_attach_surface(struct window *window)
791{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400792 struct display *display = window->display;
793 struct wl_buffer *buffer;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400794 struct wl_callback *cb;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000795#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100796 struct egl_window_surface_data *data;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000797#endif
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500798 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100799
800 switch (window->buffer_type) {
Benjamin Franzke22d54812011-07-16 19:50:32 +0000801#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100802 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
803 data = cairo_surface_get_user_data(window->cairo_surface,
804 &surface_data_key);
805
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100806 cairo_gl_surface_swapbuffers(window->cairo_surface);
807 wl_egl_window_get_attached_size(data->window,
808 &window->server_allocation.width,
809 &window->server_allocation.height);
810 break;
811 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Benjamin Franzke22d54812011-07-16 19:50:32 +0000812#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100813 case WINDOW_BUFFER_TYPE_SHM:
814 if (window->pending_surface != NULL)
815 return;
816
817 window->pending_surface = window->cairo_surface;
818 window->cairo_surface = NULL;
819
820 buffer =
821 display_get_buffer_for_surface(display,
822 window->pending_surface);
823
Ander Conselvan de Oliveirae018b042012-01-27 17:17:39 +0200824 window_get_resize_dx_dy(window, &x, &y);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100825 wl_surface_attach(window->surface, buffer, x, y);
826 window->server_allocation = window->allocation;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -0400827 cb = wl_display_sync(display->display);
828 wl_callback_add_listener(cb, &free_surface_listener, window);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100829 break;
Benjamin Franzke22d54812011-07-16 19:50:32 +0000830 default:
831 return;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100832 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500833
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500834 wl_surface_set_input_region(window->surface, window->input_region);
835 wl_surface_set_opaque_region(window->surface, window->opaque_region);
836
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500837 wl_surface_damage(window->surface, 0, 0,
838 window->allocation.width,
839 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500840}
841
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500842void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400843window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500844{
Benjamin Franzkebde55ec2011-03-07 15:08:09 +0100845 if (window->cairo_surface) {
846 switch (window->buffer_type) {
847 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
848 case WINDOW_BUFFER_TYPE_SHM:
849 display_surface_damage(window->display,
850 window->cairo_surface,
851 0, 0,
852 window->allocation.width,
853 window->allocation.height);
854 break;
855 default:
856 break;
857 }
858 window_attach_surface(window);
859 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500860}
861
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400862void
863window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400864{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500865 cairo_surface_reference(surface);
866
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400867 if (window->cairo_surface != NULL)
868 cairo_surface_destroy(window->cairo_surface);
869
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500870 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400871}
872
Benjamin Franzke22d54812011-07-16 19:50:32 +0000873#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100874static void
875window_resize_cairo_window_surface(struct window *window)
876{
877 struct egl_window_surface_data *data;
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200878 int x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100879
880 data = cairo_surface_get_user_data(window->cairo_surface,
881 &surface_data_key);
882
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200883 window_get_resize_dx_dy(window, &x, &y),
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100884 wl_egl_window_resize(data->window,
885 window->allocation.width,
Benjamin Franzke14f7ff92011-06-23 12:10:51 +0200886 window->allocation.height,
887 x,y);
888
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889 cairo_gl_surface_set_size(window->cairo_surface,
890 window->allocation.width,
891 window->allocation.height);
892}
Benjamin Franzke22d54812011-07-16 19:50:32 +0000893#endif
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400895struct display *
896window_get_display(struct window *window)
897{
898 return window->display;
899}
900
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400901void
902window_create_surface(struct window *window)
903{
904 cairo_surface_t *surface;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400905 uint32_t flags = 0;
906
907 if (!window->transparent)
908 flags = SURFACE_OPAQUE;
909
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400910 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500911#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
913 if (window->cairo_surface) {
914 window_resize_cairo_window_surface(window);
915 return;
916 }
917 surface = display_create_surface(window->display,
918 window->surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400919 &window->allocation, flags);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100920 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500921 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400922 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100923 NULL,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400924 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400925 break;
926#endif
927 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400928 surface = display_create_shm_surface(window->display,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400929 &window->allocation, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400930 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800931 default:
932 surface = NULL;
933 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400934 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400935
936 window_set_surface(window, surface);
937 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938}
939
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200940static void frame_destroy(struct frame *frame);
941
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400942void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500943window_destroy(struct window *window)
944{
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200945 struct display *display = window->display;
946 struct input *input;
947
948 if (window->redraw_scheduled)
949 wl_list_remove(&window->redraw_task.link);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500950 if (window->resize_scheduled)
951 wl_list_remove(&window->resize_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200952
953 wl_list_for_each(input, &display->input_list, link) {
954 if (input->pointer_focus == window)
955 input->pointer_focus = NULL;
956 if (input->keyboard_focus == window)
957 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -0500958 if (input->focus_widget &&
959 input->focus_widget->window == window)
960 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +0200961 }
962
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500963 if (window->input_region)
964 wl_region_destroy(window->input_region);
965 if (window->opaque_region)
966 wl_region_destroy(window->opaque_region);
967
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200968 if (window->frame)
969 frame_destroy(window->frame);
970
Pekka Paalanen6b2dc912011-11-29 10:25:08 +0200971 if (window->shell_surface)
972 wl_shell_surface_destroy(window->shell_surface);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500973 wl_surface_destroy(window->surface);
974 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +0200975
976 if (window->cairo_surface != NULL)
977 cairo_surface_destroy(window->cairo_surface);
978 if (window->pending_surface != NULL)
979 cairo_surface_destroy(window->pending_surface);
980
981 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500982 free(window);
983}
984
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500985static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500986widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400987{
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500988 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400989
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500990 wl_list_for_each(child, &widget->child_list, link) {
991 target = widget_find_widget(child, x, y);
992 if (target)
993 return target;
994 }
995
996 if (widget->allocation.x <= x &&
997 x < widget->allocation.x + widget->allocation.width &&
998 widget->allocation.y <= y &&
999 y < widget->allocation.y + widget->allocation.height) {
1000 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001001 }
1002
1003 return NULL;
1004}
1005
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001006static struct widget *
1007widget_create(struct window *window, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001008{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001009 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001010
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001011 widget = malloc(sizeof *widget);
1012 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001013 widget->window = window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001014 widget->user_data = data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05001015 widget->allocation = window->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001016 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001017 widget->opaque = 0;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001018
1019 return widget;
1020}
1021
1022struct widget *
1023window_add_widget(struct window *window, void *data)
1024{
1025 window->widget = widget_create(window, data);
1026 wl_list_init(&window->widget->link);
1027
1028 return window->widget;
1029}
1030
1031struct widget *
1032widget_add_widget(struct widget *parent, void *data)
1033{
1034 struct widget *widget;
1035
1036 widget = widget_create(parent->window, data);
1037 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001038
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001039 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001040}
1041
1042void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001043widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001044{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001045 struct display *display = widget->window->display;
1046 struct input *input;
1047
1048 wl_list_for_each(input, &display->input_list, link) {
1049 if (input->focus_widget == widget)
1050 input->focus_widget = NULL;
1051 }
1052
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001053 wl_list_remove(&widget->link);
1054 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001055}
1056
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001057void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001058widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001059{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001060 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001061}
1062
1063void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001064widget_set_size(struct widget *widget, int32_t width, int32_t height)
1065{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001066 widget->allocation.width = width;
1067 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001068}
1069
1070void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001071widget_set_allocation(struct widget *widget,
1072 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001073{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001074 widget->allocation.x = x;
1075 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001076 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001077}
1078
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001079void
1080widget_set_transparent(struct widget *widget, int transparent)
1081{
1082 widget->opaque = !transparent;
1083}
1084
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001085void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001086widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001087{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001088 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001089}
1090
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001091void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001092widget_set_resize_handler(struct widget *widget,
1093 widget_resize_handler_t handler)
1094{
1095 widget->resize_handler = handler;
1096}
1097
1098void
1099widget_set_redraw_handler(struct widget *widget,
1100 widget_redraw_handler_t handler)
1101{
1102 widget->redraw_handler = handler;
1103}
1104
1105void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001106widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001107{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001108 widget->enter_handler = handler;
1109}
1110
1111void
1112widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1113{
1114 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001115}
1116
1117void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001118widget_set_motion_handler(struct widget *widget,
1119 widget_motion_handler_t handler)
1120{
1121 widget->motion_handler = handler;
1122}
1123
1124void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001125widget_set_button_handler(struct widget *widget,
1126 widget_button_handler_t handler)
1127{
1128 widget->button_handler = handler;
1129}
1130
1131void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001132widget_schedule_redraw(struct widget *widget)
1133{
1134 window_schedule_redraw(widget->window);
1135}
1136
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001137cairo_surface_t *
1138window_get_surface(struct window *window)
1139{
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001140 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001141}
1142
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001143struct wl_surface *
1144window_get_wl_surface(struct window *window)
1145{
1146 return window->surface;
1147}
1148
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001149struct wl_shell_surface *
1150window_get_wl_shell_surface(struct window *window)
1151{
1152 return window->shell_surface;
1153}
1154
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001155static void
1156frame_resize_handler(struct widget *widget,
1157 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001158{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001159 struct frame *frame = data;
1160 struct widget *child = frame->child;
1161 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001162 struct display *display = widget->window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001163 int decoration_width, decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001164 int opaque_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001165
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001166 if (widget->window->type == TYPE_TOPLEVEL) {
1167 decoration_width = 20 + frame->margin * 2;
1168 decoration_height = 60 + frame->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001169
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001170 allocation.x = 10 + frame->margin;
1171 allocation.y = 50 + frame->margin;
1172 allocation.width = width - decoration_width;
1173 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001174
1175 widget->window->input_region =
1176 wl_compositor_create_region(display->compositor);
1177 wl_region_add(widget->window->input_region,
1178 frame->margin, frame->margin,
1179 width - 2 * frame->margin,
1180 height - 2 * frame->margin);
1181
1182 opaque_margin = frame->margin + display->frame_radius;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001183 } else {
1184 decoration_width = 0;
1185 decoration_height = 0;
1186
1187 allocation.x = 0;
1188 allocation.y = 0;
1189 allocation.width = width;
1190 allocation.height = height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001191 opaque_margin = 0;
1192 }
1193
1194 if (child->opaque) {
1195 widget->window->opaque_region =
1196 wl_compositor_create_region(display->compositor);
1197 wl_region_add(widget->window->opaque_region,
1198 opaque_margin, opaque_margin,
1199 width - 2 * opaque_margin,
1200 height - 2 * opaque_margin);
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001201 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001202
1203 widget_set_allocation(child, allocation.x, allocation.y,
1204 allocation.width, allocation.height);
1205
1206 if (child->resize_handler)
1207 child->resize_handler(child,
1208 allocation.width,
1209 allocation.height,
1210 child->user_data);
1211
1212 widget_set_allocation(widget, 0, 0,
1213 child->allocation.width + decoration_width,
1214 child->allocation.height + decoration_height);
1215}
1216
1217static void
1218frame_redraw_handler(struct widget *widget, void *data)
1219{
1220 struct frame *frame = data;
1221 cairo_t *cr;
1222 cairo_text_extents_t extents;
1223 cairo_surface_t *source;
1224 int width, height, shadow_dx = 3, shadow_dy = 3;
1225 struct window *window = widget->window;
1226
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05001227 if (window->type == TYPE_FULLSCREEN)
1228 return;
1229
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001230 width = widget->allocation.width;
1231 height = widget->allocation.height;
1232
1233 cr = cairo_create(window->cairo_surface);
1234
1235 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1236 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1237 cairo_paint(cr);
1238
1239 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
1240 tile_mask(cr, window->display->shadow,
1241 shadow_dx, shadow_dy, width, height,
1242 frame->margin + 10 - shadow_dx,
1243 frame->margin + 10 - shadow_dy);
1244
1245 if (window->keyboard_device)
1246 source = window->display->active_frame;
1247 else
1248 source = window->display->inactive_frame;
1249
1250 tile_source(cr, source, 0, 0, width, height,
1251 frame->margin + 10, frame->margin + 50);
1252
1253 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1254 cairo_set_font_size(cr, 14);
1255 cairo_text_extents(cr, window->title, &extents);
1256 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
1257 if (window->keyboard_device)
1258 cairo_set_source_rgb(cr, 0, 0, 0);
1259 else
1260 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1261 cairo_show_text(cr, window->title);
1262
1263 cairo_destroy(cr);
1264}
1265
1266static int
1267frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1268{
1269 struct widget *widget = frame->widget;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001270 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001271 const int grip_size = 8;
1272
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001273 if (x < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001274 hlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001275 else if (frame->margin <= x && x < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001276 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001277 else if (x < widget->allocation.width - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278 hlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001279 else if (x < widget->allocation.width - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001280 hlocation = WINDOW_RESIZING_RIGHT;
1281 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001282 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001283
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001284 if (y < frame->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001285 vlocation = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001286 else if (frame->margin <= y && y < frame->margin + grip_size)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001287 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001288 else if (y < widget->allocation.height - frame->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001289 vlocation = WINDOW_INTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001290 else if (y < widget->allocation.height - frame->margin)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001291 vlocation = WINDOW_RESIZING_BOTTOM;
1292 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001293 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001294
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001295 location = vlocation | hlocation;
1296 if (location & WINDOW_EXTERIOR)
1297 location = WINDOW_EXTERIOR;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001298 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001299 location = WINDOW_TITLEBAR;
1300 else if (location == WINDOW_INTERIOR)
1301 location = WINDOW_CLIENT_AREA;
1302
1303 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001304}
1305
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001306static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001307frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001308{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001309 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001310
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001311 location = frame_get_pointer_location(frame, input->sx, input->sy);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001312 switch (location) {
1313 case WINDOW_RESIZING_TOP:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001314 return POINTER_TOP;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001315 case WINDOW_RESIZING_BOTTOM:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001316 return POINTER_BOTTOM;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001317 case WINDOW_RESIZING_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001318 return POINTER_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001319 case WINDOW_RESIZING_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001320 return POINTER_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321 case WINDOW_RESIZING_TOP_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001322 return POINTER_TOP_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001323 case WINDOW_RESIZING_TOP_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001324 return POINTER_TOP_RIGHT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001325 case WINDOW_RESIZING_BOTTOM_LEFT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001326 return POINTER_BOTTOM_LEFT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001327 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001328 return POINTER_BOTTOM_RIGHT;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001329 case WINDOW_EXTERIOR:
1330 case WINDOW_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001331 default:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001332 return POINTER_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001333 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001334}
1335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001336static void
1337frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001338{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001339 switch (index) {
1340 case 0: /* close */
1341 if (window->close_handler)
1342 window->close_handler(window->parent,
1343 window->user_data);
1344 else
1345 display_exit(window->display);
1346 break;
1347 case 1: /* fullscreen */
1348 /* we don't have a way to get out of fullscreen for now */
1349 window_set_fullscreen(window, 1);
1350 break;
1351 case 2: /* rotate */
1352 case 3: /* scale */
1353 break;
1354 }
1355}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001356
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001357void
1358window_show_frame_menu(struct window *window,
1359 struct input *input, uint32_t time)
1360{
1361 int32_t x, y;
1362
1363 static const char *entries[] = {
1364 "Close", "Fullscreen", "Rotate", "Scale"
1365 };
1366
1367 input_get_position(input, &x, &y);
1368 window_show_menu(window->display, input, time, window,
1369 x - 10, y - 10, frame_menu_func, entries, 4);
1370}
1371
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001372static int
1373frame_enter_handler(struct widget *widget,
1374 struct input *input, uint32_t time,
1375 int32_t x, int32_t y, void *data)
1376{
1377 return frame_get_pointer_image_for_location(data, input);
1378}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001379
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001380static int
1381frame_motion_handler(struct widget *widget,
1382 struct input *input, uint32_t time,
1383 int32_t x, int32_t y, void *data)
1384{
1385 return frame_get_pointer_image_for_location(data, input);
1386}
Rob Bradford8bd35c72011-10-25 12:20:51 +01001387
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001388static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001389frame_button_handler(struct widget *widget,
1390 struct input *input, uint32_t time,
1391 int button, int state, void *data)
1392
1393{
1394 struct frame *frame = data;
1395 struct window *window = widget->window;
1396 int location;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001397 location = frame_get_pointer_location(frame, input->sx, input->sy);
1398
1399 if (window->display->shell && button == BTN_LEFT && state == 1) {
1400 switch (location) {
1401 case WINDOW_TITLEBAR:
1402 if (!window->shell_surface)
1403 break;
1404 input_set_pointer_image(input, time, POINTER_DRAGGING);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001405 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001406 wl_shell_surface_move(window->shell_surface,
1407 input_get_input_device(input),
1408 time);
1409 break;
1410 case WINDOW_RESIZING_TOP:
1411 case WINDOW_RESIZING_BOTTOM:
1412 case WINDOW_RESIZING_LEFT:
1413 case WINDOW_RESIZING_RIGHT:
1414 case WINDOW_RESIZING_TOP_LEFT:
1415 case WINDOW_RESIZING_TOP_RIGHT:
1416 case WINDOW_RESIZING_BOTTOM_LEFT:
1417 case WINDOW_RESIZING_BOTTOM_RIGHT:
1418 if (!window->shell_surface)
1419 break;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001420 input_ungrab(input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001421 wl_shell_surface_resize(window->shell_surface,
1422 input_get_input_device(input),
1423 time, location);
1424 break;
1425 }
1426 } else if (button == BTN_RIGHT && state == 1) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05001427 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001428 }
1429}
1430
1431struct widget *
1432frame_create(struct window *window, void *data)
1433{
1434 struct frame *frame;
1435
1436 frame = malloc(sizeof *frame);
1437 memset(frame, 0, sizeof *frame);
1438
1439 frame->widget = window_add_widget(window, frame);
1440 frame->child = widget_add_widget(frame->widget, data);
1441 frame->margin = 16;
1442
1443 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1444 widget_set_resize_handler(frame->widget, frame_resize_handler);
1445 widget_set_enter_handler(frame->widget, frame_enter_handler);
1446 widget_set_motion_handler(frame->widget, frame_motion_handler);
1447 widget_set_button_handler(frame->widget, frame_button_handler);
1448
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001449 window->frame = frame;
1450
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001451 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001452}
1453
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001454static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001455frame_destroy(struct frame *frame)
1456{
1457 /* frame->child must be destroyed by the application */
1458 widget_destroy(frame->widget);
1459 free(frame);
1460}
1461
1462static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001463input_set_focus_widget(struct input *input, struct widget *focus,
1464 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001465{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001466 struct widget *old, *widget;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001467 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001468
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001469 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001470 return;
1471
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001472 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001473 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001474 widget = old;
1475 if (input->grab)
1476 widget = input->grab;
1477 if (widget->leave_handler)
1478 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001479 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05001480 }
1481
1482 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001483 widget = focus;
1484 if (input->grab)
1485 widget = input->grab;
1486 if (widget->enter_handler)
1487 pointer = widget->enter_handler(focus, input, time,
1488 x, y,
1489 widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001490 input->focus_widget = focus;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001491
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05001492 input_set_pointer_image(input, time, pointer);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001493 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001494}
1495
1496static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001497input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001498 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001499{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001500 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001501 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001502 struct widget *widget;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001503 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001504
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001505 input->sx = sx;
1506 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001507
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001508 if (!(input->grab && input->grab_button)) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001509 widget = widget_find_widget(window->widget, sx, sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001510 input_set_focus_widget(input, widget, time, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001511 }
1512
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001513 if (input->grab)
1514 widget = input->grab;
1515 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001516 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001517 if (widget && widget->motion_handler)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001518 pointer = widget->motion_handler(input->focus_widget,
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001519 input, time, sx, sy,
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001520 widget->user_data);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001521
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001522 input_set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001523}
1524
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001525void
1526input_grab(struct input *input, struct widget *widget, uint32_t button)
1527{
1528 input->grab = widget;
1529 input->grab_button = button;
1530}
1531
1532void
1533input_ungrab(struct input *input, uint32_t time)
1534{
1535 struct widget *widget;
1536
1537 input->grab = NULL;
1538 if (input->pointer_focus) {
1539 widget = widget_find_widget(input->pointer_focus->widget,
1540 input->sx, input->sy);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001541 input_set_focus_widget(input, widget,
1542 time, input->sx, input->sy);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001543 }
1544}
1545
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001546static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001547input_handle_button(void *data,
1548 struct wl_input_device *input_device,
1549 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001550{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001551 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001552 struct widget *widget;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001553
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001554 if (input->focus_widget && input->grab == NULL && state)
1555 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001556
Neil Roberts6b28aad2012-01-23 19:11:18 +00001557 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001558 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00001559 (*widget->button_handler)(widget,
1560 input, time,
1561 button, state,
1562 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001563
Kristian Høgsberg831dd522012-01-10 23:46:33 -05001564 if (input->grab && input->grab_button == button && !state)
1565 input_ungrab(input, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001566}
1567
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001568static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001569input_handle_key(void *data, struct wl_input_device *input_device,
1570 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001571{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001572 struct input *input = data;
1573 struct window *window = input->keyboard_focus;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001574 struct display *d = input->display;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001575 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001576
Daniel Stone0d5a5092012-02-16 12:48:00 +00001577 code = key + 8;
Pekka Paalanena03a93c2011-11-28 16:13:57 +02001578 if (!window || window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001579 return;
1580
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001581 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001582 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001583 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1584 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001585
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001586 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1587
1588 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001589 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001590 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001591 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001592
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05001593 if (key == KEY_F5 && input->modifiers == Mod4Mask) {
1594 if (state)
1595 window_set_maximized(window,
1596 window->type != TYPE_MAXIMIZED);
1597 } else if (window->key_handler) {
1598 (*window->key_handler)(window, input, time, key,
1599 sym, state, window->user_data);
1600 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001601}
1602
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001603static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001604input_remove_pointer_focus(struct input *input, uint32_t time)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001605{
1606 struct window *window = input->pointer_focus;
1607
1608 if (!window)
1609 return;
1610
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001611 input_set_focus_widget(input, NULL, 0, 0, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02001612
Pekka Paalanene1207c72011-12-16 12:02:09 +02001613 input->pointer_focus = NULL;
1614 input->current_pointer_image = POINTER_UNSET;
1615}
1616
1617static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001618input_handle_pointer_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001619 struct wl_input_device *input_device,
1620 uint32_t time, struct wl_surface *surface,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001621 int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001622{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001623 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001624 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001625 struct widget *widget;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001626
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001627 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001628 window = input->pointer_focus;
Kristian Høgsberg900b2262011-09-06 14:33:52 -04001629
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001630 input->sx = sx;
1631 input->sy = sy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001632
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001633 widget = widget_find_widget(window->widget, sx, sy);
1634 input_set_focus_widget(input, widget, time, sx, sy);
1635}
Kristian Høgsberg59826582011-01-20 11:56:57 -05001636
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001637static void
1638input_handle_pointer_leave(void *data,
1639 struct wl_input_device *input_device,
1640 uint32_t time, struct wl_surface *surface)
1641{
1642 struct input *input = data;
1643
1644 input_remove_pointer_focus(input, time);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001645}
1646
1647static void
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001648input_remove_keyboard_focus(struct input *input)
Pekka Paalanene1207c72011-12-16 12:02:09 +02001649{
1650 struct window *window = input->keyboard_focus;
1651
1652 if (!window)
1653 return;
1654
1655 window->keyboard_device = NULL;
1656 if (window->keyboard_focus_handler)
1657 (*window->keyboard_focus_handler)(window, NULL,
1658 window->user_data);
1659
1660 input->keyboard_focus = NULL;
1661}
1662
1663static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001664input_handle_keyboard_enter(void *data,
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001665 struct wl_input_device *input_device,
1666 uint32_t time,
1667 struct wl_surface *surface,
1668 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001669{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001670 struct input *input = data;
Pekka Paalanene1207c72011-12-16 12:02:09 +02001671 struct window *window;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001672 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001673 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001674
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001675 input->keyboard_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001676
1677 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001678 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001679 for (k = keys->data; k < end; k++)
1680 input->modifiers |= d->xkb->map->modmap[*k];
1681
1682 window = input->keyboard_focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001683 window->keyboard_device = input;
1684 if (window->keyboard_focus_handler)
1685 (*window->keyboard_focus_handler)(window,
1686 window->keyboard_device,
1687 window->user_data);
1688}
1689
1690static void
1691input_handle_keyboard_leave(void *data,
1692 struct wl_input_device *input_device,
1693 uint32_t time,
1694 struct wl_surface *surface)
1695{
1696 struct input *input = data;
1697
1698 input_remove_keyboard_focus(input);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001699}
1700
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001701static void
1702input_handle_touch_down(void *data,
1703 struct wl_input_device *wl_input_device,
1704 uint32_t time, struct wl_surface *surface,
1705 int32_t id, int32_t x, int32_t y)
1706{
1707}
1708
1709static void
1710input_handle_touch_up(void *data,
1711 struct wl_input_device *wl_input_device,
1712 uint32_t time, int32_t id)
1713{
1714}
1715
1716static void
1717input_handle_touch_motion(void *data,
1718 struct wl_input_device *wl_input_device,
1719 uint32_t time, int32_t id, int32_t x, int32_t y)
1720{
1721}
1722
1723static void
1724input_handle_touch_frame(void *data,
1725 struct wl_input_device *wl_input_device)
1726{
1727}
1728
1729static void
1730input_handle_touch_cancel(void *data,
1731 struct wl_input_device *wl_input_device)
1732{
1733}
1734
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001735static const struct wl_input_device_listener input_device_listener = {
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05001736 input_handle_motion,
1737 input_handle_button,
1738 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001739 input_handle_pointer_enter,
1740 input_handle_pointer_leave,
1741 input_handle_keyboard_enter,
1742 input_handle_keyboard_leave,
Kristian Høgsberge04ad572011-12-21 17:14:54 -05001743 input_handle_touch_down,
1744 input_handle_touch_up,
1745 input_handle_touch_motion,
1746 input_handle_touch_frame,
1747 input_handle_touch_cancel,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001748};
1749
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001750void
1751input_get_position(struct input *input, int32_t *x, int32_t *y)
1752{
1753 *x = input->sx;
1754 *y = input->sy;
1755}
1756
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001757struct wl_input_device *
1758input_get_input_device(struct input *input)
1759{
1760 return input->input_device;
1761}
1762
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001763uint32_t
1764input_get_modifiers(struct input *input)
1765{
1766 return input->modifiers;
1767}
1768
Kristian Høgsbergb6323512012-01-11 00:04:42 -05001769struct widget *
1770input_get_focus_widget(struct input *input)
1771{
1772 return input->focus_widget;
1773}
1774
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001775struct data_offer {
1776 struct wl_data_offer *offer;
1777 struct input *input;
1778 struct wl_array types;
1779 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001780
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001781 struct task io_task;
1782 int fd;
1783 data_func_t func;
1784 int32_t x, y;
1785 void *user_data;
1786};
1787
1788static void
1789data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1790{
1791 struct data_offer *offer = data;
1792 char **p;
1793
1794 p = wl_array_add(&offer->types, sizeof *p);
1795 *p = strdup(type);
1796}
1797
1798static const struct wl_data_offer_listener data_offer_listener = {
1799 data_offer_offer,
1800};
1801
1802static void
1803data_offer_destroy(struct data_offer *offer)
1804{
1805 char **p;
1806
1807 offer->refcount--;
1808 if (offer->refcount == 0) {
1809 wl_data_offer_destroy(offer->offer);
1810 for (p = offer->types.data; *p; p++)
1811 free(*p);
1812 wl_array_release(&offer->types);
1813 free(offer);
1814 }
1815}
1816
1817static void
1818data_device_data_offer(void *data,
1819 struct wl_data_device *data_device, uint32_t id)
1820{
1821 struct data_offer *offer;
1822
1823 offer = malloc(sizeof *offer);
1824
1825 wl_array_init(&offer->types);
1826 offer->refcount = 1;
1827 offer->input = data;
1828
1829 /* FIXME: Generate typesafe wrappers for this */
1830 offer->offer = (struct wl_data_offer *)
1831 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1832 id, &wl_data_offer_interface);
1833
1834 wl_data_offer_add_listener(offer->offer,
1835 &data_offer_listener, offer);
1836}
1837
1838static void
1839data_device_enter(void *data, struct wl_data_device *data_device,
1840 uint32_t time, struct wl_surface *surface,
1841 int32_t x, int32_t y, struct wl_data_offer *offer)
1842{
1843 struct input *input = data;
1844 struct window *window;
1845 char **p;
1846
1847 input->drag_offer = wl_data_offer_get_user_data(offer);
1848 window = wl_surface_get_user_data(surface);
1849 input->pointer_focus = window;
1850
1851 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1852 *p = NULL;
1853
1854 window = input->pointer_focus;
1855 if (window->data_handler)
1856 window->data_handler(window, input, time, x, y,
1857 input->drag_offer->types.data,
1858 window->user_data);
1859}
1860
1861static void
1862data_device_leave(void *data, struct wl_data_device *data_device)
1863{
1864 struct input *input = data;
1865
1866 data_offer_destroy(input->drag_offer);
1867 input->drag_offer = NULL;
1868}
1869
1870static void
1871data_device_motion(void *data, struct wl_data_device *data_device,
1872 uint32_t time, int32_t x, int32_t y)
1873{
1874 struct input *input = data;
1875 struct window *window = input->pointer_focus;
1876
1877 input->sx = x;
1878 input->sy = y;
1879
1880 if (window->data_handler)
1881 window->data_handler(window, input, time, x, y,
1882 input->drag_offer->types.data,
1883 window->user_data);
1884}
1885
1886static void
1887data_device_drop(void *data, struct wl_data_device *data_device)
1888{
1889 struct input *input = data;
1890 struct window *window = input->pointer_focus;
1891
1892 if (window->drop_handler)
1893 window->drop_handler(window, input,
1894 input->sx, input->sy, window->user_data);
1895}
1896
1897static void
1898data_device_selection(void *data,
1899 struct wl_data_device *wl_data_device,
1900 struct wl_data_offer *offer)
1901{
1902 struct input *input = data;
1903 char **p;
1904
1905 if (input->selection_offer)
1906 data_offer_destroy(input->selection_offer);
1907
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05001908 if (offer) {
1909 input->selection_offer = wl_data_offer_get_user_data(offer);
1910 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1911 *p = NULL;
1912 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001913}
1914
1915static const struct wl_data_device_listener data_device_listener = {
1916 data_device_data_offer,
1917 data_device_enter,
1918 data_device_leave,
1919 data_device_motion,
1920 data_device_drop,
1921 data_device_selection
1922};
1923
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001924void
1925input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1926{
1927 struct display *display = input->display;
1928 struct wl_buffer *buffer;
1929 cairo_surface_t *surface;
1930
1931 if (pointer == input->current_pointer_image)
1932 return;
1933
1934 input->current_pointer_image = pointer;
1935 surface = display->pointer_surfaces[pointer];
1936
1937 if (!surface)
1938 return;
1939
1940 buffer = display_get_buffer_for_surface(display, surface);
1941 wl_input_device_attach(input->input_device, time, buffer,
1942 pointer_images[pointer].hotspot_x,
1943 pointer_images[pointer].hotspot_y);
1944}
1945
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04001946struct wl_data_device *
1947input_get_data_device(struct input *input)
1948{
1949 return input->data_device;
1950}
1951
1952void
1953input_set_selection(struct input *input,
1954 struct wl_data_source *source, uint32_t time)
1955{
1956 wl_data_device_set_selection(input->data_device, source, time);
1957}
1958
1959void
1960input_accept(struct input *input, uint32_t time, const char *type)
1961{
1962 wl_data_offer_accept(input->drag_offer->offer, time, type);
1963}
1964
1965static void
1966offer_io_func(struct task *task, uint32_t events)
1967{
1968 struct data_offer *offer =
1969 container_of(task, struct data_offer, io_task);
1970 unsigned int len;
1971 char buffer[4096];
1972
1973 len = read(offer->fd, buffer, sizeof buffer);
1974 offer->func(buffer, len,
1975 offer->x, offer->y, offer->user_data);
1976
1977 if (len == 0) {
1978 close(offer->fd);
1979 data_offer_destroy(offer);
1980 }
1981}
1982
1983static void
1984data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1985 data_func_t func, void *user_data)
1986{
1987 int p[2];
1988
1989 pipe2(p, O_CLOEXEC);
1990 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øgsberg2ad3b7f2012-01-31 15:34:15 -05002053idle_resize(struct task *task, uint32_t events)
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002054{
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002055 struct window *window =
2056 container_of(task, struct window, resize_task);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002057 struct widget *widget;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002058
Kristian Høgsberg2ad3b7f2012-01-31 15:34:15 -05002059 window->resize_scheduled = 0;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002060 widget = window->widget;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002061 widget_set_allocation(widget,
2062 window->pending_allocation.x,
2063 window->pending_allocation.y,
2064 window->pending_allocation.width,
2065 window->pending_allocation.height);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002066
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002067 if (window->input_region) {
2068 wl_region_destroy(window->input_region);
2069 window->input_region = NULL;
2070 }
2071
2072 if (window->opaque_region) {
2073 wl_region_destroy(window->opaque_region);
2074 window->opaque_region = NULL;
2075 }
2076
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002077 if (widget->resize_handler)
2078 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002079 widget->allocation.width,
2080 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002081 widget->user_data);
2082
Kristian Høgsberg8e054f72012-01-31 11:53:20 -05002083 if (window->allocation.width != widget->allocation.width ||
2084 window->allocation.height != widget->allocation.height) {
2085 window->allocation = widget->allocation;
2086 window_schedule_redraw(window);
2087 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002088}
2089
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002090void
2091window_schedule_resize(struct window *window, int width, int height)
2092{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002093 window->pending_allocation.x = 0;
2094 window->pending_allocation.y = 0;
2095 window->pending_allocation.width = width;
2096 window->pending_allocation.height = height;
2097
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002098 if (!window->resize_scheduled) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002099 window->resize_task.run = idle_resize;
2100 display_defer(window->display, &window->resize_task);
2101 window->resize_scheduled = 1;
2102 }
2103}
2104
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002105void
2106widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2107{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002108 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05002109}
2110
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002111static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002112handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002113 uint32_t time, uint32_t edges,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002114 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002115{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002117
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002118 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01002119 return;
2120
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01002121 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05002122 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002123}
2124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002125static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002126menu_destroy(struct menu *menu)
2127{
2128 widget_destroy(menu->widget);
2129 window_destroy(menu->window);
2130 free(menu);
2131}
2132
2133static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002134handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2135{
2136 struct window *window = data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002137 struct menu *menu = window->widget->user_data;
2138
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002139 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002140 * device. Or just use wl_callback. And this really needs to
2141 * be a window vfunc that the menu can set. And we need the
2142 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002143
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002144 menu->func(window->parent, menu->current, window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002145 input_ungrab(menu->input, 0);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002146 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002147}
2148
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002149static const struct wl_shell_surface_listener shell_surface_listener = {
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002150 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002151 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04002152};
2153
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002154void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01002155window_get_allocation(struct window *window,
2156 struct rectangle *allocation)
2157{
2158 *allocation = window->allocation;
2159}
2160
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002161static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002162widget_redraw(struct widget *widget)
2163{
2164 struct widget *child;
2165
2166 if (widget->redraw_handler)
2167 widget->redraw_handler(widget, widget->user_data);
2168 wl_list_for_each(child, &widget->child_list, link)
2169 widget_redraw(child);
2170}
2171
2172static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002173idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002174{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002175 struct window *window =
2176 container_of(task, struct window, redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002177
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002178 window_create_surface(window);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05002179 widget_redraw(window->widget);
Kristian Høgsberg5d129902012-01-10 10:49:41 -05002180 window_flush(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002181 window->redraw_scheduled = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002182}
2183
2184void
2185window_schedule_redraw(struct window *window)
2186{
2187 if (!window->redraw_scheduled) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002188 window->redraw_task.run = idle_redraw;
2189 display_defer(window->display, &window->redraw_task);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002190 window->redraw_scheduled = 1;
2191 }
2192}
2193
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05002194void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002195window_set_custom(struct window *window)
2196{
2197 window->type = TYPE_CUSTOM;
2198}
2199
2200void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002201window_set_fullscreen(struct window *window, int fullscreen)
2202{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002203 if (!window->display->shell)
2204 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002205
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002206 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05002207 return;
2208
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002209 if (fullscreen) {
2210 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002211 window->saved_allocation = window->allocation;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002212 wl_shell_surface_set_fullscreen(window->shell_surface,
2213 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2214 0, NULL);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002215 } else {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002216 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002217 wl_shell_surface_set_toplevel(window->shell_surface);
2218 window_schedule_resize(window,
2219 window->saved_allocation.width,
2220 window->saved_allocation.height);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002221 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002222}
2223
2224void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002225window_set_maximized(struct window *window, int maximized)
2226{
2227 if (!window->display->shell)
2228 return;
2229
2230 if ((window->type == TYPE_MAXIMIZED) == maximized)
2231 return;
2232
2233 if (window->type == TYPE_TOPLEVEL) {
2234 window->saved_allocation = window->allocation;
2235 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2236 window->type = TYPE_MAXIMIZED;
2237 } else {
2238 wl_shell_surface_set_toplevel(window->shell_surface);
2239 window->type = TYPE_TOPLEVEL;
2240 window_schedule_resize(window,
2241 window->saved_allocation.width,
2242 window->saved_allocation.height);
2243 }
2244}
2245
2246void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002247window_set_user_data(struct window *window, void *data)
2248{
2249 window->user_data = data;
2250}
2251
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04002252void *
2253window_get_user_data(struct window *window)
2254{
2255 return window->user_data;
2256}
2257
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002258void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002259window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002260 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002261{
2262 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002263}
2264
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002265void
2266window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002267 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002268{
2269 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002270}
2271
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002272void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002273window_set_data_handler(struct window *window, window_data_handler_t handler)
2274{
2275 window->data_handler = handler;
2276}
2277
2278void
2279window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2280{
2281 window->drop_handler = handler;
2282}
2283
2284void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002285window_set_close_handler(struct window *window,
2286 window_close_handler_t handler)
2287{
2288 window->close_handler = handler;
2289}
2290
2291void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002292window_set_title(struct window *window, const char *title)
2293{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05002294 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13002295 window->title = strdup(title);
2296}
2297
2298const char *
2299window_get_title(struct window *window)
2300{
2301 return window->title;
2302}
2303
2304void
Benjamin Franzkebde55ec2011-03-07 15:08:09 +01002305display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2306 int32_t x, int32_t y, int32_t width, int32_t height)
2307{
2308 struct wl_buffer *buffer;
2309
2310 buffer = display_get_buffer_for_surface(display, cairo_surface);
2311
2312 wl_buffer_damage(buffer, x, y, width, height);
2313}
2314
2315void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002316window_damage(struct window *window, int32_t x, int32_t y,
2317 int32_t width, int32_t height)
2318{
2319 wl_surface_damage(window->surface, x, y, width, height);
2320}
2321
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002322static struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002323window_create_internal(struct display *display, struct window *parent)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002324{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05002325 struct window *window;
2326
2327 window = malloc(sizeof *window);
2328 if (window == NULL)
2329 return NULL;
2330
Kristian Høgsberg78231c82008-11-08 15:06:01 -05002331 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05002332 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002333 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002334 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002335 if (display->shell) {
2336 window->shell_surface =
2337 wl_shell_get_shell_surface(display->shell,
2338 window->surface);
2339 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002340 window->allocation.x = 0;
2341 window->allocation.y = 0;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002342 window->allocation.width = 0;
2343 window->allocation.height = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002344 window->saved_allocation = window->allocation;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -04002345 window->transparent = 1;
Tiago Vignattia571e752012-02-09 19:06:54 +02002346 window->type = TYPE_TOPLEVEL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002347 window->input_region = NULL;
2348 window->opaque_region = NULL;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002349
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002350 if (display->dpy)
Benjamin Franzke22d54812011-07-16 19:50:32 +00002351#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002352 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2353 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00002354#else
2355 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2356#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05002357 else
2358 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002359
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002360 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002361 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002362
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02002363 if (window->shell_surface) {
2364 wl_shell_surface_set_user_data(window->shell_surface, window);
2365 wl_shell_surface_add_listener(window->shell_surface,
2366 &shell_surface_listener, window);
2367 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002368
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002369 return window;
2370}
2371
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002372struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002373window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002374{
2375 struct window *window;
2376
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002377 window = window_create_internal(display, NULL);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002378 if (!window)
2379 return NULL;
2380
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002381 window->type = TYPE_TOPLEVEL;
2382 if (display->shell)
2383 wl_shell_surface_set_toplevel(window->shell_surface);
2384
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002385 return window;
2386}
2387
2388struct window *
2389window_create_transient(struct display *display, struct window *parent,
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002390 int32_t x, int32_t y)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002391{
2392 struct window *window;
2393
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002394 window = window_create_internal(parent->display, parent);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002395 if (!window)
2396 return NULL;
2397
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04002398 window->type = TYPE_TRANSIENT;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002399 window->x = x;
2400 window->y = y;
2401
Kristian Høgsberg1517def2012-02-16 22:56:12 -05002402 if (display->shell)
2403 wl_shell_surface_set_transient(window->shell_surface,
2404 window->parent->shell_surface,
2405 window->x, window->y, 0);
2406
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002407 return window;
2408}
2409
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002410static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05002411menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002412{
2413 int next;
2414
2415 next = (sy - 8) / 20;
2416 if (menu->current != next) {
2417 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002418 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002419 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002420}
2421
2422static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002423menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002424 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002425 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002426{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002427 struct menu *menu = data;
2428
2429 if (widget == menu->widget)
2430 menu_set_item(data, y);
2431
2432 return POINTER_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002433}
2434
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002435static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002436menu_enter_handler(struct widget *widget,
2437 struct input *input, uint32_t time,
2438 int32_t x, int32_t y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002439{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002440 struct menu *menu = data;
2441
2442 if (widget == menu->widget)
2443 menu_set_item(data, y);
2444
2445 return POINTER_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002446}
2447
2448static void
2449menu_leave_handler(struct widget *widget, struct input *input, void *data)
2450{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002451 struct menu *menu = data;
2452
2453 if (widget == menu->widget)
2454 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002455}
2456
2457static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002458menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002459 struct input *input, uint32_t time,
2460 int button, int state, void *data)
2461
2462{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002463 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002464
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002465 if (state == 0 && time - menu->time > 500) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002466 /* Either relase after press-drag-release or
2467 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002468 menu->func(menu->window->parent,
2469 menu->current, menu->window->parent->user_data);
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002470 input_ungrab(input, time);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002471 menu_destroy(menu);
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002472 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002473}
2474
2475static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002476menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002477{
2478 cairo_t *cr;
2479 const int32_t r = 3, margin = 3;
2480 struct menu *menu = data;
2481 int32_t width, height, i;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002482 struct window *window = widget->window;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002483
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002484 cr = cairo_create(window->cairo_surface);
2485 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2486 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2487 cairo_paint(cr);
2488
2489 width = window->allocation.width;
2490 height = window->allocation.height;
2491 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05002492
2493 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002494 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2495 cairo_fill(cr);
2496
2497 for (i = 0; i < menu->count; i++) {
2498 if (i == menu->current) {
2499 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2500 cairo_rectangle(cr, margin, i * 20 + margin,
2501 width - 2 * margin, 20);
2502 cairo_fill(cr);
2503 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2504 cairo_move_to(cr, 10, i * 20 + 16);
2505 cairo_show_text(cr, menu->entries[i]);
2506 } else {
2507 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2508 cairo_move_to(cr, 10, i * 20 + 16);
2509 cairo_show_text(cr, menu->entries[i]);
2510 }
2511 }
2512
2513 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002514}
2515
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002516void
2517window_show_menu(struct display *display,
2518 struct input *input, uint32_t time, struct window *parent,
2519 int32_t x, int32_t y,
2520 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002521{
2522 struct window *window;
2523 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002524 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002525
2526 menu = malloc(sizeof *menu);
2527 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002528 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002529
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002530 window = window_create_internal(parent->display, parent);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002531 if (!window)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02002532 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002533
2534 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002535 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002536 menu->entries = entries;
2537 menu->count = count;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002539 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05002540 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002541 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002542 window->type = TYPE_MENU;
2543 window->x = x;
2544 window->y = y;
2545
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002546 wl_shell_surface_set_popup(window->shell_surface,
2547 input->input_device, time,
2548 window->parent->shell_surface,
2549 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002550
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002551 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002552 widget_set_enter_handler(menu->widget, menu_enter_handler);
2553 widget_set_leave_handler(menu->widget, menu_leave_handler);
2554 widget_set_motion_handler(menu->widget, menu_motion_handler);
2555 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05002556
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002557 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002558 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05002559}
2560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002561void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04002562window_set_buffer_type(struct window *window, enum window_buffer_type type)
2563{
2564 window->buffer_type = type;
2565}
2566
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04002567
2568static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002569display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002570 struct wl_output *wl_output,
2571 int x, int y,
2572 int physical_width,
2573 int physical_height,
2574 int subpixel,
2575 const char *make,
2576 const char *model)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002577{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002578 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002579
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002580 output->allocation.x = x;
2581 output->allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002582}
2583
2584static void
2585display_handle_mode(void *data,
2586 struct wl_output *wl_output,
2587 uint32_t flags,
2588 int width,
2589 int height,
2590 int refresh)
2591{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002592 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002593 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002594
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002595 if (flags & WL_OUTPUT_MODE_CURRENT) {
2596 output->allocation.width = width;
2597 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002598 if (display->output_configure_handler)
2599 (*display->output_configure_handler)(
2600 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002601 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002602}
2603
2604static const struct wl_output_listener output_listener = {
2605 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002606 display_handle_mode
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002607};
2608
2609static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002610display_add_output(struct display *d, uint32_t id)
2611{
2612 struct output *output;
2613
2614 output = malloc(sizeof *output);
2615 if (output == NULL)
2616 return;
2617
2618 memset(output, 0, sizeof *output);
2619 output->display = d;
2620 output->output =
2621 wl_display_bind(d->display, id, &wl_output_interface);
2622 wl_list_insert(d->output_list.prev, &output->link);
2623
2624 wl_output_add_listener(output->output, &output_listener, output);
2625}
2626
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02002627static void
2628output_destroy(struct output *output)
2629{
2630 if (output->destroy_handler)
2631 (*output->destroy_handler)(output, output->user_data);
2632
2633 wl_output_destroy(output->output);
2634 wl_list_remove(&output->link);
2635 free(output);
2636}
2637
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002638void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002639display_set_output_configure_handler(struct display *display,
2640 display_output_handler_t handler)
2641{
2642 struct output *output;
2643
2644 display->output_configure_handler = handler;
2645 if (!handler)
2646 return;
2647
2648 wl_list_for_each(output, &display->output_list, link)
2649 (*display->output_configure_handler)(output,
2650 display->user_data);
2651}
2652
2653void
2654output_set_user_data(struct output *output, void *data)
2655{
2656 output->user_data = data;
2657}
2658
2659void *
2660output_get_user_data(struct output *output)
2661{
2662 return output->user_data;
2663}
2664
2665void
2666output_set_destroy_handler(struct output *output,
2667 display_output_handler_t handler)
2668{
2669 output->destroy_handler = handler;
2670 /* FIXME: implement this, once we have way to remove outputs */
2671}
2672
2673void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002674output_get_allocation(struct output *output, struct rectangle *allocation)
2675{
2676 *allocation = output->allocation;
2677}
2678
Pekka Paalanen999c5b52011-11-30 10:52:38 +02002679struct wl_output *
2680output_get_wl_output(struct output *output)
2681{
2682 return output->output;
2683}
2684
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002685static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002686display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002687{
2688 struct input *input;
2689
2690 input = malloc(sizeof *input);
2691 if (input == NULL)
2692 return;
2693
2694 memset(input, 0, sizeof *input);
2695 input->display = d;
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002696 input->input_device =
2697 wl_display_bind(d->display, id, &wl_input_device_interface);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002698 input->pointer_focus = NULL;
2699 input->keyboard_focus = NULL;
2700 wl_list_insert(d->input_list.prev, &input->link);
2701
2702 wl_input_device_add_listener(input->input_device,
2703 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002704 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002705
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002706 input->data_device =
2707 wl_data_device_manager_get_data_device(d->data_device_manager,
2708 input->input_device);
2709 wl_data_device_add_listener(input->data_device,
2710 &data_device_listener, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002711}
2712
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002713static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02002714input_destroy(struct input *input)
2715{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05002716 input_remove_keyboard_focus(input);
2717 input_remove_pointer_focus(input, 0);
Pekka Paalanene1207c72011-12-16 12:02:09 +02002718
2719 if (input->drag_offer)
2720 data_offer_destroy(input->drag_offer);
2721
2722 if (input->selection_offer)
2723 data_offer_destroy(input->selection_offer);
2724
2725 wl_data_device_destroy(input->data_device);
2726 wl_list_remove(&input->link);
2727 wl_input_device_destroy(input->input_device);
2728 free(input);
2729}
2730
2731static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002732display_handle_global(struct wl_display *display, uint32_t id,
2733 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002734{
2735 struct display *d = data;
2736
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002737 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002738 d->compositor =
2739 wl_display_bind(display, id, &wl_compositor_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002740 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002741 display_add_output(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002742 } else if (strcmp(interface, "wl_input_device") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04002743 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002744 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002745 d->shell = wl_display_bind(display, id, &wl_shell_interface);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04002746 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsberga8d1fa72011-08-23 18:14:06 -04002747 d->shm = wl_display_bind(display, id, &wl_shm_interface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002748 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2749 d->data_device_manager =
2750 wl_display_bind(display, id,
2751 &wl_data_device_manager_interface);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002752 }
2753}
2754
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002755static void
2756display_render_frame(struct display *d)
2757{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002758 cairo_t *cr;
2759
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002760 d->frame_radius = 8;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002761 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2762 cr = cairo_create(d->shadow);
2763 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2764 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002765 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002766 cairo_fill(cr);
2767 cairo_destroy(cr);
2768 blur_surface(d->shadow, 64);
2769
2770 d->active_frame =
2771 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2772 cr = cairo_create(d->active_frame);
2773 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2774 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002775 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002776 cairo_fill(cr);
2777 cairo_destroy(cr);
2778
2779 d->inactive_frame =
2780 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2781 cr = cairo_create(d->inactive_frame);
2782 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2783 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002784 rounded_rect(cr, 16, 16, 112, 112, d->frame_radius);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04002785 cairo_fill(cr);
2786 cairo_destroy(cr);
2787}
2788
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002789static void
2790init_xkb(struct display *d)
2791{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002792 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002793
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002794 names.rules = "evdev";
2795 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002796 names.layout = option_xkb_layout;
2797 names.variant = option_xkb_variant;
2798 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002799
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04002800 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002801 if (!d->xkb) {
2802 fprintf(stderr, "Failed to compile keymap\n");
2803 exit(1);
2804 }
2805}
2806
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002807static void
2808fini_xkb(struct display *display)
2809{
2810 xkb_free_keymap(display->xkb);
2811}
2812
Yuval Fledel45568f62010-12-06 09:18:12 -05002813static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002814init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05002815{
2816 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002817 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002818
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002819 static const EGLint argb_cfg_attribs[] = {
2820 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002821 EGL_RED_SIZE, 1,
2822 EGL_GREEN_SIZE, 1,
2823 EGL_BLUE_SIZE, 1,
2824 EGL_ALPHA_SIZE, 1,
2825 EGL_DEPTH_SIZE, 1,
2826 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2827 EGL_NONE
2828 };
Yuval Fledel45568f62010-12-06 09:18:12 -05002829
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002830 static const EGLint rgb_cfg_attribs[] = {
2831 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2832 EGL_RED_SIZE, 1,
2833 EGL_GREEN_SIZE, 1,
2834 EGL_BLUE_SIZE, 1,
2835 EGL_ALPHA_SIZE, 0,
2836 EGL_DEPTH_SIZE, 1,
2837 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2838 EGL_NONE
2839 };
2840
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002841#ifdef USE_CAIRO_GLESV2
2842 static const EGLint context_attribs[] = {
2843 EGL_CONTEXT_CLIENT_VERSION, 2,
2844 EGL_NONE
2845 };
2846 EGLint api = EGL_OPENGL_ES_API;
2847#else
2848 EGLint *context_attribs = NULL;
2849 EGLint api = EGL_OPENGL_API;
2850#endif
2851
Kristian Høgsberg91342c62011-04-14 14:44:58 -04002852 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05002853 if (!eglInitialize(d->dpy, &major, &minor)) {
2854 fprintf(stderr, "failed to initialize display\n");
2855 return -1;
2856 }
2857
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002858 if (!eglBindAPI(api)) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002859 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2860 return -1;
2861 }
2862
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002863 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2864 &d->argb_config, 1, &n) || n != 1) {
2865 fprintf(stderr, "failed to choose argb config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01002866 return -1;
2867 }
2868
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04002869 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2870 &d->rgb_config, 1, &n) || n != 1) {
2871 fprintf(stderr, "failed to choose rgb config\n");
2872 return -1;
2873 }
2874
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002875 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config,
2876 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002877 if (d->rgb_ctx == NULL) {
2878 fprintf(stderr, "failed to create context\n");
2879 return -1;
2880 }
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05002881 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05002882 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02002883 if (d->argb_ctx == NULL) {
Yuval Fledel45568f62010-12-06 09:18:12 -05002884 fprintf(stderr, "failed to create context\n");
2885 return -1;
2886 }
2887
Benjamin Franzke0c991632011-09-27 21:57:31 +02002888 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01002889 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002890 return -1;
2891 }
2892
Kristian Høgsberg8def2642011-01-14 17:41:33 -05002893#ifdef HAVE_CAIRO_EGL
Benjamin Franzke0c991632011-09-27 21:57:31 +02002894 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2895 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002896 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05002897 return -1;
2898 }
Benjamin Franzke0c991632011-09-27 21:57:31 +02002899 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2900 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2901 fprintf(stderr, "failed to get cairo egl argb device\n");
2902 return -1;
2903 }
Yuval Fledel45568f62010-12-06 09:18:12 -05002904#endif
2905
2906 return 0;
2907}
2908
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02002909static void
2910fini_egl(struct display *display)
2911{
2912#ifdef HAVE_CAIRO_EGL
2913 cairo_device_destroy(display->argb_device);
2914 cairo_device_destroy(display->rgb_device);
2915#endif
2916
2917 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2918 EGL_NO_CONTEXT);
2919
2920 eglTerminate(display->dpy);
2921 eglReleaseThread();
2922}
2923
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002924static int
2925event_mask_update(uint32_t mask, void *data)
2926{
2927 struct display *d = data;
2928
2929 d->mask = mask;
2930
2931 return 0;
2932}
2933
2934static void
2935handle_display_data(struct task *task, uint32_t events)
2936{
2937 struct display *display =
2938 container_of(task, struct display, display_task);
2939
2940 wl_display_iterate(display->display, display->mask);
2941}
2942
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002943struct display *
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002944display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002945{
2946 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002947 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002948 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002949 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002950
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002951 g_type_init();
2952
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002953 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002954 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002955 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002956
2957 xkb_option_group = g_option_group_new("xkb",
2958 "Keyboard options",
2959 "Show all XKB options",
2960 NULL, NULL);
2961 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2962 g_option_context_add_group (context, xkb_option_group);
2963
2964 if (!g_option_context_parse(context, argc, argv, &error)) {
2965 fprintf(stderr, "option parsing failed: %s\n", error->message);
2966 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002967 }
2968
Tim Wiederhake748f6722011-01-23 23:25:25 +01002969 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04002970
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002971 d = malloc(sizeof *d);
2972 if (d == NULL)
2973 return NULL;
2974
Tim Wiederhake81bd9792011-01-23 23:25:26 +01002975 memset(d, 0, sizeof *d);
2976
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05002977 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002978 if (d->display == NULL) {
2979 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002980 return NULL;
2981 }
2982
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002983 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2984 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2985 d->display_task.run = handle_display_data;
2986 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2987
2988 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002989 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05002990 wl_list_init(&d->output_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002991
Kristian Høgsberg478d9262010-06-08 20:34:11 -04002992 /* Set up listener so we'll catch all events. */
2993 wl_display_add_global_listener(d->display,
2994 display_handle_global, d);
2995
2996 /* Process connection events. */
2997 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05002998 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002999 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05003000
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05003001 d->image_target_texture_2d =
3002 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3003 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3004 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3005
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003006 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04003007
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04003008 display_render_frame(d);
3009
Kristian Høgsberg478d9262010-06-08 20:34:11 -04003010 wl_list_init(&d->window_list);
3011
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04003012 init_xkb(d);
3013
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003014 return d;
3015}
3016
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003017static void
3018display_destroy_outputs(struct display *display)
3019{
3020 struct output *tmp;
3021 struct output *output;
3022
3023 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3024 output_destroy(output);
3025}
3026
Pekka Paalanene1207c72011-12-16 12:02:09 +02003027static void
3028display_destroy_inputs(struct display *display)
3029{
3030 struct input *tmp;
3031 struct input *input;
3032
3033 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3034 input_destroy(input);
3035}
3036
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003037void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003038display_destroy(struct display *display)
3039{
Pekka Paalanenc2052982011-12-16 11:41:32 +02003040 if (!wl_list_empty(&display->window_list))
3041 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3042
3043 if (!wl_list_empty(&display->deferred_list))
3044 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3045
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003046 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02003047 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02003048
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003049 fini_xkb(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02003050
3051 cairo_surface_destroy(display->active_frame);
3052 cairo_surface_destroy(display->inactive_frame);
3053 cairo_surface_destroy(display->shadow);
3054 destroy_pointer_surfaces(display);
3055
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003056 fini_egl(display);
3057
Pekka Paalanenc2052982011-12-16 11:41:32 +02003058 if (display->shell)
3059 wl_shell_destroy(display->shell);
3060
3061 if (display->shm)
3062 wl_shm_destroy(display->shm);
3063
3064 if (display->data_device_manager)
3065 wl_data_device_manager_destroy(display->data_device_manager);
3066
3067 wl_compositor_destroy(display->compositor);
3068
3069 close(display->epoll_fd);
3070
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02003071 wl_display_flush(display->display);
3072 wl_display_destroy(display->display);
3073 free(display);
3074}
3075
3076void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02003077display_set_user_data(struct display *display, void *data)
3078{
3079 display->user_data = data;
3080}
3081
3082void *
3083display_get_user_data(struct display *display)
3084{
3085 return display->user_data;
3086}
3087
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003088struct wl_display *
3089display_get_display(struct display *display)
3090{
3091 return display->display;
3092}
3093
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05003094struct output *
3095display_get_output(struct display *display)
3096{
3097 return container_of(display->output_list.next, struct output, link);
3098}
3099
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003100struct wl_compositor *
3101display_get_compositor(struct display *display)
3102{
3103 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05003104}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003105
3106EGLDisplay
3107display_get_egl_display(struct display *d)
3108{
3109 return d->dpy;
3110}
3111
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003112struct wl_data_source *
3113display_create_data_source(struct display *display)
3114{
3115 return wl_data_device_manager_create_data_source(display->data_device_manager);
3116}
3117
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003118EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02003119display_get_rgb_egl_config(struct display *d)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003120{
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04003121 return d->rgb_config;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003122}
3123
Benjamin Franzke0c991632011-09-27 21:57:31 +02003124EGLConfig
3125display_get_argb_egl_config(struct display *d)
3126{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05003127 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003128}
3129
Kristian Høgsberg58eec362011-01-19 14:27:42 -05003130struct wl_shell *
3131display_get_shell(struct display *display)
3132{
3133 return display->shell;
3134}
3135
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003136int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003137display_acquire_window_surface(struct display *display,
3138 struct window *window,
3139 EGLContext ctx)
3140{
Benjamin Franzke22d54812011-07-16 19:50:32 +00003141#ifdef HAVE_CAIRO_EGL
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003142 struct egl_window_surface_data *data;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003143 cairo_device_t *device;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003144
3145 if (!window->cairo_surface)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003146 return -1;
Benjamin Franzke0c991632011-09-27 21:57:31 +02003147 device = cairo_surface_get_device(window->cairo_surface);
3148 if (!device)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003149 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003150
Benjamin Franzke0c991632011-09-27 21:57:31 +02003151 if (!ctx) {
3152 if (device == display->rgb_device)
3153 ctx = display->rgb_ctx;
3154 else if (device == display->argb_device)
3155 ctx = display->argb_ctx;
3156 else
3157 assert(0);
3158 }
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003159
3160 data = cairo_surface_get_user_data(window->cairo_surface,
3161 &surface_data_key);
3162
Pekka Paalanen9015ead2011-12-19 13:57:59 +02003163 cairo_device_flush(device);
Benjamin Franzke0c991632011-09-27 21:57:31 +02003164 cairo_device_acquire(device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003165 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3166 fprintf(stderr, "failed to make surface current\n");
Benjamin Franzke1a89f282011-10-07 09:33:06 +02003167
3168 return 0;
3169#else
3170 return -1;
Benjamin Franzke22d54812011-07-16 19:50:32 +00003171#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003172}
3173
3174void
Benjamin Franzke0c991632011-09-27 21:57:31 +02003175display_release_window_surface(struct display *display,
3176 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003177{
Benjamin Franzke0c991632011-09-27 21:57:31 +02003178#ifdef HAVE_CAIRO_EGL
3179 cairo_device_t *device;
3180
3181 device = cairo_surface_get_device(window->cairo_surface);
3182 if (!device)
3183 return;
3184
3185 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003186 fprintf(stderr, "failed to make context current\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02003187 cairo_device_release(device);
3188#endif
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003189}
3190
3191void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003192display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003193{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003194 wl_list_insert(&display->deferred_list, &task->link);
3195}
3196
3197void
3198display_watch_fd(struct display *display,
3199 int fd, uint32_t events, struct task *task)
3200{
3201 struct epoll_event ep;
3202
3203 ep.events = events;
3204 ep.data.ptr = task;
3205 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3206}
3207
3208void
3209display_run(struct display *display)
3210{
3211 struct task *task;
3212 struct epoll_event ep[16];
3213 int i, count;
3214
Pekka Paalanen826d7952011-12-15 10:14:07 +02003215 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003216 while (1) {
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003217 wl_display_flush(display->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003218
3219 while (!wl_list_empty(&display->deferred_list)) {
3220 task = container_of(display->deferred_list.next,
3221 struct task, link);
3222 wl_list_remove(&task->link);
3223 task->run(task, 0);
3224 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05003225
3226 if (!display->running)
3227 break;
3228
3229 wl_display_flush(display->display);
3230
3231 count = epoll_wait(display->epoll_fd,
3232 ep, ARRAY_LENGTH(ep), -1);
3233 for (i = 0; i < count; i++) {
3234 task = ep[i].data.ptr;
3235 task->run(task, ep[i].events);
3236 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003237 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003238}
Pekka Paalanen826d7952011-12-15 10:14:07 +02003239
3240void
3241display_exit(struct display *display)
3242{
3243 display->running = 0;
3244}